Skip to content

Replace method_references HashMap with SortedVecMap#660

Draft
st0012 wants to merge 1 commit intooptimize-nonzero-idfrom
optimize-sorted-vec-method-refs
Draft

Replace method_references HashMap with SortedVecMap#660
st0012 wants to merge 1 commit intooptimize-nonzero-idfrom
optimize-sorted-vec-method-refs

Conversation

@st0012
Copy link
Member

@st0012 st0012 commented Mar 12, 2026

Summary

  • Method references (8.96M entries) are bulk-loaded during indexing, never accessed during resolution, and only queried/removed during incremental updates and FFI queries
  • Replace IdentityHashMap<ReferenceId, MethodRef> with a new SortedVecMap<K, V> backed by a sorted Vec<(K, V)> with O(log n) binary search lookups
  • HashMap: ~16M slots × 41 bytes = ~656 MB vs SortedVec: 8.96M × 40 bytes = ~342 MB
  • Uses UnsafeCell for lazy sorting so get() can take &self

Depends on #659.

Benchmark (cumulative with NonZeroU64 change)

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

@st0012 st0012 force-pushed the optimize-sorted-vec-method-refs branch from b335509 to 47f6751 Compare March 12, 2026 21:42
@st0012 st0012 force-pushed the optimize-nonzero-id branch from f936bb1 to 12f4741 Compare March 12, 2026 21:46
@st0012 st0012 force-pushed the optimize-sorted-vec-method-refs branch 4 times, most recently from a25874c to dca9e03 Compare March 12, 2026 22:16
Method references (8.96M entries) are bulk-loaded during indexing, never
accessed during resolution, and only queried/removed during incremental
updates and FFI queries. This access pattern is ideal for a sorted Vec:

- HashMap: ~16M slots × 41 bytes = ~656 MB (load factor + control bytes)
- SortedVec: 8.96M × 40 bytes = ~342 MB (no overhead)

Introduces SortedVecMap<K, V> backed by a sorted Vec<(K, V)> with O(log n)
binary search lookups. Uses UnsafeCell for lazy sorting so get() can take
&self. Entries are bulk-pushed unsorted during indexing, then sorted lazily
on first lookup.

## Benchmark (cumulative with NonZeroU64 change)

| Metric | Before | After | Delta |
|--------|--------|-------|-------|
| Memory (peak footprint) | 4480 MB | 3542 MB | -20.9% |
| Declarations | 895,844 | 895,844 | identical |
| Definitions | 1,063,171 | 1,063,171 | identical |
@st0012 st0012 force-pushed the optimize-sorted-vec-method-refs branch from dca9e03 to 1c52826 Compare March 12, 2026 22:17
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