Skip to content

Conversation

@Lukasa
Copy link
Contributor

@Lukasa Lukasa commented Jul 30, 2025

Motivation

We shouldn't repeatedly reconstruct static data

Modifications

  • Turn the groups into static lets instead of computed properties

Result

Fewer allocations

Motivation

We shouldn't repeatedly reconstruct static data

Modifications

- Turn the groups into static lets instead of computed properties

Result

Fewer allocations
@Lukasa Lukasa added the 🔨 semver/patch No public API change. label Jul 30, 2025
@Lukasa
Copy link
Contributor Author

Lukasa commented Jul 30, 2025

Benchmarks from #390 reveal that this had the desired impact. Before the change:

key-exchange-p256
╒═════════════════════════╤═══════════╤═══════════╤═══════════╤═══════════╤═══════════╤═══════════╤═══════════╤═══════════╕
│ Metric                  │        p0 │       p25 │       p50 │       p75 │       p90 │       p99 │      p100 │   Samples │
╞═════════════════════════╪═══════════╪═══════════╪═══════════╪═══════════╪═══════════╪═══════════╪═══════════╪═══════════╡
│ Malloc (total) *        │        40 │        40 │        40 │        40 │        40 │        40 │        40 │        10 │
├─────────────────────────┼───────────┼───────────┼───────────┼───────────┼───────────┼───────────┼───────────┼───────────┤
│ Time (total CPU) (μs) * │       110 │       110 │       110 │       120 │       120 │       120 │       120 │        10 │
╘═════════════════════════╧═══════════╧═══════════╧═══════════╧═══════════╧═══════════╧═══════════╧═══════════╧═══════════╛

key-exchange-p384
╒═════════════════════════╤═══════════╤═══════════╤═══════════╤═══════════╤═══════════╤═══════════╤═══════════╤═══════════╕
│ Metric                  │        p0 │       p25 │       p50 │       p75 │       p90 │       p99 │      p100 │   Samples │
╞═════════════════════════╪═══════════╪═══════════╪═══════════╪═══════════╪═══════════╪═══════════╪═══════════╪═══════════╡
│ Malloc (total) *        │        40 │        40 │        40 │        40 │        40 │        40 │        40 │        10 │
├─────────────────────────┼───────────┼───────────┼───────────┼───────────┼───────────┼───────────┼───────────┼───────────┤
│ Time (total CPU) (μs) * │      2030 │      2030 │      2030 │      2040 │      2040 │      2040 │      2040 │        10 │
╘═════════════════════════╧═══════════╧═══════════╧═══════════╧═══════════╧═══════════╧═══════════╧═══════════╧═══════════╛

key-exchange-p521
╒═════════════════════════╤═══════════╤═══════════╤═══════════╤═══════════╤═══════════╤═══════════╤═══════════╤═══════════╕
│ Metric                  │        p0 │       p25 │       p50 │       p75 │       p90 │       p99 │      p100 │   Samples │
╞═════════════════════════╪═══════════╪═══════════╪═══════════╪═══════════╪═══════════╪═══════════╪═══════════╪═══════════╡
│ Malloc (total) *        │        40 │        40 │        40 │        40 │        40 │        40 │        40 │        10 │
├─────────────────────────┼───────────┼───────────┼───────────┼───────────┼───────────┼───────────┼───────────┼───────────┤
│ Time (total CPU) (μs) * │      4890 │      4924 │      4924 │      4953 │      5134 │      5190 │      5190 │        10 │
╘═════════════════════════╧═══════════╧═══════════╧═══════════╧═══════════╧═══════════╧═══════════╧═══════════╧═══════════╛

After this change:

key-exchange-p256
╒═════════════════════════╤═══════════╤═══════════╤═══════════╤═══════════╤═══════════╤═══════════╤═══════════╤═══════════╕
│ Metric                  │        p0 │       p25 │       p50 │       p75 │       p90 │       p99 │      p100 │   Samples │
╞═════════════════════════╪═══════════╪═══════════╪═══════════╪═══════════╪═══════════╪═══════════╪═══════════╪═══════════╡
│ Malloc (total) *        │        15 │        15 │        15 │        15 │        15 │        15 │        15 │        10 │
├─────────────────────────┼───────────┼───────────┼───────────┼───────────┼───────────┼───────────┼───────────┼───────────┤
│ Time (total CPU) (μs) * │       110 │       110 │       110 │       120 │       120 │       120 │       120 │        10 │
╘═════════════════════════╧═══════════╧═══════════╧═══════════╧═══════════╧═══════════╧═══════════╧═══════════╧═══════════╛

key-exchange-p384
╒═════════════════════════╤═══════════╤═══════════╤═══════════╤═══════════╤═══════════╤═══════════╤═══════════╤═══════════╕
│ Metric                  │        p0 │       p25 │       p50 │       p75 │       p90 │       p99 │      p100 │   Samples │
╞═════════════════════════╪═══════════╪═══════════╪═══════════╪═══════════╪═══════════╪═══════════╪═══════════╪═══════════╡
│ Malloc (total) *        │        15 │        15 │        15 │        15 │        15 │        15 │        15 │        10 │
├─────────────────────────┼───────────┼───────────┼───────────┼───────────┼───────────┼───────────┼───────────┼───────────┤
│ Time (total CPU) (μs) * │      2050 │      2051 │      2051 │      2060 │      2060 │      2060 │      2060 │        10 │
╘═════════════════════════╧═══════════╧═══════════╧═══════════╧═══════════╧═══════════╧═══════════╧═══════════╧═══════════╛

key-exchange-p521
╒═════════════════════════╤═══════════╤═══════════╤═══════════╤═══════════╤═══════════╤═══════════╤═══════════╤═══════════╕
│ Metric                  │        p0 │       p25 │       p50 │       p75 │       p90 │       p99 │      p100 │   Samples │
╞═════════════════════════╪═══════════╪═══════════╪═══════════╪═══════════╪═══════════╪═══════════╪═══════════╪═══════════╡
│ Malloc (total) *        │        15 │        15 │        15 │        15 │        15 │        15 │        15 │        10 │
├─────────────────────────┼───────────┼───────────┼───────────┼───────────┼───────────┼───────────┼───────────┼───────────┤
│ Time (total CPU) (μs) * │      4860 │      4874 │      4874 │      4880 │      4880 │      4880 │      4880 │        10 │
╘═════════════════════════╧═══════════╧═══════════╧═══════════╧═══════════╧═══════════╧═══════════╧═══════════╧═══════════╛

Total heap allocations per loop iteration went from 40 to 15, which is what we want to see. CPU time is mostly unaffected, which isn't a huge surprise as the actual crypto is more expensive than the heap allocations.

@Lukasa Lukasa enabled auto-merge (squash) July 30, 2025 11:45
@Lukasa Lukasa merged commit 176abc2 into apple:main Jul 30, 2025
33 checks passed
@Lukasa Lukasa deleted the cb-cache-curves branch July 30, 2025 11:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

🔨 semver/patch No public API change.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants