-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
KTOR-7941 Fix performance on readUtfLineTo #4537
Conversation
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.
Could you add a benchmark before merging?
while (!isClosedForRead) { | ||
while (!readBuffer.exhausted()) { | ||
when (val b = readBuffer.readByte()) { | ||
CR -> { |
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.
I was assuming indexOf is working faster than manual readByte
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.
Yeah, it's all buffered anyway so it seems to be roughly the same implementation.
Oh! I just realized why it's so slow! It's because if you have an input with no \r
characters, then it would be O(n^2) for the previous implementation, because it searches to the end of the content every time!
You mean adding a benchmark to |
63e2e21
to
ef7c54c
Compare
Benchmark: ktorio/ktor-benchmarks#49 |
0a71fa9
to
b1172f7
Compare
b1172f7
to
0590d42
Compare
Subsystem
Core, I/O
Motivation
KTOR-7941 A Performance issue reading with ByteReadChannel.readUTF8LineTo request body
Solution
When reading input that is missing either CR or LF characters, the current implementation will read the full buffer for every line, which leads to a
O(n^2)
performance, which is very bad for this kind of operation when there are many newlines with only\n
for example.