-
Notifications
You must be signed in to change notification settings - Fork 422
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Optimize parsing headers for pekko-http and akka-http #3575
Conversation
Maybe we can optimize |
But otherwise nice gains! :) |
@@ -132,7 +132,7 @@ private[akkahttp] class AkkaToResponseBody(implicit m: Materializer, ec: Executi | |||
} | |||
|
|||
private def parseContentType(ct: String): ContentType = | |||
ContentType.parse(ct).getOrElse(throw new IllegalArgumentException(s"Cannot parse content type: $ct")) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm wondering if can get some "wild" media types, e.g. with some parameters, which could fill our cache with junk. We have a list of (currently) 1208 mime types in mimeByExtension.txt
, but it has some very weird media types, and parsing them upfront would take significant time. Or maybe we could cache only if contentType.mediaType.params.isEmpty
? (well .. maybe except for the charset)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like the idea of storing only values which have empty params (except the charset), this should significantly limit the cases where the cache is not useful after being filled with a lot of types.
I can work some more on this, it's kinda outside of scope of this pekko-specific PR anyway. |
Fixes #3544
CPU samples registered in profiler:
Before:
After:
Throughput in
SimpleGet
Simulation for 128 users:Before:
After:
(Yes, the timestamps are OK, I ran the test on the optimized server first :))
Note:
The issue mentions one more hot frame:
It's a common issue caused by the base
ServerRequest.acceptContentTypes
callingAccepts.parse
fromsttp-model
, involving some pattern matching. I tried two approaches, without great results:HttpHeader.parse
which is more efficient, then repacking Pekko datatypes to Tapir's. This lowered the load to ~1.9%. Again, doesn't seem worthwhile, the repacking was also pretty complex and error prone, it probably involved unnecessary allocations as well.HttpHeader.parse
is not very efficient by itself, because internally it callsevery time. The full power of
HeaderParser
could be leveraged if the parser was instantiated once and reused within a connection, then passed to new connections once the connection is closed (it has its own internal cache). This is what happens underneath if one uses raw pekko-http.