When dealing with low latency programming, e.g., micro seconds or nano seconds, one thing we want to avoid as much as we can is context switches. Once a context witch happens, the program switches from use space to kernel space, and this is very cost, and the latency will increase quite dramatically.
How to avoid the context switches then?
First, avoid system calls. if the the same thing can be done in the user space quickly, don't rely on system calls. For example, many programmers nowadays use the user space spin locks quite often, this can be done by some simple atomic operations. Otherwise, you would fall to pthread_mutex, which is quite expensive.
Second, avoid frequent memory allocations, e.g., malloc/free, or new/delete, these function calls eventually will call some system calls to get some memory from heap (brk). Instead, you can allocate the memory just once, but allocate enough and create a memory pool with that memory.
No comments:
Post a Comment