I was implementing a circular buffer to store fixed-sized data structures like a queue. This circular buffer is initialized with three parameters:-
/*
* Initialize the ring buffer.
* @capacity Max capacity of ring buffer.
* @item_size Fixed size of item that will be put in this circular buffer.
* @item_cleaner Clean callback, NULL if cleanup not required.
*/
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 last item is always replaced when new item is put in full circular buffer. Since, dynamically allocated objects can also be put into this buffer, therefore, circular buffer keeps reference a clean-up callback function to free up the items when they are replaced or deleted. But at the same time, this call back function can also be NULL (when no clean up is required). Everywhere in my code, I've statements like these:-
if(buffer->callback != NULL)
buffer->callback(item);
Now, to prevent these if statement, I put empty stub function when user does not provide any call back function. This prevent me to check every time if callback function is NULL or not.
Using this approach, my code looks neat to me. But I'm not sure, which one of them is faster? On assembly level, how does empty function call and if statement relate in terms of speed? Are they equivalent?