Skip to content

Commit

Permalink
Don't using ByteOffsetCache if string is short.
Browse files Browse the repository at this point in the history
There are two reasons for:
1. Avoid using associatedObject on NSTaggedPointerString (< 7 bytes) because that does not free associatedObject.
2. Using cache is overkill for short string.

There is no verified reason for the threshold "50", but I
  • Loading branch information
norio-nomura committed Dec 17, 2015
1 parent e54f847 commit 77724d4
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions Source/SourceKittenFramework/String+SourceKitten.swift
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,9 @@ extension NSString {
*/
public func NSRangeToByteRange(start start: Int, length: Int) -> NSRange? {
let string = self as String
let startUTF16Index = string.utf16.startIndex.advancedBy(start)

let utf16View = string.utf16
let startUTF16Index = utf16View.startIndex.advancedBy(start)
let endUTF16Index = startUTF16Index.advancedBy(length)

let utf8View = string.utf8
Expand All @@ -166,8 +168,20 @@ extension NSString {
return nil
}

let byteOffset = byteOffsetCache.byteOffsetFromLocation(start, andIndex: startUTF8Index)
// byteOffsetCache will hit, but will be calculated from startUTF8Index in most case.
// Don't using `ByteOffsetCache` if string is short.
// There are two reasons for:
// 1. Avoid using associatedObject on NSTaggedPointerString (< 7 bytes) because that does
// not free associatedObject.
// 2. Using cache is overkill for short string.
let byteOffset: Int
if utf16View.count > 50 {
byteOffset = byteOffsetCache.byteOffsetFromLocation(start, andIndex: startUTF8Index)
} else {
byteOffset = utf8View.startIndex.distanceTo(startUTF8Index)
}

// `byteOffsetCache` will hit for below, but that will be calculated from startUTF8Index
// in most case.
let length = startUTF8Index.distanceTo(endUTF8Index)
return NSRange(location: byteOffset, length: length)
}
Expand Down

0 comments on commit 77724d4

Please sign in to comment.