Thread: Why is my function macro so stupid slow?

  1. #1
    Registered User
    Join Date
    Dec 2022
    Posts
    2

    Why is my function macro so stupid slow?

    So I am trying to make simple ECS system for my game framework.
    I decided to make a macro function to have a universal way to create / register components instead of users having to call specific functions for things like int,bool,char and so on. Plus than users will have to make there own functions for custom structs. Macros seem the way to go. The issue is that is so stupid slow. Using it 3 times makes the application stall for 10-15 seconds on startup (release). If I use it to many times the application just crashes. In debug mode 3 takes to long and application crashes.

    I am new-Ish to C, so I am probably doing something stupid.

    Here is my macro code
    Code:
    // Macro : Entities Component Initialize Macro
    #define CALYPSO_FRAMEWORK_ENTITIES_SIMPLE_REGISTER_COMPONENT(TYPE,ID,ARRAY,DEFAULT_VALUE) do\
    {\
        unsigned int count = _calypso_framework_entities_simple_entity_count_max;\
        ARRAY = (TYPE*)malloc(count * sizeof(TYPE));\
        for (int i = 0; i < count; i++)\
            ARRAY = DEFAULT_VALUE;\
    } while (0)
    Here is how its being used (Called once)
    Code:
    calypso_framework_entities_simple_init(1000);
    _entity_count_max =
    calypso_framework_entities_simple_get_entity_count_max_ptr();
    CALYPSO_FRAMEWORK_ENTITIES_SIMPLE_REGISTER_COMPONENT(int, "position_x",
                                                         _entity_position_x_array,
                                                         600);
    CALYPSO_FRAMEWORK_ENTITIES_SIMPLE_REGISTER_COMPONENT(float, "position_y",
                                                         _entity_position_y_array,
                                                         300);
    CALYPSO_FRAMEWORK_ENTITIES_SIMPLE_REGISTER_COMPONENT(float, "bounds_x",
                                                         _entity_bounds_x_array,
                                                         130);
    Here is just the regular function which is much faster.
    Code:
    int *calypso_framework_entities_simple_create_entity_data_int_array(int *array,
                                                                        const int
                                                                        default_value)
    {
      const int count = *_calypso_framework_entities_simple_entity_count_max;
      array = malloc(count * sizeof(int));
      for (int i = 0; i < count; i++)
        array = default_value;
      return array;
    }
    Not sure if this is helpful but here is my compile flags for release.
    Code:
    CC_FLAGS_RELEASE = -std = c99 - O3 - Wall - DNDEBUG
    Last edited by Salem; 12-16-2022 at 11:46 PM. Reason: Removed crayola

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    Why does the macro not have an asterisk before _calypso_framework_entities_simple_entity_count_ma x?
    A little inaccuracy saves tons of explanation. - H.H. Munro

  3. #3
    Registered User
    Join Date
    Dec 2022
    Posts
    2
    Your right. Such a simple thing. I added asterisk and solved everything. I feel extra dumb overlooking it.

  4. #4
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,111
    Quote Originally Posted by WayfarerGameDev View Post
    Your right. Such a simple thing. I added asterisk and solved everything. I feel extra dumb overlooking it.
    Don't feel dumb! Most programmers would never admit to all the typos and dumb mistakes they make on a daily basis! I know I wouldn't! ;^)

    That's why the error messages from the compiler are getting better and better!

    Turn on your compiler warnings and set the warning level to the highest level.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Slow response from function
    By rogster001 in forum C++ Programming
    Replies: 7
    Last Post: 09-22-2009, 02:36 AM
  2. Macro has same name as Function?
    By coolclu3 in forum C Programming
    Replies: 10
    Last Post: 09-28-2007, 04:55 AM
  3. macro function
    By bradleyd in forum C Programming
    Replies: 8
    Last Post: 05-21-2007, 05:18 PM
  4. Very slow file writing of 'fwrite' function in C
    By scho in forum C Programming
    Replies: 6
    Last Post: 08-03-2006, 02:16 PM
  5. slow function
    By joed in forum C Programming
    Replies: 7
    Last Post: 07-17-2005, 11:24 AM

Tags for this Thread