Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8000803
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T15:54:44+00:00 2026-06-04T15:54:44+00:00

I was implementing a circular buffer to store fixed-sized data structures like a queue.

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T15:54:46+00:00Added an answer on June 4, 2026 at 3:54 pm

    An empty stub function is actually two JMP operations and allot of PUSH/POP operations on the CPU. An IF is usually a single COMP operation which is much cheaper than any JMP+PUSHS+POPS.

    If your ‘IF’s usually return false/true then I won’t worry about it as the CPU optimizes IFs in a very good way by predicting the result as long as the IFs are “predictable” (usually returns true or false or has some pattern of true/false)

    I would go with the IFs.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need help on implementing a circular progress bar like this: How should I
I see a lot of templates and complicated data structures for implementing a circular
hi i am implementing gallery application in this gallery circular order draging left to
When implementing move constructors and move assignment operators, one often writes code like this:
I need a python script implementing a circular buffer for rows in a text
I'm having a lot of trouble implementing this deque using a circular array; in
implementing publishActivity in PHP using the REST API using this code: $activity = array(
implementing service something similar with tinyurl or bit.ly, I'm would like to expose service
Implementing a custom VM and I've come to use registers (these will store pointers
When implementing HTTP services in node.js, there is a lot of sample code like

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.