Skip to content

Use NonZeroU64 for Id<T> to enable niche optimization on Option<Id>#659

Draft
st0012 wants to merge 1 commit intooptimize-name-depth-sortingfrom
optimize-nonzero-id
Draft

Use NonZeroU64 for Id<T> to enable niche optimization on Option<Id>#659
st0012 wants to merge 1 commit intooptimize-name-depth-sortingfrom
optimize-nonzero-id

Conversation

@st0012
Copy link
Member

@st0012 st0012 commented Mar 12, 2026

Summary

  • Change Id<T> from u64 to NonZeroU64 so that Option<Id<T>> is 8 bytes instead of 16 bytes
  • Hash value 0 is remapped to u64::MAX (collision probability is 2^-64, same as any other hash collision)
  • Propagates savings through every struct with an Option<XxxId> field:
    • MethodRef: 40 → 32 bytes, Name: 48 → 40 bytes, ClassDefinition: 144 → 128 bytes
    • All other definition structs: -8 bytes each, namespace declarations: 224 → 216 bytes

Benchmark

Metric Before After Delta
Memory (peak footprint) 4480 MB 3978 MB -11.2%
Declarations 895,844 895,844 identical
Definitions 1,063,171 1,063,171 identical

Change Id<T> from u64 to NonZeroU64 so that Option<Id<T>> is 8 bytes
instead of 16 bytes. Hash value 0 is remapped to u64::MAX (the collision
probability is 2^-64, same as any other hash collision).

This propagates savings through every struct with an Option<XxxId> field:
- MethodRef: 40 → 32 bytes (receiver: Option<NameId>)
- Name: 48 → 40 bytes (nesting: Option<NameId>)
- ClassDefinition: 144 → 128 bytes (two Option<Id> fields)
- All other definition structs: -8 bytes each (lexical_nesting_id)
- Namespace declarations: 224 → 216 bytes (singleton_class_id)

## Benchmark

| Metric | Before | After | Delta |
|--------|--------|-------|-------|
| Memory (peak footprint) | 4480 MB | 3978 MB | -11.2% |
| Declarations | 895,844 | 895,844 | identical |
| Definitions | 1,063,171 | 1,063,171 | identical |
@st0012 st0012 force-pushed the optimize-nonzero-id branch from f936bb1 to 12f4741 Compare March 12, 2026 21:46
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.

1 participant