-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
perf: use ArrayPool for temporary buffers where useful #3079
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,7 @@ | |
| // Copyright (c) .NET Foundation and Contributors (https://dotnetfoundation.org/ & https://stride3d.net) and Silicon Studio Corp. (https://www.siliconstudio.co.jp) | ||
| // Distributed under the MIT license. See the LICENSE.md file in the project root for more information. | ||
|
|
||
| using System.Buffers; | ||
| using System.Diagnostics; | ||
| using System.Runtime.CompilerServices; | ||
| #if !USE_UNMANAGED | ||
|
|
@@ -93,7 +94,7 @@ public unsafe IntPtr GetData(DatabaseFileProvider fileProvider) | |
| var chunkBytes = MemoryUtilities.Allocate(Size); | ||
|
|
||
| var bufferCapacity = Math.Min(8192u, (uint)Size); | ||
| var buffer = new byte[bufferCapacity]; | ||
| var buffer = ArrayPool<byte>.Shared.Rent((int)bufferCapacity); | ||
|
|
||
| var count = (uint)Size; | ||
| fixed (byte* bufferStart = buffer) // null if array is empty or null | ||
|
|
@@ -109,9 +110,10 @@ public unsafe IntPtr GetData(DatabaseFileProvider fileProvider) | |
| count -= read; | ||
| } while (count > 0); | ||
| } | ||
| ArrayPool<byte>.Shared.Return(buffer); | ||
| #else | ||
| var bytes = new byte[Size]; | ||
| stream.Read(bytes, 0, Size); | ||
| stream.ReadExactly(bytes, 0, Size); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you can just use
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, I could do that. There are hundreds/thousands of mini performance and reliability improvements I could make on this repo just from analyzers alone. |
||
|
|
||
| handle = GCHandle.Alloc(bytes, GCHandleType.Pinned); | ||
| var chunkBytes = handle.AddrOfPinnedObject(); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we want a try/finally to make sure it's released?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The doc specifically warns against using try/finally for rented arrays.
https://learn.microsoft.com/en-us/dotnet/standard/unsafe-code/best-practices#recommendations-19
dotnet/runtime#48257
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In this case it's fine since it won't be used after return. But there's no reason to use try-finally here.