yhirose/cpp-httplib

Slow performance caused by `get_remote_ip_and_port` and `get_local_ip_and_port`

Closed this issue · 1 comments

Currently, one of bottle necks of performance is caused by get_remote_ip_and_port and get_local_ip_and_port.

image

cpp-httplib/httplib.h

Lines 6961 to 6967 in 61c4180

strm.get_remote_ip_and_port(req.remote_addr, req.remote_port);
req.set_header("REMOTE_ADDR", req.remote_addr);
req.set_header("REMOTE_PORT", std::to_string(req.remote_port));
strm.get_local_ip_and_port(req.local_addr, req.local_port);
req.set_header("LOCAL_ADDR", req.local_addr);
req.set_header("LOCAL_PORT", std::to_string(req.local_port));

These slow functions are called regardless of whether REMOTE_ADDR, REMOTE_PORT, LOCAL_ADDR, or LOCAL_PORT will be used later on.

I am thinking of making them "lazy evaluation" calls, so that we don't have to pay such costs.

  • REMOTE_ADDR, REMOTE_PORT, LOCAL_ADDR, LOCAL_PORT will be removed from Request
  • remote_addr, remote_port, local_addr, local_port will be removed from Request
  • get_remote_ip, get_remote_port, get_local_ip and get_local_port will be added

This will be a break change, and will be available from v0.18.0.

To avoid the breaking change, we can share the same information per socket connection. In other words, we can share it over requests in the same KeepAlive connection.