Turtle icon indicating copy to clipboard operation
Turtle copied to clipboard

Enhancement: strip off the std::map,use std::unordered_map instead

Open YukunJ opened this issue 2 years ago • 1 comments

Thanks to Cppers on reddit here.

Currently the Looper use std::map<int, std::unique_ptr<Connection>> to keep track of which connection's lifecycle is under its monitoring.

/**
 * This Looper acts as the executor on a single thread
 * adopt the philosophy of 'one looper per thread'
 * */
class Looper {
 public:
  ...
 private:
  ...
  std::map<int, std::unique_ptr<Connection>> connections_;
  ...
};

However, since we don't required the ordered property provided by std::map, a better performance could be achieved by using std::unordered_map. Typically given the fact that currently any access to this connections_ variable requires mutex for synchronization.

I plan to implement this feature shortly.

Yukun Feb 07

YukunJ avatar Feb 07 '23 15:02 YukunJ

Experimental profiling result:

On AWS m2.large with 8 vCPUs and 32GB Memory, 50GB root disk, running 10500 clients concurrently for 5 seconds, and taking average of 5 times:

With std::unordered_map, QPS = 58.9k. With std::map, QPS = 59.0k. With boost::unordered_map, QPS=59.1k.

This is a bit surprising, as it reveals that the performance bottlenect now is not the critical session of adding/removing Connection from the map data structure.

YukunJ avatar Feb 07 '23 19:02 YukunJ