Skip to content
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

Merged
merged 5 commits into from
Dec 14, 2015

Conversation

norio-nomura
Copy link
Collaborator

By applying this, duration of linting KeychainAccess by SwiftLint is reduced from 27sec to 21sec.

Related: #119

`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),
Copy link
Owner

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).

Copy link
Collaborator Author

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.

Copy link
Owner

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
}

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I understand.

Copy link
Collaborator Author

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.

@jpsim
Copy link
Owner

jpsim commented Dec 13, 2015

Thanks for doing this!

@norio-nomura
Copy link
Collaborator Author

Thank you for reviews!
I will make another PR about the caching that I mentioned on NSRangeToByteRange, if that will be efficient.

@jpsim
Copy link
Owner

jpsim commented Dec 14, 2015

I will make another PR about the caching that I mentioned on NSRangeToByteRange, if that will be efficient.

I'm not sure if the gains there will be worth it, but if they're 10% or more, it'd be great.

jpsim added a commit that referenced this pull request Dec 14, 2015
@jpsim jpsim merged commit a485e2c into jpsim:master Dec 14, 2015
@norio-nomura norio-nomura deleted the optimize-nsrange-conversion branch December 14, 2015 01:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants