Search code examples
c++memory-managementiocp

Server memory management


I'm developing server on Windows. My server uses IOCP sockets and a database. Usual situation is that user connects and stays connected for a long time. While connected user can request some data from server. Usually request is associated with db access. Client request messages are of variable length. Messages are packet with google protobuf and fixed length header is sent before every message.

Is there anything different I can do regarding memory management, instead of using usual new/delete functions, that can speed my server up? The only idea I have right now is to have a pool of header buffers and to reuse them instead allocating/deallocating them frequently. What do you think about having separate heap for each user, so on user disconnect I could just delete all user data at once instead of using dozen of delete calls ? Any tip or trick regarding this topic is appreciated.

Thanks.


Solution

  • A pool of buffers is a good idea, I tend to have a "buffer allocator" object that all connections share and that can allocate fixed sized buffers for I/O purposes. It may be a good idea to also pool at the connection level (i.e. each socket has a smaller pool for reuse) as this reduces cross connection contention on the shared allocator.

    One heap per connection seems to be a bit of a heavy-weight solution to the problem.

    I have some code that you can download from here that demonstrates the shared buffer allocator approach.