-
Notifications
You must be signed in to change notification settings - Fork 57
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
Avoid unnecessary checks when calling readCodePointValue on Buffer #343
Conversation
Improves performance by ~10%
@@ -260,6 +260,9 @@ public fun Source.readString(byteCount: Long): String { | |||
*/ | |||
@OptIn(InternalIoApi::class) | |||
public fun Source.readCodePointValue(): Int { | |||
if (this is Buffer) { |
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.
@fzhinkin question, would this be a valid solution to avoid an is
check?
public fun Buffer.readCodePointValue(): Int {
return commonReadUtf8CodePoint()
}
public fun Source.readCodePointValue(): Int {
...
}
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.
No, it wouldn't, as extension functions are resolved statically based on the declared receiver's type.
So if there's a code like
val src: Source = Buffer().apply { ... }
println(src.readCodePointValue())
readCodePointValue
will be resolved to Source.readCodePointValue
and an extension defined on Buffer
won't help.
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.
@fzhinkin thanks! If we have the additional extension function tho, it will avoid the check when the type is Buffer
, which I think is the most common case.
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.
it will avoid the check when the type is Buffer, which I think is the most common case.
Indeed, at least for Okio, the corresponding function is invoked on Buffer
more frequently, compared to BufferedSource
.
I don't really like the idea of adding an extra extension function, as performance benefits should be neglectingly small. On the other hand, there will be some benefits, so I consider adding the function later in the course of #342.
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.
Thanks! I think the best approach would be to do the same thing you did for unsafe operations, throw in a quick kotlinx-benchmark. Would be interesting to document all of these cases for JS.
For K/JS here we are lucky because Buffer is a class, and the is
check will be compiled to a native instanceof
. When the check is done on an interface, there is a lot more work involved.
Improves
readCodePointValue
performance by ~10% when the receiver isBuffer
.Addresses a particular issue uncovered in Kotlin/kotlinx.serialization#2707.
In general, similar issues will be tracked by #342.
Benchmarking results w/ jdk21 on macOS host w/ AS M2 Pro CPU: