-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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
[O] Optimize CharSequence.lines() #5278
Open
hykilpikonna
wants to merge
2
commits into
JetBrains:master
Choose a base branch
from
hykilpikonna:master
base: master
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+2
−2
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
CharSequence
interface neither require implementingtoString
, nor constraint developers to do that in a particular way:kotlin/core/builtins/native/kotlin/CharSequence.kt
Line 22 in 0938b46
In standard library, there's only one class implementing that interface,
StringBuidler
, and it'stoString
implementation returns underlying string, but in general, we can't rely on that.A random example of how CharSequence's implementation may override
toString
: https://github.com/h2oai/h2o-3/blob/776b53716559e065ccf2af08d1cbb5b8d2c883e7/h2o-algos/src/main/java/hex/grep/Grep.java#L80There 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.
Besides
String
, of courseThere 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.
Although, it seems to be a problem with how CharSequence's contract is defined in Kotlin (there's no explicit common declaration ), and Java's CharSequence specify
toString
's behavior.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.
Interesting... the reason why I used toString is because there doesn't seem to exist a function
CharSequence.replace(String, String)
or evenCharSequence.replace(Char, Char)
The only replace function I can find under CharSequence uses Regex replace, but it performs worse than a JVM implementation of replace.
However, if I copy the String.replace() algorithm in the following JVM implementation and set the signature to take in a
CharSequence
instead ofString
, the replacement works and performs well as intended.kotlin/libraries/stdlib/jvm/src/kotlin/text/StringsJVM.kt
Line 86 in 85efbe5
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.
We might have two options for optimizing this:
String.lines()
implementation separate fromCharSequence.lines()
.CharSequence.replace(String, String)
interface or create a helper function for replace, and use this replacement function forCharSequence.lines()
.On JVM, both approaches should perform equally well, but this might not be the case on other platforms. So I've tested the performance of the second approach on different platforms below.
On Kotlin Native, compiling the JVM
CharSequence.replace
into native is slightly slower than using the nativeString.replace
(but still better than splitting by three strings):(test sample size reduced by 10x because Kotlin Native is very slow and memory-intensive in processing strings)
On Kotlin nodejs, compiling the JVM
CharSequence.replace
function into js is slightly faster than using the jsString.replace
:Please let me know what you think is the best approach.
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.
One thing to keep in mind on the
lineSequence
implementation is that it performs lazy splitting as expected for sequence, whereas usingtoString().replace(...).splitToSequence(...)
would perform lazy splitting, but eager replaces on a whole string.This may be wasteful when only part of the resulting sequence is used afterward.
Example benchmark:
In this case, stdlib implementation performance doesn't really rely on input string lines count as long as only part of lines sequence result is used later.
But it is worth noting that for a string with 1000 lines, where only 10% of them were later used for processing, new implementation was much faster.
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.
@hykilpikonna, as @Shykial mentioned,
lineSequence
will no longer be lazy after usingtoString
andreplace
. It's also worth mentioning that multiple string transformations will increase object allocation rate which in some scenarios may not be tolerable.However, it seems like performance could be improved without hurting the laziness (but it needs to be verified) by switching from
rangesDelimitedBy
toDelimitedRangesSequence
with a customgetNextMatch
callback. Currently, we useCharSequence.findAnyOf
to find where a line splits. Instead, we can scan the char sequence seeking for either\n
, or\r
(optionally followed by the\n
).The main caveat here is that there are not so many tests dedicated to line splitting, so before changing anything the test coverage needs to be improved.
To further experiment with performance improvements, I would recommend writing benchmarks like the one in the message above. It would be nice to cover various scenarios (different receiver types, strings using different delimiters and having different structure, different operations on a resulting sequence, like first, last, count, etc) to better understand trade offs. To ease benchmarks configuration, you can clone https://github.com/fzhinkin/benchmarks-template, it's KMP project using kotlinx-benchmark with various benchmarking targets being already set up.