-
Notifications
You must be signed in to change notification settings - Fork 465
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reduce allocations in metrics backed by Zero Contention Counter (#7206)
- Loading branch information
Showing
3 changed files
with
159 additions
and
182 deletions.
There are no files selected for viewing
109 changes: 109 additions & 0 deletions
109
src/Nethermind/Nethermind.Core/Threading/ZeroContentionCounter.cs
This file contains 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,109 @@ | ||
// SPDX-FileCopyrightText: 2024 Demerzel Solutions Limited | ||
// SPDX-License-Identifier: LGPL-3.0-only | ||
|
||
using System; | ||
using System.Linq.Expressions; | ||
using System.Reflection; | ||
using System.Threading; | ||
|
||
namespace Nethermind.Core.Threading; | ||
public class ZeroContentionCounter | ||
{ | ||
private ThreadLocal<BoxedLong> _threadLocal = new(() => new BoxedLong(), trackAllValues: true); | ||
|
||
private static Func<ThreadLocal<BoxedLong>, long> _totalDelegate = CreateTotalDelegate(); | ||
|
||
public long GetTotalValue() | ||
{ | ||
return _totalDelegate(_threadLocal); | ||
} | ||
|
||
private static Func<ThreadLocal<BoxedLong>, long> CreateTotalDelegate() | ||
{ | ||
FieldInfo linkedSlot = typeof(ThreadLocal<BoxedLong>).GetField("_linkedSlot", BindingFlags.NonPublic | BindingFlags.Instance)!; | ||
FieldInfo next = linkedSlot.FieldType.GetField("_next", BindingFlags.NonPublic | BindingFlags.Instance)!; | ||
FieldInfo value = next.FieldType.GetField("_value", BindingFlags.NonPublic | BindingFlags.Instance)!; | ||
|
||
// The code we are trying to generate: | ||
// | ||
// object? linkedSlot = linkedSlot.GetValue(threadLocal); | ||
// if (linkedSlot == null) | ||
// { | ||
// return 0; | ||
// } | ||
// | ||
// long total = 0; | ||
// for (linkedSlot = next.GetValue(linkedSlot); linkedSlot != null; linkedSlot = next.GetValue(linkedSlot)) | ||
// { | ||
// total += (value.GetValue(linkedSlot) as BoxedLong)!.Value; | ||
// } | ||
// return total; | ||
|
||
// Parameters | ||
ParameterExpression threadLocalParam = Expression.Parameter(typeof(ThreadLocal<BoxedLong>), "threadLocal"); | ||
|
||
// Fields | ||
MemberExpression linkedSlotField = Expression.Field(threadLocalParam, linkedSlot); | ||
|
||
// Variables | ||
ParameterExpression linkedSlotVar = Expression.Variable(linkedSlotField.Type, "linkedSlot"); | ||
ParameterExpression totalVar = Expression.Variable(typeof(long), "total"); | ||
|
||
// Assignments | ||
BinaryExpression assignLinkedSlot = Expression.Assign(linkedSlotVar, linkedSlotField); | ||
BinaryExpression assignTotal = Expression.Assign(totalVar, Expression.Constant(0L)); | ||
BinaryExpression assignNextSlot = Expression.Assign(linkedSlotVar, Expression.Field(linkedSlotVar, next)); | ||
|
||
// Labels | ||
LabelTarget breakLabel = Expression.Label(typeof(long), "breakLabel"); | ||
|
||
ConditionalExpression breakCondition = | ||
Expression.IfThen( | ||
Expression.Equal(linkedSlotVar, Expression.Constant(null)), | ||
Expression.Break(breakLabel, totalVar) | ||
); | ||
|
||
// Loop body | ||
BlockExpression loopBody = Expression.Block( | ||
breakCondition, | ||
Expression.AddAssign( | ||
totalVar, | ||
Expression.Property( | ||
Expression.Field(linkedSlotVar, value), typeof(BoxedLong), | ||
nameof(BoxedLong.Value) | ||
) | ||
), | ||
assignNextSlot | ||
); | ||
|
||
// Loop | ||
LoopExpression loop = Expression.Loop(loopBody); | ||
|
||
// Block | ||
BlockExpression block = Expression.Block( | ||
new[] { linkedSlotVar, totalVar }, | ||
assignLinkedSlot, | ||
assignTotal, | ||
breakCondition, | ||
assignNextSlot, | ||
loop, | ||
Expression.Label(breakLabel, totalVar) | ||
); | ||
|
||
// Lambda | ||
Expression<Func<ThreadLocal<BoxedLong>, long>> lambda = | ||
Expression.Lambda<Func<ThreadLocal<BoxedLong>, long>>(block, threadLocalParam); | ||
|
||
return lambda.Compile(); | ||
} | ||
|
||
public void Increment(int value = 1) => _threadLocal.Value!.Increment(value); | ||
public long ThreadLocalValue => _threadLocal.Value!.Value; | ||
|
||
private class BoxedLong | ||
{ | ||
private long _value; | ||
public long Value => _value; | ||
public void Increment(int value) => _value += value; | ||
} | ||
} |
This file contains 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
Oops, something went wrong.