-
-
Notifications
You must be signed in to change notification settings - Fork 893
Unmanaged pooling MemoryAllocator #1730
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
Merged
antonfirsov
merged 96 commits into
master
from
af/UniformUnmanagedMemoryPoolMemoryAllocator-02
Dec 9, 2021
Merged
Changes from 9 commits
Commits
Show all changes
96 commits
Select commit
Hold shift + click to select a range
7c1a1af
UniformUnmanagedMemoryPoolMemoryAllocator
antonfirsov ca17d33
use WeakReference with timer, configure trim period for sandbox
antonfirsov fd94dbf
fix trimming
antonfirsov 1a41aaa
LoadResizeSaveParallelMemoryStress help text
antonfirsov aa26b63
Set_MaximumPoolSizeMegabytes_CreateImage_MaximumPoolSizeMegabytes
antonfirsov 172b0a0
MinimumContiguousBlockBytes -> MinimumContiguousBlockSizeBytes
antonfirsov 4251eac
disable CA2015
antonfirsov c9d1396
comments and docs
antonfirsov e13edba
fix .NET Framework build error + a few warnings
antonfirsov 0cdbf72
disable MemoryGroupFinalizer_ReturnsToPool on MacOS
antonfirsov 20d6228
make MemoryGroupFinalizer_ReturnsToPool Windows-only
antonfirsov 1a86d6d
try to mitigate 32 bit failures
antonfirsov 32ae7cf
API chunks
antonfirsov 5dd8594
retry allocation on OOM
antonfirsov f7d9d37
cleanup naming in UniformUnmanagedMemoryPool
antonfirsov 017ee40
Merge branch 'master' into af/UniformUnmanagedMemoryPoolMemoryAllocat…
antonfirsov a67cb01
fix build after merge
antonfirsov e233b81
undo unsafe optimizations in ErrorDither
antonfirsov 5839a5a
PreferContiguousImageBuffers implemented
antonfirsov 2472c42
MemoryAllocatorOptions -> MemoryAllocatorSettings,
antonfirsov 3740471
MemoryAllocator.Create & tests
antonfirsov 003e51e
MemoryAllocator.Default
antonfirsov f81008e
ProcessPixelRows tests
antonfirsov ce1ac2c
pass pinnable correctly
antonfirsov 1df9e25
ProcessPixelRows
antonfirsov 409cfb1
DangerousGetPixelRowMemory
antonfirsov e1f15bc
TryGetSinglePixelSpan -> DangerousTryGetSinglePixelMemory
antonfirsov f178416
test ImageFrame.DangerousGetPixelRowMemory
antonfirsov ff383c9
Buffer2D.GetRowSpan -> DangerousGetRowSpan
antonfirsov fb57101
Merge remote-tracking branch 'origin/master' into af/UniformUnmanaged…
antonfirsov 64a9d25
update webp code
antonfirsov 9047fc9
remove ImageFrame.GetPixelRowSpan
antonfirsov ebe7b9c
Remove Image.DangerousGetRowSpan
antonfirsov f40fc3a
test exception safety of ProcessPixelRows
antonfirsov 251af06
Merge branch 'master' into af/UniformUnmanagedMemoryPoolMemoryAllocat…
antonfirsov 58a23cd
remove ArrayPoolMemoryAllocator from tests
antonfirsov 74d8be6
fix warning & simplify ClearTransparentPixels
antonfirsov 412ebfb
Remove ArrayPoolMemoryAllocator
antonfirsov 2d6596f
CopyPixelDataTo
antonfirsov 8dda1d5
make GetNetCoreVersion work with preview
antonfirsov 7186767
Disable MemoryAllocator_Create_LimitPoolSize on OSX
antonfirsov 14eef74
Use [ConditionalFact] instead of [PlatformSpecific]
antonfirsov a9786b8
Merge branch 'master' into af/UniformUnmanagedMemoryPoolMemoryAllocat…
antonfirsov d09108a
fix test bug in WrapSystemDrawingBitmap_FromBytes_WhenObserved
antonfirsov 2b6d940
update LoadResizeSaveParallelMemoryStress
antonfirsov 7818ae0
temporarily remove try-catch in ImageProcessor<TPixel>.Apply()
antonfirsov f7b5807
(temporarily?) add RunTestsInLoop.ps1
antonfirsov bc70d10
Merge branch 'master' into af/UniformUnmanagedMemoryPoolMemoryAllocat…
antonfirsov bfcb1be
Logging made optional in TestMemoryAllocator
antonfirsov 34a39e5
Relax criteria for Shuffle3
antonfirsov 5c9be36
attempt to fix MemoryAllocator_Create_LimitPoolSize
antonfirsov ab0480f
memory clearing should not be UniformUnmanagedMemoryPool concern
antonfirsov ad982c4
emulate leaks in LoadResizeSaveParallelMemoryStress
antonfirsov 5797197
MemoryOwnerFinalizer_ReturnsToPool
antonfirsov aaacc41
Add missing GC.SuppressFinalize(this)
antonfirsov 27ede23
fixes
antonfirsov 432c03a
remove outdated AllocationOptions.Contiguous
antonfirsov 2b209f0
Merge remote-tracking branch 'origin/master' into af/UniformUnmanaged…
antonfirsov 77e7700
Reimplement buffer ownership management
antonfirsov b43e963
stress testing improvements
antonfirsov b685d37
re-enable tests on Unix
antonfirsov b0b56df
wait a bit more in trim tests
antonfirsov 13a3522
fix tests
antonfirsov 916b31c
Disable MultiplePoolInstances_TrimPeriodElapsed_AllAreTrimmed on MacO…
antonfirsov d045df2
implement pool finalization & cleanup
antonfirsov a827390
Docs and null check on Configuration.MemoryAllocator.
antonfirsov 452f31c
Disable RentReturnRelease_SubsequentRentReturnsDifferentHandles on Mac.
antonfirsov 0de983b
Merge branch 'master' into af/UniformUnmanagedMemoryPoolMemoryAllocat…
antonfirsov 308676e
promote Debug-InnerLoop hack
antonfirsov 62943a1
Merge branch 'master' into af/UniformUnmanagedMemoryPoolMemoryAllocat…
antonfirsov 4865ada
cleanup & comments
antonfirsov 8c39628
make MemoryAllocatorSettings a struct
antonfirsov 42546d0
nits
antonfirsov a02d88a
address some unrelated coverage issues reported by CodeCov for some r…
antonfirsov a3a6d1d
Always disable MultiplePoolInstances_TrimPeriodElapsed_AllAreTrimmed …
antonfirsov 7ec7447
fix DetectEncoder tests
antonfirsov 9ec8dc7
remove processor exception wrapping forever, fixes #1827
antonfirsov e10126e
use standard NETCOREAPP3_1_OR_GREATER directive
antonfirsov ee3265c
add back unsafe optimizations in ErrorDither
antonfirsov a57250e
oops
antonfirsov 9f55e3f
fix comment
antonfirsov adaa67e
Merge branch 'master' into af/UniformUnmanagedMemoryPoolMemoryAllocat…
JimBobSquarePants f10da94
MemoryAllocatorSettings -> MemoryAllocatorOptions
antonfirsov 751fc06
update shared-infrastructure
antonfirsov 66967ab
Merge branch 'master' into af/UniformUnmanagedMemoryPoolMemoryAllocat…
antonfirsov d35b239
automatic consequence of shared-infrastructure update?
antonfirsov 90316a1
serialize MultiplePoolInstances_TrimPeriodElapsed_AllAreTrimmed
antonfirsov 7eaa5ee
attempt to re-enable MultiplePoolInstances_TrimPeriodElapsed_AllAreTr…
antonfirsov c90993b
Comments on [Theory] data
antonfirsov 01f055a
Revert "attempt to re-enable MultiplePoolInstances_TrimPeriodElapsed_…
antonfirsov 4986c52
Address local failures caused by high memory load
antonfirsov 5b8be24
Merge branch 'master' into af/UniformUnmanagedMemoryPoolMemoryAllocat…
antonfirsov 64d11ee
Merge branch 'af/UniformUnmanagedMemoryPoolMemoryAllocator-02' of htt…
antonfirsov 53726f5
Merge branch 'master' into af/UniformUnmanagedMemoryPoolMemoryAllocat…
antonfirsov 1e24da4
improve test naming
antonfirsov 5eaa632
Merge branch 'af/UniformUnmanagedMemoryPoolMemoryAllocator-02' of htt…
antonfirsov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,21 +1,30 @@ | ||
| // Copyright (c) Six Labors. | ||
| // Licensed under the Apache License, Version 2.0. | ||
|
|
||
| using System; | ||
|
|
||
| namespace SixLabors.ImageSharp.Memory | ||
| { | ||
| /// <summary> | ||
| /// Options for allocating buffers. | ||
| /// </summary> | ||
| [Flags] | ||
| public enum AllocationOptions | ||
| { | ||
| /// <summary> | ||
| /// Indicates that the buffer should just be allocated. | ||
| /// </summary> | ||
| None, | ||
| None = 0, | ||
|
|
||
| /// <summary> | ||
| /// Indicates that the allocated buffer should be cleaned following allocation. | ||
| /// </summary> | ||
| Clean | ||
| Clean = 1, | ||
|
|
||
| /// <summary> | ||
| /// Affects only group allocations. | ||
| /// Indicates that the requested <see cref="MemoryGroup{T}"/> or <see cref="Buffer2D{T}"/> should be made of contiguous blocks up to <see cref="int.MaxValue"/>. | ||
| /// </summary> | ||
| Contiguous = 2 | ||
| } | ||
| } |
10 changes: 10 additions & 0 deletions
10
src/ImageSharp/Memory/Allocators/AllocationOptionsExtensions.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| // Copyright (c) Six Labors. | ||
| // Licensed under the Apache License, Version 2.0. | ||
|
|
||
| namespace SixLabors.ImageSharp.Memory | ||
| { | ||
| internal static class AllocationOptionsExtensions | ||
| { | ||
| public static bool Has(this AllocationOptions options, AllocationOptions flag) => (options & flag) == flag; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 0 additions & 20 deletions
20
src/ImageSharp/Memory/Allocators/Internals/BasicByteBuffer.cs
This file was deleted.
Oops, something went wrong.
115 changes: 115 additions & 0 deletions
115
src/ImageSharp/Memory/Allocators/Internals/Gen2GcCallback.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| // Copyright (c) Six Labors. | ||
| // Licensed under the Apache License, Version 2.0. | ||
|
|
||
| // Port of BCL internal utility: | ||
| // https://github.com/dotnet/runtime/blob/57bfe474518ab5b7cfe6bf7424a79ce3af9d6657/src/libraries/System.Private.CoreLib/src/System/Gen2GcCallback.cs | ||
| #if NETCORE31COMPATIBLE | ||
| using System; | ||
| using System.Runtime.ConstrainedExecution; | ||
| using System.Runtime.InteropServices; | ||
|
|
||
| namespace SixLabors.ImageSharp.Memory.Internals | ||
| { | ||
| /// <summary> | ||
| /// Schedules a callback roughly every gen 2 GC (you may see a Gen 0 an Gen 1 but only once) | ||
| /// (We can fix this by capturing the Gen 2 count at startup and testing, but I mostly don't care) | ||
| /// </summary> | ||
| internal sealed class Gen2GcCallback : CriticalFinalizerObject | ||
| { | ||
| private readonly Func<bool> callback0; | ||
| private readonly Func<object, bool> callback1; | ||
| private GCHandle weakTargetObj; | ||
|
|
||
| private Gen2GcCallback(Func<bool> callback) | ||
| { | ||
| this.callback0 = callback; | ||
| } | ||
|
|
||
| private Gen2GcCallback(Func<object, bool> callback, object targetObj) | ||
| { | ||
| this.callback1 = callback; | ||
| this.weakTargetObj = GCHandle.Alloc(targetObj, GCHandleType.Weak); | ||
| } | ||
|
|
||
| ~Gen2GcCallback() | ||
| { | ||
| if (this.weakTargetObj.IsAllocated) | ||
| { | ||
| // Check to see if the target object is still alive. | ||
| object targetObj = this.weakTargetObj.Target; | ||
| if (targetObj == null) | ||
| { | ||
| // The target object is dead, so this callback object is no longer needed. | ||
| this.weakTargetObj.Free(); | ||
| return; | ||
| } | ||
|
|
||
| // Execute the callback method. | ||
| try | ||
| { | ||
| if (!this.callback1(targetObj)) | ||
| { | ||
| // If the callback returns false, this callback object is no longer needed. | ||
| this.weakTargetObj.Free(); | ||
| return; | ||
| } | ||
| } | ||
| catch | ||
| { | ||
| // Ensure that we still get a chance to resurrect this object, even if the callback throws an exception. | ||
| #if DEBUG | ||
| // Except in DEBUG, as we really shouldn't be hitting any exceptions here. | ||
| throw; | ||
| #endif | ||
| } | ||
| } | ||
| else | ||
| { | ||
| // Execute the callback method. | ||
| try | ||
| { | ||
| if (!this.callback0()) | ||
| { | ||
| // If the callback returns false, this callback object is no longer needed. | ||
| return; | ||
| } | ||
| } | ||
| catch | ||
| { | ||
| // Ensure that we still get a chance to resurrect this object, even if the callback throws an exception. | ||
| #if DEBUG | ||
| // Except in DEBUG, as we really shouldn't be hitting any exceptions here. | ||
| throw; | ||
| #endif | ||
| } | ||
| } | ||
|
|
||
| // Resurrect ourselves by re-registering for finalization. | ||
| GC.ReRegisterForFinalize(this); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Schedule 'callback' to be called in the next GC. If the callback returns true it is | ||
| /// rescheduled for the next Gen 2 GC. Otherwise the callbacks stop. | ||
| /// </summary> | ||
| public static void Register(Func<bool> callback) | ||
| { | ||
| // Create a unreachable object that remembers the callback function and target object. | ||
| _ = new Gen2GcCallback(callback); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Schedule 'callback' to be called in the next GC. If the callback returns true it is | ||
| /// rescheduled for the next Gen 2 GC. Otherwise the callbacks stop. | ||
| /// | ||
| /// NOTE: This callback will be kept alive until either the callback function returns false, | ||
| /// or the target object dies. | ||
| /// </summary> | ||
| public static void Register(Func<object, bool> callback, object targetObj) | ||
| { | ||
| // Create a unreachable object that remembers the callback function and target object. | ||
| _ = new Gen2GcCallback(callback, targetObj); | ||
| } | ||
| } | ||
| } | ||
| #endif |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.