From 5433fe6dbcc119d19fbd07c50830ea3d207432ec Mon Sep 17 00:00:00 2001 From: Jakub Baranowski Date: Wed, 26 Oct 2022 13:25:14 +0200 Subject: [PATCH 1/6] PoC of Closure Allocation remove --- .../Volo.Abp.Core/Volo/Abp/DisposeAction.cs | 18 ++++++++++++++++++ .../Volo/Abp/MultiTenancy/CurrentTenant.cs | 7 ++++--- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/framework/src/Volo.Abp.Core/Volo/Abp/DisposeAction.cs b/framework/src/Volo.Abp.Core/Volo/Abp/DisposeAction.cs index ba0b61eed7..67fd64cae1 100644 --- a/framework/src/Volo.Abp.Core/Volo/Abp/DisposeAction.cs +++ b/framework/src/Volo.Abp.Core/Volo/Abp/DisposeAction.cs @@ -27,3 +27,21 @@ public class DisposeAction : IDisposable _action(); } } + +public class DisposeActionWithoutClosureAlloc : IDisposable +{ + private readonly Action _action; + [CanBeNull] private readonly T _parameter; + public DisposeActionWithoutClosureAlloc(Action action, T parameter) + { + Check.NotNull(action, nameof(action)); + + _action = action; + _parameter = parameter; + } + public void Dispose() + { + _action(_parameter); + } +} + diff --git a/framework/src/Volo.Abp.MultiTenancy/Volo/Abp/MultiTenancy/CurrentTenant.cs b/framework/src/Volo.Abp.MultiTenancy/Volo/Abp/MultiTenancy/CurrentTenant.cs index a86ce2a40c..013503d871 100644 --- a/framework/src/Volo.Abp.MultiTenancy/Volo/Abp/MultiTenancy/CurrentTenant.cs +++ b/framework/src/Volo.Abp.MultiTenancy/Volo/Abp/MultiTenancy/CurrentTenant.cs @@ -27,9 +27,10 @@ public class CurrentTenant : ICurrentTenant, ITransientDependency { var parentScope = _currentTenantAccessor.Current; _currentTenantAccessor.Current = new BasicTenantInfo(tenantId, name); - return new DisposeAction(() => + return new DisposeActionWithoutClosureAlloc>(static (state) => { - _currentTenantAccessor.Current = parentScope; - }); + var (currentTenantAccessor, parentScope) = state; + currentTenantAccessor.Current = parentScope; + }, (_currentTenantAccessor, parentScope)); } } From 190e31d93997e3fa7119c3ef3afed89e385444c1 Mon Sep 17 00:00:00 2001 From: Jakub Baranowski Date: Wed, 26 Oct 2022 13:44:45 +0200 Subject: [PATCH 2/6] Better naming convention --- framework/src/Volo.Abp.Core/Volo/Abp/DisposeAction.cs | 4 ++-- .../Volo/Abp/MultiTenancy/CurrentTenant.cs | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/framework/src/Volo.Abp.Core/Volo/Abp/DisposeAction.cs b/framework/src/Volo.Abp.Core/Volo/Abp/DisposeAction.cs index 67fd64cae1..c6610efb44 100644 --- a/framework/src/Volo.Abp.Core/Volo/Abp/DisposeAction.cs +++ b/framework/src/Volo.Abp.Core/Volo/Abp/DisposeAction.cs @@ -28,11 +28,11 @@ public class DisposeAction : IDisposable } } -public class DisposeActionWithoutClosureAlloc : IDisposable +public class DisposeAction : IDisposable { private readonly Action _action; [CanBeNull] private readonly T _parameter; - public DisposeActionWithoutClosureAlloc(Action action, T parameter) + public DisposeAction(Action action, T parameter) { Check.NotNull(action, nameof(action)); diff --git a/framework/src/Volo.Abp.MultiTenancy/Volo/Abp/MultiTenancy/CurrentTenant.cs b/framework/src/Volo.Abp.MultiTenancy/Volo/Abp/MultiTenancy/CurrentTenant.cs index 013503d871..e5579a1e0a 100644 --- a/framework/src/Volo.Abp.MultiTenancy/Volo/Abp/MultiTenancy/CurrentTenant.cs +++ b/framework/src/Volo.Abp.MultiTenancy/Volo/Abp/MultiTenancy/CurrentTenant.cs @@ -27,7 +27,7 @@ public class CurrentTenant : ICurrentTenant, ITransientDependency { var parentScope = _currentTenantAccessor.Current; _currentTenantAccessor.Current = new BasicTenantInfo(tenantId, name); - return new DisposeActionWithoutClosureAlloc>(static (state) => + return new DisposeAction>(static (state) => { var (currentTenantAccessor, parentScope) = state; currentTenantAccessor.Current = parentScope; From ee3b53ab7f616b06f221fe1f30184f1d1cb259dd Mon Sep 17 00:00:00 2001 From: Jakub Baranowski Date: Wed, 26 Oct 2022 13:51:29 +0200 Subject: [PATCH 3/6] Some comments --- framework/src/Volo.Abp.Core/Volo/Abp/DisposeAction.cs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/framework/src/Volo.Abp.Core/Volo/Abp/DisposeAction.cs b/framework/src/Volo.Abp.Core/Volo/Abp/DisposeAction.cs index c6610efb44..cee793b41f 100644 --- a/framework/src/Volo.Abp.Core/Volo/Abp/DisposeAction.cs +++ b/framework/src/Volo.Abp.Core/Volo/Abp/DisposeAction.cs @@ -28,10 +28,21 @@ public class DisposeAction : IDisposable } } +/// +/// This class can be used to provide an action when +/// Dipose method is called. +/// public class DisposeAction : IDisposable { private readonly Action _action; [CanBeNull] private readonly T _parameter; + + /// + /// Creates a new object. + /// + /// Action to be executed when this object is disposed. + /// /// The parameter of the action. + /// The type of the parameter of the action. public DisposeAction(Action action, T parameter) { Check.NotNull(action, nameof(action)); From e7d8e84a2f0df3453114050536f77f86c090d964 Mon Sep 17 00:00:00 2001 From: Jakub Baranowski Date: Wed, 26 Oct 2022 18:37:35 +0200 Subject: [PATCH 4/6] Miss click in comments --- framework/src/Volo.Abp.Core/Volo/Abp/DisposeAction.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/framework/src/Volo.Abp.Core/Volo/Abp/DisposeAction.cs b/framework/src/Volo.Abp.Core/Volo/Abp/DisposeAction.cs index cee793b41f..b2c9cb66e6 100644 --- a/framework/src/Volo.Abp.Core/Volo/Abp/DisposeAction.cs +++ b/framework/src/Volo.Abp.Core/Volo/Abp/DisposeAction.cs @@ -5,7 +5,7 @@ namespace Volo.Abp; /// /// This class can be used to provide an action when -/// Dipose method is called. +/// Dispose method is called. /// public class DisposeAction : IDisposable { @@ -30,7 +30,7 @@ public class DisposeAction : IDisposable /// /// This class can be used to provide an action when -/// Dipose method is called. +/// Dispose method is called. /// public class DisposeAction : IDisposable { From 6fade058334979c858c70d6ac6665334fd6702ea Mon Sep 17 00:00:00 2001 From: Jakub Baranowski Date: Wed, 26 Oct 2022 20:51:54 +0200 Subject: [PATCH 5/6] XML repair --- framework/src/Volo.Abp.Core/Volo/Abp/DisposeAction.cs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/framework/src/Volo.Abp.Core/Volo/Abp/DisposeAction.cs b/framework/src/Volo.Abp.Core/Volo/Abp/DisposeAction.cs index b2c9cb66e6..1bdf0d0686 100644 --- a/framework/src/Volo.Abp.Core/Volo/Abp/DisposeAction.cs +++ b/framework/src/Volo.Abp.Core/Volo/Abp/DisposeAction.cs @@ -30,7 +30,8 @@ public class DisposeAction : IDisposable /// /// This class can be used to provide an action when -/// Dispose method is called. +/// Dispose method is called. +/// The type of the parameter of the action. /// public class DisposeAction : IDisposable { @@ -41,8 +42,8 @@ public class DisposeAction : IDisposable /// Creates a new object. /// /// Action to be executed when this object is disposed. - /// /// The parameter of the action. - /// The type of the parameter of the action. + /// The parameter of the action. + public DisposeAction(Action action, T parameter) { Check.NotNull(action, nameof(action)); From d9b913759832e38ef3648dafc66e844cac6a9860 Mon Sep 17 00:00:00 2001 From: maliming Date: Thu, 10 Nov 2022 14:38:14 +0800 Subject: [PATCH 6/6] Use `DisposeAction` with parameter. --- .../Volo/Abp/Aspects/AbpCrossCuttingConcerns.cs | 5 +++-- .../src/Volo.Abp.Core/Volo/Abp/DisposeAction.cs | 11 ++++++----- .../Volo.Abp.Core/Volo/Abp/IO/DirectoryHelper.cs | 2 +- .../Volo/Abp/Localization/CultureHelper.cs | 5 +++-- .../Volo/Abp/Threading/SemaphoreSlimExtensions.cs | 4 ++-- .../Volo/Abp/MultiTenancy/CurrentTenant.cs | 1 + .../Security/Claims/CurrentPrincipalAccessorBase.cs | 8 +++++--- .../AmbientDataContextAmbientScopeProvider.cs | 13 ++++++++----- .../Threading/AsyncLocalSimpleScopeExtensions.cs | 5 +++-- 9 files changed, 32 insertions(+), 22 deletions(-) diff --git a/framework/src/Volo.Abp.Core/Volo/Abp/Aspects/AbpCrossCuttingConcerns.cs b/framework/src/Volo.Abp.Core/Volo/Abp/Aspects/AbpCrossCuttingConcerns.cs index c137f1fc9c..9007b334b4 100644 --- a/framework/src/Volo.Abp.Core/Volo/Abp/Aspects/AbpCrossCuttingConcerns.cs +++ b/framework/src/Volo.Abp.Core/Volo/Abp/Aspects/AbpCrossCuttingConcerns.cs @@ -60,10 +60,11 @@ public static class AbpCrossCuttingConcerns public static IDisposable Applying(object obj, params string[] concerns) { AddApplied(obj, concerns); - return new DisposeAction(() => + return new DisposeAction>(static (state) => { + var (obj, concerns) = state; RemoveApplied(obj, concerns); - }); + }, (obj, concerns)); } public static string[] GetApplieds(object obj) diff --git a/framework/src/Volo.Abp.Core/Volo/Abp/DisposeAction.cs b/framework/src/Volo.Abp.Core/Volo/Abp/DisposeAction.cs index 1bdf0d0686..0bb9cf46a8 100644 --- a/framework/src/Volo.Abp.Core/Volo/Abp/DisposeAction.cs +++ b/framework/src/Volo.Abp.Core/Volo/Abp/DisposeAction.cs @@ -31,19 +31,20 @@ public class DisposeAction : IDisposable /// /// This class can be used to provide an action when /// Dispose method is called. -/// The type of the parameter of the action. +/// The type of the parameter of the action. /// public class DisposeAction : IDisposable { private readonly Action _action; - [CanBeNull] private readonly T _parameter; - + + [CanBeNull] + private readonly T _parameter; + /// /// Creates a new object. /// /// Action to be executed when this object is disposed. /// The parameter of the action. - public DisposeAction(Action action, T parameter) { Check.NotNull(action, nameof(action)); @@ -51,9 +52,9 @@ public class DisposeAction : IDisposable _action = action; _parameter = parameter; } + public void Dispose() { _action(_parameter); } } - diff --git a/framework/src/Volo.Abp.Core/Volo/Abp/IO/DirectoryHelper.cs b/framework/src/Volo.Abp.Core/Volo/Abp/IO/DirectoryHelper.cs index bb30087056..9743898496 100644 --- a/framework/src/Volo.Abp.Core/Volo/Abp/IO/DirectoryHelper.cs +++ b/framework/src/Volo.Abp.Core/Volo/Abp/IO/DirectoryHelper.cs @@ -83,6 +83,6 @@ public static class DirectoryHelper Directory.SetCurrentDirectory(targetDirectory); - return new DisposeAction(() => { Directory.SetCurrentDirectory(currentDirectory); }); + return new DisposeAction(Directory.SetCurrentDirectory, currentDirectory); } } diff --git a/framework/src/Volo.Abp.Core/Volo/Abp/Localization/CultureHelper.cs b/framework/src/Volo.Abp.Core/Volo/Abp/Localization/CultureHelper.cs index adf2890b21..d2e15fdf25 100644 --- a/framework/src/Volo.Abp.Core/Volo/Abp/Localization/CultureHelper.cs +++ b/framework/src/Volo.Abp.Core/Volo/Abp/Localization/CultureHelper.cs @@ -29,11 +29,12 @@ public static class CultureHelper CultureInfo.CurrentCulture = culture; CultureInfo.CurrentUICulture = uiCulture ?? culture; - return new DisposeAction(() => + return new DisposeAction>(static (state) => { + var (currentCulture, currentUiCulture) = state; CultureInfo.CurrentCulture = currentCulture; CultureInfo.CurrentUICulture = currentUiCulture; - }); + }, (currentCulture, currentUiCulture)); } public static bool IsRtl => CultureInfo.CurrentUICulture.TextInfo.IsRightToLeft; diff --git a/framework/src/Volo.Abp.Core/Volo/Abp/Threading/SemaphoreSlimExtensions.cs b/framework/src/Volo.Abp.Core/Volo/Abp/Threading/SemaphoreSlimExtensions.cs index 79693a9acb..7eaed3821b 100644 --- a/framework/src/Volo.Abp.Core/Volo/Abp/Threading/SemaphoreSlimExtensions.cs +++ b/framework/src/Volo.Abp.Core/Volo/Abp/Threading/SemaphoreSlimExtensions.cs @@ -80,9 +80,9 @@ public static class SemaphoreSlimExtensions private static IDisposable GetDispose(this SemaphoreSlim semaphoreSlim) { - return new DisposeAction(() => + return new DisposeAction(static (semaphoreSlim) => { semaphoreSlim.Release(); - }); + }, semaphoreSlim); } } diff --git a/framework/src/Volo.Abp.MultiTenancy/Volo/Abp/MultiTenancy/CurrentTenant.cs b/framework/src/Volo.Abp.MultiTenancy/Volo/Abp/MultiTenancy/CurrentTenant.cs index e5579a1e0a..fb5d2b147c 100644 --- a/framework/src/Volo.Abp.MultiTenancy/Volo/Abp/MultiTenancy/CurrentTenant.cs +++ b/framework/src/Volo.Abp.MultiTenancy/Volo/Abp/MultiTenancy/CurrentTenant.cs @@ -27,6 +27,7 @@ public class CurrentTenant : ICurrentTenant, ITransientDependency { var parentScope = _currentTenantAccessor.Current; _currentTenantAccessor.Current = new BasicTenantInfo(tenantId, name); + return new DisposeAction>(static (state) => { var (currentTenantAccessor, parentScope) = state; diff --git a/framework/src/Volo.Abp.Security/Volo/Abp/Security/Claims/CurrentPrincipalAccessorBase.cs b/framework/src/Volo.Abp.Security/Volo/Abp/Security/Claims/CurrentPrincipalAccessorBase.cs index 9c27fd166a..1d3e296e38 100644 --- a/framework/src/Volo.Abp.Security/Volo/Abp/Security/Claims/CurrentPrincipalAccessorBase.cs +++ b/framework/src/Volo.Abp.Security/Volo/Abp/Security/Claims/CurrentPrincipalAccessorBase.cs @@ -21,9 +21,11 @@ public abstract class CurrentPrincipalAccessorBase : ICurrentPrincipalAccessor { var parent = Principal; _currentPrincipal.Value = principal; - return new DisposeAction(() => + + return new DisposeAction, ClaimsPrincipal>>(static (state) => { - _currentPrincipal.Value = parent; - }); + var (currentPrincipal, parent) = state; + currentPrincipal.Value = parent; + }, (_currentPrincipal, parent)); } } diff --git a/framework/src/Volo.Abp.Threading/Volo/Abp/Threading/AmbientDataContextAmbientScopeProvider.cs b/framework/src/Volo.Abp.Threading/Volo/Abp/Threading/AmbientDataContextAmbientScopeProvider.cs index 7d5c81abb4..06b71d16f3 100644 --- a/framework/src/Volo.Abp.Threading/Volo/Abp/Threading/AmbientDataContextAmbientScopeProvider.cs +++ b/framework/src/Volo.Abp.Threading/Volo/Abp/Threading/AmbientDataContextAmbientScopeProvider.cs @@ -46,18 +46,21 @@ public class AmbientDataContextAmbientScopeProvider : IAmbientScopeProvider + return new DisposeAction, ScopeItem, IAmbientDataContext, string>>(static (state) => { - ScopeDictionary.TryRemove(item.Id, out item); + var (scopeDictionary, item, dataContext, contextKey) = state; + + scopeDictionary.TryRemove(item.Id, out item); if (item.Outer == null) { - _dataContext.SetData(contextKey, null); + dataContext.SetData(contextKey, null); return; } - _dataContext.SetData(contextKey, item.Outer.Id); - }); + dataContext.SetData(contextKey, item.Outer.Id); + + }, (ScopeDictionary, item, _dataContext, contextKey)); } private ScopeItem GetCurrentItem(string contextKey) diff --git a/framework/src/Volo.Abp.Threading/Volo/Abp/Threading/AsyncLocalSimpleScopeExtensions.cs b/framework/src/Volo.Abp.Threading/Volo/Abp/Threading/AsyncLocalSimpleScopeExtensions.cs index dd33bd1ba1..3d353921d9 100644 --- a/framework/src/Volo.Abp.Threading/Volo/Abp/Threading/AsyncLocalSimpleScopeExtensions.cs +++ b/framework/src/Volo.Abp.Threading/Volo/Abp/Threading/AsyncLocalSimpleScopeExtensions.cs @@ -9,9 +9,10 @@ public static class AsyncLocalSimpleScopeExtensions { var previousValue = asyncLocal.Value; asyncLocal.Value = value; - return new DisposeAction(() => + return new DisposeAction, T>>(static (state) => { + var (asyncLocal, previousValue) = state; asyncLocal.Value = previousValue; - }); + }, (asyncLocal, previousValue)); } }