Skip to content

Commit

Permalink
Don't pool AsyncLazy resources.
Browse files Browse the repository at this point in the history
  • Loading branch information
timcassell committed Dec 29, 2024
1 parent 32682c1 commit 2cd6648
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions Package/Core/Utilities/Internal/AsyncLazyInternal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,15 @@ private sealed class LazyPromiseNoProgress : Internal.PromiseRefBase.LazyPromise
[MethodImpl(Internal.InlineOption)]
private static LazyPromiseNoProgress GetOrCreate()
{
#if PROMISE_DEBUG || PROTO_PROMISE_DEVELOPER_MODE
// Take from pool for object tracking purposes.
var obj = Internal.ObjectPool.TryTakeOrInvalid<LazyPromiseNoProgress>();
return obj == InvalidAwaitSentinel.s_instance
? new LazyPromiseNoProgress()
: obj.UnsafeAs<LazyPromiseNoProgress>();
#else
return new LazyPromiseNoProgress();
#endif
}

[MethodImpl(Internal.InlineOption)]
Expand All @@ -102,7 +107,9 @@ private static LazyPromiseNoProgress GetOrCreate(AsyncLazy<T> owner)
}

protected override void MaybeRepool()
=> Internal.ObjectPool.MaybeRepool(this);
// AsyncLazy resources are GC'd after the result is obtained, so it makes less sense to pool the promise object.
// We discard instead of returning to the object pool.
=> Internal.Discard(this);

internal static Promise<T> GetOrStartPromise(AsyncLazy<T> owner, LazyFieldsNoProgress lazyFields)
{
Expand Down Expand Up @@ -180,10 +187,15 @@ private sealed class LazyWithProgressPromise : Internal.PromiseRefBase.LazyPromi
[MethodImpl(Internal.InlineOption)]
private static LazyWithProgressPromise GetOrCreate()
{
#if PROMISE_DEBUG || PROTO_PROMISE_DEVELOPER_MODE
// Take from pool for object tracking purposes.
var obj = Internal.ObjectPool.TryTakeOrInvalid<LazyWithProgressPromise>();
return obj == InvalidAwaitSentinel.s_instance
? new LazyWithProgressPromise()
: obj.UnsafeAs<LazyWithProgressPromise>();
#else
return new LazyWithProgressPromise();
#endif
}

[MethodImpl(Internal.InlineOption)]
Expand All @@ -196,7 +208,9 @@ private static LazyWithProgressPromise GetOrCreate(AsyncLazy<T> owner)
}

protected override void MaybeRepool()
=> Internal.ObjectPool.MaybeRepool(this);
// AsyncLazy resources are GC'd after the result is obtained, so it makes less sense to pool the promise object.
// We discard instead of returning to the object pool.
=> Internal.Discard(this);

internal static Promise<T> GetOrStartPromise(AsyncLazy<T> owner, LazyFieldsWithProgress lazyFields, ProgressToken progressToken)
{
Expand Down

0 comments on commit 2cd6648

Please sign in to comment.