I used a circular buffer to store fixed-size data structures such as a queue. This circular buffer is initialized with three parameters: -
ringbuf* ringbuf_create(size_t capacity, size_t item_size, clean_up_cb item_cleaner)
My circular buffer is always in wrapping
mode, which means that the last element is always replaced when a new element is placed in a full circular buffer. Since dynamically allocated objects can also be placed in this buffer, the circular buffer therefore saves a reference to the cleanup callback function to free the elements when they are replaced or deleted. But at the same time, this callback function can also be NULL
(unless cleanup is required). Everywhere in my code I have statements like this: -
if(buffer->callback != NULL) buffer->callback(item);
Now, to prevent this if
, I put an empty stub function when the user does not provide any callback function. This prevents me from checking every time if the callback function is NULL
or not.
Using this approach, my code looks neat to me. But I'm not sure which one is faster? At the assembly level, how do empty function call
and if statement
relate in terms of speed? Are they equivalent?
source share