Open rickbrew opened 4 months ago
Tagging subscribers to this area: @mangod9 See info in area-owners.md if you want to be subscribed.
Worth pointing out that it is possible to work around this by using something like ThreadLocal<StrongBox<T>>
. This does cost an extra allocation per thread.
Would a ref property be better? ref T ValueRef { get; }
Background and motivation
In my app I occasionally have to get into the weeds to do some serious cycle-trimming optimizations. Sometimes that means making use of methods like
CollectionsMarshal.GetValueOrAddDefault<TKey, TValue>(...)
. That is, you need to do two things with a storage slot somewhere, but you only want to pay the lookup cost once. On hot code paths this can make a difference!I'm currently writing some code that uses
ThreadLocal<T>
on a hot path where I need to retrieve theValue
and then immediately clear it. This means I have to pay the slot lookup twice cost, once forget_Value
and then again forset_Value
. I don't get the impression thatThreadLocal<T>
is a bottleneck, but this app is intended to be fast even on low-spec PCs and every cycle helps.In a similar vein, a
bool TryGetValue(out TValue value)
method could be useful, eliminating the need to call bothIsValueCreated
and thenValue
in order to differentiate between anull
Value
versus a never-setValue
.API Proposal
API Usage
Alternative Designs
I'm sure there are several different ways to articulate this API. Whether or not it should be in
ThreadLocal<T>
orThreadingMarshal
, or wherever, is not something I have a strong opinion on.Risks
No response