-
Notifications
You must be signed in to change notification settings - Fork 147
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Dynamic Instrumentation] DEBUG-3088 Add object pool (#6105)
## Summary of changes Add object pool (IPoolable) to use in probe processor and snapshot creator. ## Implementation details Object pool with size limitation.
- Loading branch information
1 parent
a509060
commit b80b1ae
Showing
10 changed files
with
320 additions
and
33 deletions.
There are no files selected for viewing
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
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
9 changes: 9 additions & 0 deletions
9
tracer/src/Datadog.Trace/Debugger/Expressions/MethodScopeMembersParameters.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,9 @@ | ||
// <copyright file="MethodScopeMembersParameters.cs" company="Datadog"> | ||
// Unless explicitly stated otherwise all files in this repository are licensed under the Apache 2 License. | ||
// This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2017 Datadog, Inc. | ||
// </copyright> | ||
|
||
namespace Datadog.Trace.Debugger.Expressions; | ||
|
||
#nullable enable | ||
internal record struct MethodScopeMembersParameters(int NumberOfLocals, int NumberOfArguments); |
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
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,15 @@ | ||
// <copyright file="IPoolable.cs" company="Datadog"> | ||
// Unless explicitly stated otherwise all files in this repository are licensed under the Apache 2 License. | ||
// This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2017 Datadog, Inc. | ||
// </copyright> | ||
|
||
#nullable enable | ||
namespace Datadog.Trace.Debugger.Helpers | ||
{ | ||
internal interface IPoolable<in TSetParameters> | ||
{ | ||
void Set(TSetParameters parameters); | ||
|
||
void Reset(); | ||
} | ||
} |
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,51 @@ | ||
// <copyright file="ObjectPool.cs" company="Datadog"> | ||
// Unless explicitly stated otherwise all files in this repository are licensed under the Apache 2 License. | ||
// This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2017 Datadog, Inc. | ||
// </copyright> | ||
|
||
#nullable enable | ||
using System; | ||
using System.Collections.Concurrent; | ||
|
||
namespace Datadog.Trace.Debugger.Helpers | ||
{ | ||
internal class ObjectPool<T, TSetParameters> | ||
where T : class, IPoolable<TSetParameters>, new() | ||
{ | ||
private readonly ConcurrentBag<T> _objects; | ||
private readonly Func<T> _objectFactory; | ||
private readonly int _maxSize; | ||
|
||
public ObjectPool(Func<T>? objectFactory = null, int maxSize = 100) | ||
{ | ||
if (maxSize <= 0) | ||
{ | ||
throw new ArgumentException("Maximum pool size must be greater than zero.", nameof(maxSize)); | ||
} | ||
|
||
_objects = new ConcurrentBag<T>(); | ||
_objectFactory = objectFactory ?? (() => new T()); | ||
_maxSize = maxSize; | ||
} | ||
|
||
public int Count => _objects.Count; | ||
|
||
public T? Get() => _objects.TryTake(out var item) ? item : _objectFactory(); | ||
|
||
public T? Get(TSetParameters parameters) | ||
{ | ||
var item = _objects.TryTake(out var obj) ? obj : _objectFactory(); | ||
item?.Set(parameters); | ||
return item; | ||
} | ||
|
||
public void Return(T? item) | ||
{ | ||
item?.Reset(); | ||
if (item != null && _objects.Count < _maxSize) | ||
{ | ||
_objects.Add(item); | ||
} | ||
} | ||
} | ||
} |
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
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
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.