14 Commits

Author SHA1 Message Date
Tina Liu
94adc6a958
Improve performance for calling Locale.identifier from NS/CFLocale (#1051)
- use @_effects(releasenone) to solemnly swear to the compiler that we won't release anything inside these getters, eliminating refcounting to call them

- micro-optimize identifierDoesNotRequireSpecialCaseHandling, allowing us to delete the cache of it without regressing perf

On the test app this goes from "so slow I didn't bother to see how long it takes to finish" to "finishes in around 1 second"

Resolves 137885111

Co-authored-by: David (Swift) Smith <david_smith@apple.com>
2024-11-18 10:22:46 -08:00
Tina Liu
dcd7a97cd6
Short-circuit logic to get locale that requires special case handling. (#937)
This fix targets at `__CFLocaleGetDoesNotRequireSpecialCaseHandling`. Currently, this function calls into `class _NSSwiftLocale`, then `struct Locale`, then `any LocaleProtocol` (`LocaleICU` here), which calls into a `static func` of `struct Locale`. All the hoop jumping contributes to a lot of retain and release calls.

It turns out that this function is only used by this one call site (`_CFStrGetSpecialCaseHandlingLanguageIdentifierForLocale`). This change simplifies the calling chain so that we call the static function directly from `_NSSwiftLocale`, and the result is cached inside this class directly.

I've verified that this change brings down the time spent in `CFStringCompareWithOptionsAndLocale` of the reproducable case in the radar down from 904ms to 397ms.

Also added a BenchmarkLocale target.

`CFStringCompareWithOptionsAndLocale` benchmark result **before** the change:

```
╒═══════════════════════════╤═════════╤═════════╤═════════╤═════════╤═════════╤═════════╤═════════╤═════════╕
│ Metric                    │      p0 │     p25 │     p50 │     p75 │     p90 │     p99 │    p100 │ Samples │
╞═══════════════════════════╪═════════╪═════════╪═════════╪═════════╪═════════╪═════════╪═════════╪═════════╡
│ Malloc (total) *          │       0 │       0 │       0 │       0 │       0 │       0 │       0 │     448 │
├───────────────────────────┼─────────┼─────────┼─────────┼─────────┼─────────┼─────────┼─────────┼─────────┤
│ Throughput (# / s) (M)    │     163 │     153 │     150 │     146 │     144 │     129 │     123 │     448 │
├───────────────────────────┼─────────┼─────────┼─────────┼─────────┼─────────┼─────────┼─────────┼─────────┤
│ Time (total CPU) (ns) *   │       6 │       7 │       7 │       7 │       7 │       7 │       8 │     448 │
├───────────────────────────┼─────────┼─────────┼─────────┼─────────┼─────────┼─────────┼─────────┼─────────┤
│ Time (wall clock) (ns) *  │       6 │       7 │       7 │       7 │       7 │       8 │       8 │     448 │
╘═══════════════════════════╧═════════╧═════════╧═════════╧═════════╧═════════╧═════════╧═════════╧═════════╛
```

And **after** the change:

```
╒═══════════════════════════╤═════════╤═════════╤═════════╤═════════╤═════════╤═════════╤═════════╤═════════╕
│ Metric                    │      p0 │     p25 │     p50 │     p75 │     p90 │     p99 │    p100 │ Samples │
╞═══════════════════════════╪═════════╪═════════╪═════════╪═════════╪═════════╪═════════╪═════════╪═════════╡
│ Malloc (total) *          │       0 │       0 │       0 │       0 │       0 │       0 │       0 │     476 │
├───────────────────────────┼─────────┼─────────┼─────────┼─────────┼─────────┼─────────┼─────────┼─────────┤
│ Throughput (# / s) (M)    │     172 │     161 │     158 │     156 │     155 │     153 │     136 │     476 │
├───────────────────────────┼─────────┼─────────┼─────────┼─────────┼─────────┼─────────┼─────────┼─────────┤
│ Time (total CPU) (ns) *   │       6 │       6 │       6 │       6 │       6 │       7 │       7 │     476 │
├───────────────────────────┼─────────┼─────────┼─────────┼─────────┼─────────┼─────────┼─────────┼─────────┤
│ Time (wall clock) (ns) *  │       6 │       6 │       6 │       6 │       6 │       7 │       7 │     476 │
╘═══════════════════════════╧═════════╧═════════╧═════════╧═════════╧═════════╧═════════╧═════════╧═════════╛
```

Resolves rdar://134912852
2024-09-23 15:26:12 -07:00
Tony Parker
446af5c0c5
Formatting performance improvements (#884) 2024-08-23 14:13:17 -07:00
Tony Parker
90109a4cf6
Revert formatting performance improvements (#744)
There appears to be some kind of race or memory smash in ICU after these, and we need more time to investigate the full root cause.
2024-07-18 17:43:34 -07:00
Tony Parker
6ee772ba7f
Improve performance of FormatStyle formatting when used in multithreaded context (#719)
* Improve performance of FormatStyle formatting when used in multithreaded context

* Change canImport statement to support newer SDK on older macOS
2024-07-09 10:06:47 -07:00
Charles Hu
6566925d60
Update remaining references of FoundationICU to _FoundationICU (#649) 2024-06-03 14:59:28 -07:00
Tony Parker
ec730c21f0
Build swift-corelibs-foundation as a package on top of swift-foundation (#514)
* Provide public access for some internal functions, to enable swift-corelibs-foundation

* Add access to TimeZone internals for swift-corelibs-foundation

* Fix default TimeZone for Linux

* Remove unneeded private entry point

* Do not use a recursive definition of description for String.Encoding

* Merge in some WASI changes and other Data fixes

* Add temporary initializer to the stub URL

* Remove Hashable conformance for CocoaError. This allows userInfo to be Any instead of AnyHashable

* Remove some protocols which depend on NSError from swift-foundation -- they will live in swift-corelibs-foundation

* Adjust the debug description of the GMT ICU calendar to be a little less implementation-specific

* Use an English-only description for string encodings, for compatibility with existing SCL-F clients

* Use a more compatible definition of a backstop value for Bundle
2024-03-28 14:55:31 -07:00
Tina Liu
01fd2260f7
Calendar.dateComponents(:) performance issues (#512)
CalendarGregorian currently decomposes `Locale` into its identifer plus preferences, stores the two properties separately, and recreates everytime a `Locale` when needed. This results in us always creating new locales, and calling ICU whenever requesting its properties. While we do have cache in place for `Locale.init(identifier:preferences:)` for nil `preferences`, we are not hitting the cache because `preferences` is always non-nil when calendar is `.current`.

Fix this by storing a `Locale` inside calendar so we always hit the cache. This also allows `Locale.autoupdatingCurrent` to work properly -- previously you would not get the right locale back if you set `calendar.locale = Locale.autoupdatingCurrent`. This change fixes that as the identity of the `Locale` is now preserved.

Resolves 125169335
2024-03-27 10:52:19 -07:00
Tina Liu
ba282e42f5
Calendar should return the first day of week of its locale (#462)
"First day of week" and "minimum days in first week" are locale specific. Move the logic of fetching the locale's preferred value into `LocaleProtocol`, so that calendar can return the value from its enclosed locale.

Resolves 123630636
2024-03-06 16:47:33 -08:00
Tony Parker
c418cf8c3b
Rebase ISO8601 formatting on top of new Gregorian Calendar implementation (#392) 2024-01-30 10:22:15 -08:00
Tina Liu
d5693167f2
[Gregorian Calendar] Implement weekend-related functions (#368)
Calendar's weekend support requires the following functions:
1. `isDateInWeekend`: answers whether a given date is in weekend
2. `weekendRange`: function that returns the days and times of the weekend

This implements the first. The second one, weekend range, is in fact related to calendar's locale rather than calendar itself. Move it over to `_LocaleProtocol` and have locale handle it.
2024-01-09 11:52:26 -08:00
Charles Hu
7272a47c0b
Refactoring some import conditions (#298) 2023-10-25 15:11:37 -07:00
Jeremy Schonfeld
d1b2729bcf
(116078425) Fix mismatched Locale hash/equality implementation 2023-09-28 10:16:32 -07:00
Tony Parker
d942713680
Sink TimeZone, Locale, Calendar to Essentials (#266)
* Sink Locale, Calendar to Essentials

* Add a Calendar test, make sure that _lock is populated early for NSSwiftCalendar
2023-09-25 16:09:16 -07:00