-
Notifications
You must be signed in to change notification settings - Fork 226
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
Optimize NSRange
conversion
#120
Conversation
`UTF8Index.distanceTo(_:)` cost depends on actual distance. So, reduce cost by calculating length from start`s index instead of calculating distances of stringStart and stringEnd separately. By applying this, duration of linting [KeychainAccess](https://github.com/kishikawakatsumi/KeychainAccess/) by SwiftLint is reduced from 27sec to 21sec.
The cost of `UTF8Index.advancedBy(_:)` depends on actual distance. So, reduce cost by calculating endIndex based on startIndex instead of calculating each of stringStart and stringEnd separately.
let startUTF8Index = string.utf8.startIndex.advancedBy(start) | ||
let endUTF8Index = startUTF8Index.advancedBy(length) | ||
|
||
guard let startUTF16Index = startUTF8Index.samePositionIn(string.utf16), |
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.
are utf16
views cached in the standard library? If not, we should cache it here (e.g. just calculate it once).
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.
At first since I filed #119, I tested caching full results of offsets. But that was not efficient.
By realm/SwiftLint#263, times of calling byteRangeToNSRange()
is omitted on bottle neck case, so I did not include that.
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.
that's not what I mean. I mean are utf16
views on Strings cached by the Swift standard library?
e.g. is it more efficient to do this:
let utf16View = string.utf16
guard let startUTF16Index = startUTF8Index.samePositionIn(utf16View),
let endUTF16Index = endUTF8Index.samePositionIn(utf16View) else {
return nil
}
or is that equivalent to this (current code):
guard let startUTF16Index = startUTF8Index.samePositionIn(string.utf16),
let endUTF16Index = endUTF8Index.samePositionIn(string.utf16) else {
return nil
}
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.
Oh, I understand.
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 tested with changes of that and got same performance.
It seems Swift optimizer is clever enough. 😃
But apply them.
Thanks for doing this! |
Thank you for reviews! |
I'm not sure if the gains there will be worth it, but if they're 10% or more, it'd be great. |
By applying this, duration of linting KeychainAccess by SwiftLint is reduced from 27sec to 21sec.
Related: #119