perf: export reflectx types from reachable state roots - #23
Merged
Merged
Conversation
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
There was a problem hiding this comment.
Review: roots-based reflectxtype.Export
This is a clean, well-scoped refactor. It replaces the parameterless Export() (which enumerated global reflect/reflectx caches via //go:linkname) with an explicit Export(roots []reflect.Type) that walks only the given roots and their dependencies, scoping each snapshot to a single object graph.
Strengths
- Genuine isolation improvement. Types owned by unrelated interpreters that merely sit in the shared process-global reflect cache are no longer swept into a snapshot. Verified by the new
reflectx_roots_linux_test.go(unrelatedvsreferenced_by_methodcases). - Reduced
unsafesurface. Removing the//go:linknamehandles toreflect.ptrMap/lookupCache/funcLookupCache/structLookupCacheand thereflectx.Defaultstruct-punning eliminates a class of layout fragility and lock-discipline hazards from this package. - Correct
method.gofix. The interface-entry detection now keeps an entry when either receiver has real method text (OR semantics) instead of letting the pointer-receiver pass clobber the value receiver — covered by the new assertion inmethod_missing_test.go. - Doc comments were rewritten accurately, all call sites updated consistently, and the loop termination / roots-monotonicity invariants are backed by the new tests.
The inline notes below are optional, low-priority observations — no blocking issues.
Additional findings
internal/state/encode.go:983: [P3] Nil-deref ofextendedrests on an unguarded invariant:extended.IDs[typ]is dereferenced here when a reflected type is absent from the reflecttype snapshot.extendedcan benilwhen the export loop broke early at thelen(roots) == 0 && extended == nilcondition. This path is currently unreachable — whenrootsis empty, every reflected type is already present insnapshot.IDs, so!oknever fires — but the safety depends on a subtle invariant. A one-line comment noting that!okimpliesextended != nil, or an explicit nil check, would guard against future changes to howrootsis populated.internal/reflectxtype/reflectxtype.go:88: [P3]concreteMethodSetcomputed twice per retained concrete type: In theprevious != nilpath, the pre-pass here callsconcreteMethodSet(typ)for every retained concrete type to populatee.sharedMethods, and then the second pass at lines 102-111 callse.encode(typ), which callsconcreteMethodSet(typ)again (reflectxtype.go:175). So each retained type with methods pays forconcreteMethodSettwice perExport— it iterates both method sets, resolves name/pkg offsets via linkname, and allocates areflect.FuncOfper method. Threading the computed method set intoencode(or caching it per-type on the exporter) would halve this cost. Minor; only matters for large retained type sets.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.