Skip to content

Avoid unnecessary checks when calling readCodePointValue on Buffer #343

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

Merged
merged 1 commit into from
Jun 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions core/common/src/Utf8.kt
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,9 @@ public fun Source.readString(byteCount: Long): String {
*/
@OptIn(InternalIoApi::class)
public fun Source.readCodePointValue(): Int {
if (this is Buffer) {
Copy link
Contributor

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 { 
  ... 
}

Copy link
Collaborator Author

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.

Copy link
Contributor

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.

Copy link
Collaborator Author

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.

Copy link
Contributor

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.

return commonReadUtf8CodePoint()
}
require(1)

val b0 = buffer[0].toInt()
Expand Down