chriskohlhoff / asio

Asio C++ Library
http://think-async.com/Asio
4.72k stars 1.19k forks source link

Write on SSL stream is much slower #1356

Closed FrothyB closed 9 months ago

FrothyB commented 10 months ago

net::write(socket_, net::buffer(data, length), ec); takes about 8 microseconds for a tcp::socket and about 16 for a ssl::stream<tcp::socket>. The data is small (<100 bytes). This seems like a larger performance delta than should be expected, could there be some inefficiency in the implementation of ssl::stream causing this? How can I help narrow it down?

Note that for a simple tcp::socket, net::write and socket_.write_some take roughly the same amount of time, with write_some being marginally faster.

FrothyB commented 10 months ago

Running strace indicates that both cases use a sendto syscall, but ssl::stream results in an extra allocation with mmap.

FrothyB commented 9 months ago

Turns out that OpenSSL has a large call-stack, and a significant amount of branching and pointer indirection. Using it very frequently or in a loop the performance hit is as expected, but if you use it less often then performance is very poor as caches and branch predictors are not warmed up to handle it.