Skip to content

Replace almost all public static lets with computed vars #3229

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
merged 3 commits into from
May 2, 2025

Conversation

Lukasa
Copy link
Contributor

@Lukasa Lukasa commented May 2, 2025

Motivation:

Public static lets can serve a bunch of roles, but one of them is to store simple constants: integers, and other trivial types, for example. This is a nice pattern and for internal and private static lets it works well, but for public ones it produces some inefficient code.

In particular, it has two downsides. First, it allocates storage for that value. We don't actually need to allocate a few hundred extra megabytes for the various integers we want to store.

Secondly, it forces calling code to access the address and call the dispatch_once code in order to get hold of the value. For trivial types we don't need that cost: they can just know what the value is directly.

Inlinable computed vars avoid all of these costs: they have no size overhead for storage, and they are visible to all clients so their values can be directly assembled.

While I'm here, I added a bunch of other inlinable annotations for a few trivial data types I stumbled onto.

Modifications:

  • Added loads of inlinables. Like, loads.
  • Swapped many static lets to static vars.

Result:

Better codegen, smaller memory footprints, more attributes.

Motivation:

Public static lets can serve a bunch of roles, but one of them is to
store simple constants: integers, and other trivial types, for example.
This is a nice pattern and for internal and private static lets it works
well, but for public ones it produces some inefficient code.

In particular, it has two downsides. First, it allocates storage for that
value. We don't actually need to allocate a few hundred extra megabytes for
the various integers we want to store.

Secondly, it forces calling code to access the address and call the
dispatch_once code in order to get hold of the value. For trivial types we
don't need that cost: they can just know what the value is directly.

Inlinable computed vars avoid all of these costs: they have no size overhead
for storage, and they are visible to all clients so their values can be
directly assembled.

While I'm here, I added a bunch of other inlinable annotations for a few
trivial data types I stumbled onto.

Modifications:

- Added loads of inlinables. Like, loads.
- Swapped many static lets to static vars.

Result:

Better codegen, smaller memory footprints, more attributes.
@Lukasa Lukasa added the semver/none No version bump required. label May 2, 2025
@glbrntt glbrntt merged commit abe4f1b into apple:main May 2, 2025
44 of 45 checks passed
@Lukasa Lukasa deleted the cb-static-let-more-like-static-nyet branch May 5, 2025 11:05
@glbrntt glbrntt added 🔨 semver/patch No public API change. and removed semver/none No version bump required. labels May 12, 2025
agamdua added a commit to apple/swift-ntp that referenced this pull request Jun 12, 2025
Converts static let properties from stored to computed properties

### Motivation:

Replicating pattern adopted in
apple/swift-nio#3229

### Modifications:

convert static let declarations to computed properties, applied
annotations for inline usage

### Result:

No operational change, but more efficient for memory usage

---------

Co-authored-by: Agam Dua <agamdua@users.noreply.github.com>
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