Skip to content
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

Micro-optimizations and improved IOException error handling #2638

Merged
merged 2 commits into from
Jan 20, 2024

Conversation

kyri-petrou
Copy link
Collaborator

This PR contains some micro-optimizations and small fixes I run across while working on other issues:

  1. Use Netty's headers.get(key) in Headers.Native to obtain a header value. Note that this method uses a case-insensitive key, so the behaviour should be the same as previously. The performant benefit of this depends largely on the number of headers in the request. In the added benchmark, that's ~x5 faster than the current implementation
  2. Avoid pattern for converting between zio.http.Status <-> io.netty.handler.codec.http.HttpResponseStatus. Using the status code to do the conversion is both less boilerplate-y but also more performant as the pattern matching can be compiled to a tableswitch.
  3. When handling IOException in ServerInboundHandler#exceptionCaught, check if the message is not null and that it contains the Connection reset string. This is because when using native transports (e.g., EPoll, KQueue), the Connection reset message is prefixed by the native method's name
  4. Small micro-optimizations to CharSequenceExtensions.compare by checking that the length of the CharSequence is the same prior to iterating the chars, and by avoiding calls to toLower when not necessary. Performance benefit is highly dependent on different factors (in some cases, up to 1 order of magnitude), and is now en par with Java's equalsIgnoreCase method

@codecov-commenter
Copy link

Codecov Report

Attention: 3 lines in your changes are missing coverage. Please review.

Comparison is base (95b773b) 64.89% compared to head (02cfe4f) 65.08%.

Files Patch % Lines
...a/zio/http/netty/server/ServerInboundHandler.scala 25.00% 3 Missing ⚠️

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2638      +/-   ##
==========================================
+ Coverage   64.89%   65.08%   +0.18%     
==========================================
  Files         144      144              
  Lines        8495     8386     -109     
  Branches     1535     1498      -37     
==========================================
- Hits         5513     5458      -55     
+ Misses       2982     2928      -54     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Contributor

@987Nabil 987Nabil left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm
But you have to fix the build 🙂

@@ -60,9 +59,9 @@ private[netty] object Conversions {

def headersToNetty(headers: Headers): HttpHeaders =
headers match {
case Headers.FromIterable(_) => encodeHeaderListToNetty(headers.toList)
case Headers.FromIterable(_) => encodeHeaderListToNetty(headers)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Love this change 👍

@@ -176,7 +176,7 @@ object Status {
Try(code.toInt).toOption.map(fromInt)

def fromInt(code: Int): Status = {
code match {
(code: @annotation.switch) match {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just for me: Is this an optional hint? Might the compiler create a switch without it? Might the compiler not create a switch with it?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might the compiler create a switch without it

Yes

Might the compiler not create a switch with it

No

This is very similar to tailrec; if the compiler can create a lookupswitch or tableswitch, it will do it even without the annotation. However, if the annotation is present, then it'll raise an error if a lookupswitch / tableswitch cannot be created from the expression.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thx! 👍

@kyri-petrou
Copy link
Collaborator Author

But you have to fix the build

@987Nabil CI is failing due to a 502 error from sonatype when fetching dependencies. I think the failing task just needs to be retriggered.

@987Nabil 987Nabil merged commit 49d4aa4 into zio:main Jan 20, 2024
14 checks passed
@kyri-petrou kyri-petrou deleted the fixes-and-microoptimizations branch January 20, 2024 23:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants