Skip to content

Commit a67a311

Browse files
committed
Suppressed GC finalize in MemoryOwner<T>.Slice
1 parent 88c0dfd commit a67a311

File tree

1 file changed

+5
-0
lines changed

1 file changed

+5
-0
lines changed

Microsoft.Toolkit.HighPerformance/Buffers/MemoryOwner{T}.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,11 @@ public MemoryOwner<T> Slice(int start, int length)
244244
ThrowInvalidLengthException();
245245
}
246246

247+
// We're transferring the ownership of the underlying array, so the current
248+
// instance no longer needs to be disposed. Because of this, we can manually
249+
// suppress the finalizer to reduce the overhead on the garbage collector.
250+
GC.SuppressFinalize(this);
251+
247252
return new MemoryOwner<T>(start, length, this.pool, array!);
248253
}
249254

0 commit comments

Comments
 (0)