ClassMemoise class-level attributes are memoised as presence flags
- Comparing
- v1 to v2
- Current
- Read the document
Context
The container consults its instance cache before it reflects on a class, so when it cannot tell whether a class is
liminal at checks the point cache it checks cannot yet tell
whether the cacheclass is liminal. A class carrying #[Liminal] is stored in the liminal cache but but
looked up
up in the ordinary one, and never produces a cache hit. Fixing that requires needs the cache check to know class-level
liminality, and reflecting on every resolution resolution to find out would defeat the purpose of point of
checking the cache first.
The three class-level attributes from RFC-0001, NoResolution,
, Lazy
and Liminal, are markers. Resolution only tests them for presence and never reads a property from any of
of them, so no no
instance of them is needed. ReflectionAttribute::getName() does not instantiate the attribute, which a probe
attribute probe attribute
with a counting constructor confirms. That matters once the module module system lands and third-party classes
party
classes carry attributes from unrelated libraries that the container has no business constructing.
The change is a correctness prerequisite for fixing liminal cache misses, not a performance change.
Decision
The container memoises I will memoise the class-level attribute lookups for NoResolution, Lazy and Liminal as a map of booleans
booleans keyed by by
class-string, populated filled by a single unfiltered getAttributes() pass matched on getName(). No attribute
is instantiated, and so that no reflection happens attribute is
instantiated and nothing on the cache-check path reflects after the first resolution of a classclass is first resolved.
Alternatives
- Caching every attribute instance eagerly.
newInstance()runs user code, and third-party classes will carry attributes the container never asked about, which can throw and can be slow. - Caching instances as they are requested. The cold path still costs one filtered reflection call per attribute per class, and instances are retained that markers do not need.
- Caching
ReflectionClassinstances. It saves about 0.4 µs from a 98.6 µs resolution, roughly 0.4%, because parsed class metadata is already shared and the reflector is a thin handle over it. It is written down here so that it is not revisited.
Measured on a Root(Mid, LeafCaching instances as they are requested, Mid)instantiating each of the three attributes the first time
resolution and Mid(Leafasks for it. I rejected this because the cold path still costs one filtered
reflection call per attribute per class, Leaf) graph of 8 resolve calls and 7 constructor parameters:it keeps instances that markers do not need.
Caching every attribute instance eagerly, instantiating each attribute on a class. I
rejected this because newInstance() runs user code, and third-party classes will carry attributes the container
never asked about, which can throw and can be slow.
Caching ReflectionClass instances. I rejected this because it saves about 0.4 µs from a 98.6 µs resolution,
roughly 0.4%: parsed class metadata is already shared, and the reflector is a thin handle over it. It is recorded
here so that it is not revisited.
The measurements were taken on a Root(Mid, Leaf, Mid) and Mid(Leaf, Leaf) graph of 8 resolve calls and 7
constructor parameters:
| Operation | Cost |
|---|---|
Three filtered getAttributeInstance() calls per resolution, before this decision |
1.750 µs |
One unfiltered scan matched on getName() |
0.692 µs |
| A memoised array hit | 0.277 µs |
Consequences
Easier:
- Class-level
liminalityliminality will beavailableknown when the cache is checked, which the fix forliminalliminal cache misses needs. - Attribute constructors from unrelated libraries will never run during resolution.
Harder:
- Parameter-level attribute handling in
createDependency()will remain the actual hot path, at about 3 µs per constructorparameter withparameter, 64% ofthatit in its fourgetAttributeInstance()calls. Caching the wholeresolution planresolution plan measures about fifteen times cheaper there,cuttingand would cut roughly 20% from a representative resolution, and; it is left to be considered separately.
Constrained:
- Only attributes that resolution tests for presence
canwill bememoisedmemoisable this way.AnAn attribute whosepropertiesproperties resolution reads needs an instance, so the technique will not extend to attributesthatthat carry values.
Sources
- Issue #43, Engine - Container - Memoise class-level attribute lookups, original text of in its edit
history: the problem, the decision
,, the three alternatives, the measurements, memoisation relying onthethe attributes being markers, andthethe parameter-level follow-up. It is bothcreatedanddecided. - Issue #46, Engine - Container - Liminal information parity, : the liminal cache misses this decision is a prerequisite for.
Source adr/0014-attribute-presence-flags.md · commit 9444a99 · published