-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
Content.Sink.write(sink, last, utf8Content, callback) could become faster #12469
Comments
You are right, a quick JMH benchmark clearly shows that wrap/getBytes is consistenly faster than encoding:
so I'm going to create a pull request to submit this enhancement. Thanks for the suggestion! |
@lorban what's the GC difference like? |
Signed-off-by: Ludovic Orban <[email protected]>
@joakime Interesting question. JMH's GCProfiler reports that the Still, GC'ing seems to be faster than encoding. |
How is this not a JDK bug? |
My guess as to why it not a JDK bug is that Charset.encode(String) is very explicit that it must produce the same result as Charset.encode(CharBuffer), which in turn - quoting the Javadoc - always replaces malformed-input and unmappable-character sequences with charset's default replacement string - which may or may not be the case with JDK's UTF-8 encoding. |
These results are probably just an artifact of the higher overall throughput. |
Error handling / Bad input handling is an important aspect of this. @lorban what happens if the new technique encounters bad/malformed input? |
Signed-off-by: Ludovic Orban <[email protected]>
@gnikolaidis I've modified my benchmark to make it more apparent that when the given string is purely ASCII, the wrap/getBytes throughput bottlenecks on memory bandwidth, so it's purely a memcopy/GC benchmark in that case. In all other cases, some encoding logic has to run and it's more complex to compare. @joakime since the source is a |
Signed-off-by: Ludovic Orban <[email protected]>
Signed-off-by: Ludovic Orban <[email protected]>
Signed-off-by: Ludovic Orban <[email protected]>
Signed-off-by: Ludovic Orban <[email protected]>
Jetty version(s)
Jetty 12.0.x
Enhancement Description
Looking at the source code, I see that the method body of
Content.Sink.write(sink, last, utf8Content, callback)
is usingStandardCharsets.UTF_8.encode(utf8Content)
to create the UTF-8 ByteBuffer.I believe that
ByteBuffer.wrap(utf8Content.getBytes(StandardCharsets.UTF_8))
would be quite faster, as in recent Java versions (which use UTF8 internally) it is implemented as a simpleSystem.arrayCopy()
.The text was updated successfully, but these errors were encountered: