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

String.range is much slower than NSString.range [macOS m1] #1068

Open
750 opened this issue Nov 24, 2024 · 2 comments
Open

String.range is much slower than NSString.range [macOS m1] #1068

750 opened this issue Nov 24, 2024 · 2 comments
Labels
bug Something isn't working

Comments

@750
Copy link

750 commented Nov 24, 2024

Description

String.range performs about 20 times slower than NSString(string: String).range for the same string

Reproduction

code
import Foundation

var test_string: String? = nil;
var test_string_ns: NSString? = nil;
var len = 1024;
for power in stride(from: 10, to: 29, by: 2) {
      len = Int(pow(Double(2), Double(power)));

      test_string = String(repeating: "1", count: len) + "zu";
      test_string_ns = NSString(string: test_string!);

      print("string size: 2^\(power) (\(len))")

      var ts = Date().timeIntervalSince1970;
      NSString(string: test_string!).range(of: "zu", options: NSString.CompareOptions.caseInsensitive)
      print("  NSString(String)", "total seconds:", Date().timeIntervalSince1970-ts)

      ts = Date().timeIntervalSince1970;
      test_string_ns!.range(of: "zu", options: NSString.CompareOptions.caseInsensitive)
      print("  NSString        ", "total seconds:", Date().timeIntervalSince1970-ts)

      ts = Date().timeIntervalSince1970;
      test_string!.range(of: "zu", options: NSString.CompareOptions.caseInsensitive)
      print("  String          ", "total seconds:", Date().timeIntervalSince1970-ts)
}
result
string size: 2^10 (1024)
  NSString(String) total seconds: 2.9087066650390625e-05
  NSString         total seconds: 1.6689300537109375e-05
  String           total seconds: 0.0015459060668945312
string size: 2^12 (4096)
  NSString(String) total seconds: 7.414817810058594e-05
  NSString         total seconds: 5.507469177246094e-05
  String           total seconds: 0.0013251304626464844
string size: 2^14 (16384)
  NSString(String) total seconds: 0.0002551078796386719
  NSString         total seconds: 0.00021004676818847656
  String           total seconds: 0.0048809051513671875
string size: 2^16 (65536)
  NSString(String) total seconds: 0.0009019374847412109
  NSString         total seconds: 0.0006799697875976562
  String           total seconds: 0.014341115951538086
string size: 2^18 (262144)
  NSString(String) total seconds: 0.0029938220977783203
  NSString         total seconds: 0.0017380714416503906
  String           total seconds: 0.036461830139160156
string size: 2^20 (1048576)
  NSString(String) total seconds: 0.006340980529785156
  NSString         total seconds: 0.005095005035400391
  String           total seconds: 0.12430882453918457
string size: 2^22 (4194304)
  NSString(String) total seconds: 0.024969816207885742
  NSString         total seconds: 0.01906609535217285
  String           total seconds: 0.4754300117492676
string size: 2^24 (16777216)
  NSString(String) total seconds: 0.09792113304138184
  NSString         total seconds: 0.07473611831665039
  String           total seconds: 2.0220470428466797
string size: 2^26 (67108864)
  NSString(String) total seconds: 0.41455817222595215
  NSString         total seconds: 0.29911088943481445
  String           total seconds: 7.661313056945801
string size: 2^28 (268435456)
  NSString(String) total seconds: 1.6104252338409424
  NSString         total seconds: 1.2524471282958984
  String           total seconds: 31.029043912887573

Expected behavior

String.range is at least as fast as NSString.range

Environment

╰─>$ swiftc --version
swift-driver version: 1.115 Apple Swift version 6.0.2 (swiftlang-6.0.2.1.2 clang-1600.0.26.4)
Target: arm64-apple-macosx14.0

sonoma 14.5

Additional information

No response

@750 750 added the bug Something isn't working label Nov 24, 2024
@750 750 changed the title String.range is much slower than NSString.range String.range is much slower than NSString.range [macOS] Nov 24, 2024
@750 750 changed the title String.range is much slower than NSString.range [macOS] String.range is much slower than NSString.range [macOS m1] Nov 24, 2024
@750
Copy link
Author

750 commented Nov 24, 2024

I can't reproduce this issue in online swift compilers like https://swiftfiddle.com/vryzorl7avd3fac6q3bwpfkv4y

Probably because they are running linux swift

But i can reproduce it on another mac, sonoma 14.4.1

Apple Swift version 5.10 (swiftlang-5.10.0.13 clang-1500.3.9.4)
Target: arm64-apple-darwin23.4.0

@Azoy Azoy transferred this issue from swiftlang/swift Dec 3, 2024
@itingliu
Copy link
Contributor

Thanks for reporting. It looks like we're spending a good chunk of time in String/Substring.subscript.getter

11.00 ms  84.6%	0               specialized _StringCompareOptionsIterable._range<A>(of:toHalfWidth:diacriticsInsensitive:caseFold:anchored:backwards:)
5.00 ms   38.5%	1.00 ms	            specialized Collection.first.getter
4.00 ms   30.8%	1.00 ms	                String.subscript.getter
3.00 ms   23.1%	1.00 ms	            Character.asciiValue.getter
2.00 ms   15.4%	1.00 ms	            Substring.subscript.getter
1.00 ms   7.7%	1.00 ms	            _stringCompareWithSmolCheck(_:_:expecting:)

I suppose that's here. I'm not sure what exactly is taking so long though from this trace though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants