-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Re-implement Buffer.rangeEquals on Buffer.indexOf #1628
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
Conversation
The old rangeEquals function was embarassingly inefficient, using a full scan of the segments linked-list for each element. That optimization is already done in indexOf(), which has almost all of what we need for rangeEquals(). This adds an offset and count slice to the input ByteString in indexOf(), and then leverages that to simplify rangeEquals().
if (data[pos] == b0 && rangeEquals(s, pos + 1, targetByteArray, 1, bytesSize)) { | ||
if ( | ||
data[pos] == b0 && | ||
rangeEquals(s, pos + 1, targetByteArray, bytesOffset + 1, byteCount) |
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.
Note that this rangeEquals() is fast, because it accepts a Segment
for navigation.
return false | ||
} | ||
for (i in 0 until byteCount) { | ||
if (this[offset + i] != bytes[bytesOffset + i]) { |
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.
This was slow . . . walking through the segments linked list N times.
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.
Would this likely make the original buffering issue a non issue even before the okhttpo fixes landed?
Or just a performance win?
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.
Just a performance win.
@@ -418,19 +441,18 @@ internal inline fun RealBufferedSource.commonRangeEquals( | |||
): Boolean { |
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.
Also reimplement BufferedSource.rangeEquals()
on BufferedSource.indexOf()
. Of course.
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.
LGTM - but don't put a lot of weight on me spotting something here.
The old rangeEquals function was embarassingly inefficient, using a full scan of the segments linked-list for each element. That optimization is already done in indexOf(), which has almost all of what we need for rangeEquals().
This adds an offset and count slice to the input ByteString in indexOf(), and then leverages that to simplify rangeEquals().