Use pool of ObjectTraverseStacks in ToObject().#2386
Merged
Conversation
Contributor
Author
|
Benchmark: [MemoryDiagnoser]
public class ToObjectBench
{
private Engine _engine = new Engine().SetValue("test", new Dictionary<int, object>());
[Benchmark]
public void InteropCall() =>
_engine.Execute("""
for(var i = 0; i < 10; i++) {
test.Add(i, {});
}
test.Clear();
""");
}
I also ran the interop benchmark set, but they showed no difference in runtime with only one test showing a very small decrease in allocated memory. |
Collaborator
|
I think ObjectPool is not thread-safe so it shouldn't be exposed as static field (static via ownership), Engine field would be safe. Needs rebase. |
For some workloads, ToObject causes the Stack<object> internal to ObjectTraverseStack to allocate additional memory, which is then prompty disposed of only to be re-allocated again on the next call that requires the conversion. Using a pool reduces the amount of allocation and, for the workload I am optimizing for, makes `ToObject()` so fast that it vanishes from the profiler.or the workload I am optimizing for, makes ToObject() so fast that it vanishes from the profiler.
4fdf4b1 to
dccd0a9
Compare
This was referenced Apr 13, 2026
This was referenced Jun 8, 2026
This was referenced Jun 29, 2026
This was referenced Jul 7, 2026
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
For some workloads,
ObjectInstance.ToObject()causes theStack<object>internal toObjectTraverseStackto allocate additional memory, which is then promptly disposed of, only to be re-allocated again on the next call that requires the conversion.Using a pool reduces the amount of allocation and, for the workload I am optimizing for, makes
ToObject()so fast that it vanishes from the profiler.