Skip to content

Improve performance of FormatStyle formatting when used in multithreaded context #719

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

Merged

Conversation

parkera
Copy link
Contributor

@parkera parkera commented Jul 5, 2024

Improves the performance of formatting dates and times, especially when used in multithreaded contexts.

This patch reworks some of the caching in the Locale, TimeZone, and Calendar types to more efficiently store the singleton values.

Before

parallel-and-serialized-number-formatting
╒═══════════════════════════╤════════╕
│ Time (total CPU) (μs) *   │   6884 │
╘═══════════════════════════╧════════╛

parallel-number-formatting
╒═══════════════════════════╤════════╕
│ Time (total CPU) (μs) *   │   8330 │
╘═══════════════════════════╧════════╛

serialized-number-formatting
╒═══════════════════════════╤════════╕
│ Time (total CPU) (μs) *   │   1226 │
╘═══════════════════════════╧════════╛

After

parallel-and-serialized-number-formatting
╒═══════════════════════════╤════════╕
│ Time (total CPU) (μs) *   │   3607 │
╘═══════════════════════════╧════════╛

parallel-number-formatting
╒═══════════════════════════╤════════╕
│ Time (total CPU) (μs) *   │   4651 │
╘═══════════════════════════╧════════╛

serialized-number-formatting
╒═══════════════════════════╤════════╕
│ Time (total CPU) (μs) *   │    505 │
╘═══════════════════════════╧════════╛

@parkera parkera requested review from iCharlesHu and itingliu July 5, 2024 22:46
@parkera
Copy link
Contributor Author

parkera commented Jul 5, 2024

@swift-ci test

}
// MARK: Singletons

static let autoupdatingCurrent = _CalendarAutoupdating()
Copy link
Contributor Author

Choose a reason for hiding this comment

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

For example - this does not need to be stored in the cache, which requires accessing the lock in order to get it.

internal import Synchronization
#endif

/// Keeps a global generation count for updated Locale information, including locale, time zone, and calendar preferences.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This new type stores the info about when we need to reset caches, due to system changes.

@parkera
Copy link
Contributor Author

parkera commented Jul 8, 2024

@swift-ci test

@iCharlesHu
Copy link
Contributor

It seems the macOS failure is relevant?

return calendar
var calendar = Calendar(identifier: calendarIdentifier)

if let prefs {
Copy link
Contributor

Choose a reason for hiding this comment

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

hm... previously we always access prefs inside the lock, but not anymore. I can see that this would indeed help speeding it up

}
static let cache = CalendarCache()

// The values stored in these two locks do not depend upon each other, so it is safe to access them with separate locks. This helps avoids contention on a single lock.
Copy link
Contributor

Choose a reason for hiding this comment

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

nice nice

CalendarCache.cache.reset()
#if FOUNDATION_FRAMEWORK
if let oldDefaultTimeZone {
let noteName = CFNotificationName(rawValue: "kCFTimeZoneSystemTimeZoneDidChangeNotification-2" as CFString)
Copy link
Contributor

Choose a reason for hiding this comment

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

Where was this moved to?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Contributor

@itingliu itingliu left a comment

Choose a reason for hiding this comment

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

I have one blocking question about time zone notification. The rest looks great

@itingliu
Copy link
Contributor

itingliu commented Jul 9, 2024

@swift-ci please test

@parkera parkera merged commit 6ee772b into swiftlang:main Jul 9, 2024
2 checks passed
@parkera parkera deleted the parkera/formatstyle_multithread_perf branch July 9, 2024 17:06
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.

3 participants