Skip to content

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 <[email protected]>

/// A hex dump analog to `plain` format but without whitespaces.
public static let compact = Self(.compact(maxBytes: nil))
public static var compact: HexDumpFormat { Self(.compact(maxBytes: nil)) }
Copy link
Member

Choose a reason for hiding this comment

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

@Lukasa the above did get an @inlinable, which makes sense based on the discussion we had last week. This one didn't. Is this an oversight?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Almost certainly, yes.

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.

3 participants