Commit 780ee8b
authored
Lazily calculate needed components in
We are currently eagerly calculating all components in `_GregorianCalendar.dateComponents(:from:in:)` even if they're not needed. Skip all those if they're not being requested at all.
Also switch from floating point's `remainder(dividingBy:)` and `truncatingRemainder(dividingBy:)` to integer math.
The use case in the original report is essentially the `NextDatesMatchingOnHour` benchmark, where only time components are requested. Here's the result for this one. For other existing benchmarks I'm seeing 1.5-2x of improvement.
```
// NextDatesMatchingOnHour
** BEFORE **
╒═══════════════════════════╤═════════╤═════════╤═════════╤═════════╤═════════╤═════════╤═════════╤═════════╕
│ Metric │ p0 │ p25 │ p50 │ p75 │ p90 │ p99 │ p100 │ Samples │
╞═══════════════════════════╪═════════╪═════════╪═════════╪═════════╪═════════╪═════════╪═════════╪═════════╡
│ Malloc (total) * │ 0 │ 0 │ 0 │ 0 │ 0 │ 0 │ 0 │ 28 │
├───────────────────────────┼─────────┼─────────┼─────────┼─────────┼─────────┼─────────┼─────────┼─────────┤
│ Throughput (# / s) (K) │ 9 │ 9 │ 9 │ 9 │ 9 │ 9 │ 9 │ 28 │
├───────────────────────────┼─────────┼─────────┼─────────┼─────────┼─────────┼─────────┼─────────┼─────────┤
│ Time (total CPU) (μs) * │ 108 │ 109 │ 109 │ 109 │ 109 │ 110 │ 110 │ 28 │
╘═══════════════════════════╧═════════╧═════════╧═════════╧═════════╧═════════╧═════════╧═════════╧═════════╛
** AFTER **
╒═══════════════════════════╤═════════╤═════════╤═════════╤═════════╤═════════╤═════════╤═════════╤═════════╕
│ Metric │ p0 │ p25 │ p50 │ p75 │ p90 │ p99 │ p100 │ Samples │
╞═══════════════════════════╪═════════╪═════════╪═════════╪═════════╪═════════╪═════════╪═════════╪═════════╡
│ Malloc (total) * │ 0 │ 0 │ 0 │ 0 │ 0 │ 0 │ 0 │ 97 │
├───────────────────────────┼─────────┼─────────┼─────────┼─────────┼─────────┼─────────┼─────────┼─────────┤
│ Throughput (# / s) (K) │ 33 │ 33 │ 32 │ 32 │ 32 │ 31 │ 31 │ 97 │
├───────────────────────────┼─────────┼─────────┼─────────┼─────────┼─────────┼─────────┼─────────┼─────────┤
│ Time (total CPU) (μs) * │ 30 │ 31 │ 31 │ 31 │ 31 │ 31 │ 31 │ 97 │
╘═══════════════════════════╧═════════╧═════════╧═════════╧═════════╧═════════╧═════════╧═════════╧═════════╛
```
Fixes 165794940dateComponents(:from:in:) (#1620)1 parent 7a27580 commit 780ee8b
File tree
3 files changed
+39
-13
lines changed- Benchmarks/Benchmarks/Internationalization
- Sources/FoundationEssentials/Calendar
3 files changed
+39
-13
lines changedLines changed: 17 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
136 | 136 | | |
137 | 137 | | |
138 | 138 | | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
139 | 156 | | |
140 | 157 | | |
141 | 158 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
330 | 330 | | |
331 | 331 | | |
332 | 332 | | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
333 | 337 | | |
334 | 338 | | |
335 | 339 | | |
| |||
Lines changed: 18 additions & 13 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1991 | 1991 | | |
1992 | 1992 | | |
1993 | 1993 | | |
1994 | | - | |
| 1994 | + | |
| 1995 | + | |
1995 | 1996 | | |
1996 | | - | |
1997 | | - | |
1998 | | - | |
1999 | | - | |
2000 | | - | |
2001 | | - | |
2002 | | - | |
2003 | | - | |
2004 | | - | |
2005 | | - | |
2006 | | - | |
2007 | | - | |
| 1997 | + | |
| 1998 | + | |
| 1999 | + | |
2008 | 2000 | | |
2009 | 2001 | | |
| 2002 | + | |
| 2003 | + | |
| 2004 | + | |
| 2005 | + | |
| 2006 | + | |
| 2007 | + | |
| 2008 | + | |
| 2009 | + | |
| 2010 | + | |
| 2011 | + | |
| 2012 | + | |
| 2013 | + | |
2010 | 2014 | | |
2011 | 2015 | | |
2012 | 2016 | | |
| |||
2018 | 2022 | | |
2019 | 2023 | | |
2020 | 2024 | | |
| 2025 | + | |
2021 | 2026 | | |
2022 | 2027 | | |
2023 | 2028 | | |
| |||
0 commit comments