r/cpp • • Mar 06 '26

A high performance networking networking lib

So i have been programmig a multiprotocol networking lib with C++ to work with TCP/UDP, and diferent protocols. Im doing it as a hobby project to learn about high performance programming, sockets, multithreading, and diferent protocols like http1, 2, 3 CQL. The project started when i wanted to implement a basic NoSQL database, I started the networking part, and then... well, I fell into the rabbit hole.

The first step I did was a TCP listener in windows (i will make also a linux implementation later) using IOCP. After some time of performance tunning I managed to get this results with bombardier benchmark:

Bombarding http://0.0.0.0:80/index.html for 30s using 200 connection(s)

Done!
Statistics        Avg      Stdev        Max
  Reqs/sec    205515.90   24005.89  258817.56
  Latency        0.95ms   252.32us    96.90ms
  Latency Distribution
     50%     1.00ms
     75%     1.07ms
     90%     1.69ms
     95%     2.02ms
     99%     3.41ms
  HTTP codes:
    1xx - 0, 2xx - 6168458, 3xx - 0, 4xx - 0, 5xx - 0
    others - 116
  Throughput:    34.12MB/s

The responses where short "Hello world" http messages.

What do you think about these results? They were executed in a i5-11400, 16GB 2333Mhz RAM PC.

And also, i will start to benchmark for largest requests, constant open/closing connections, and implement TLS. Is there anything I should keep in mind?

If you want to see the code, here it is (it may be a bit of a mess... sorry).

Note that I did not use AI for coding at all, it is a project for purely learning.

Edit: I used LLM to document the functions with doxygen style docs (most comments are outdated though, i made many changes). But not a single line if code was written with AI.

Edit: I used Intel VTune to try to check where the bottleneck is, and it seems it's on WSASend function, probably due to running both the benchmark and the app in the same machine

10 Upvotes

20 comments sorted by

View all comments

2

u/Chaosvex Mar 08 '26 edited Mar 08 '26

Had a look at the code and commits and noticed that you swapped a mutex for a shared mutex, with a comment saying it was for improved performance but you don't actually use it any differently than the standard mutex.

Either way, shared mutex is very slow and you need to think carefully about how you use them. If you're only holding the lock for a very brief period, it's often faster to stick to the standard mutex. Shared mutex shines when you're going to be holding locks for a decent chunk of time and it isn't cheaper to just take a copy of the data and write it back with a pointer swap when you're done

2

u/libichi Mar 08 '26

So I took a second look, and in fact, no lock is needed at all. I made a change later were I used one assembling queue per assembling thread instead of a shared queue between all assembling threads, and sharding client connections for each queue (with a basic modulo op using the client id)

No data is shared between them, avoiding any race conditions. I can remove all locks safely. Thank you for pointing me that!

3

u/Chaosvex Mar 08 '26

No locks, even better! :)

1

u/libichi Mar 08 '26

Thanks! I thought that having many readers would cause a bottleneck when using the normal lock (the iocp listener thread + the assembling threads + user threads processing the requests.

-1

u/Icy_Shopping3474 Mar 08 '26

That's probably because of a "optimize my code" prompt.