Skip to content

feat(alloc): new allocator crate#402

Merged
morrisonlevi merged 11 commits into
mainfrom
levi/alloc-crate
May 13, 2024
Merged

feat(alloc): new allocator crate#402
morrisonlevi merged 11 commits into
mainfrom
levi/alloc-crate

Conversation

@morrisonlevi

@morrisonlevi morrisonlevi commented Apr 25, 2024

Copy link
Copy Markdown
Contributor

What does this PR do?

Adds a new datadog-alloc crate. There are three allocators:

  1. LinearAllocator<A: Allocator>
  2. ChainAllocator<A: Allocator + Clone>
  3. VirtualAllocator

The ChainAllocator and LinearAllocator support querying the number of used and reserved bytes. They are arena allocators, which means they do not deallocate individual items, and do batched deallocation when they drop.

Each of these allocators is further documented in the code.

Motivation

This will be used with the new StringTable API for datadog-profiling. Avoiding the system allocator has benefits. In particular, sometimes with the individual string allocations it would look like there was a memory leak but was just the system allocator holding onto memory we previously allocated without returning it to the system.

Being able to query used and reserved memory can be useful for stats. The PHP profiler intends to use these to conditionally reset its string cache.

Additional Notes

The allocator API is not stable, so this uses the allocator-api2 which exports the same API for stable usage.

Here is a string table diagram that shows how these allocators get used:

StringTableDiagram

How to test the change?

Regular cargo tests.

For Reviewers

  • If this PR touches code that signs or publishes builds or packages, or handles credentials of any kind, I've requested a review from @DataDog/security-design-and-guidance.
  • This PR doesn't touch any of that.

Comment thread alloc/src/virtual.rs Outdated
Comment thread alloc/src/chain.rs
Comment thread alloc/src/chain.rs
/// new [LinearAllocator] to the previous one, creating a chain. This is where
/// its name comes from.
pub struct ChainAllocator<A: Allocator + Clone> {
top: UnsafeCell<ChainNodePtr<A>>,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this called top

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you think of the chain as a stack, this is the one on "top".

Comment thread alloc/src/chain.rs Outdated
Comment thread alloc/src/chain.rs
/// is worth it. This is somewhat arbitrarily chosen at the moment.
const MIN_NODE_SIZE: usize = 4 * size_of::<Self>();

/// Creates a new [ChainAllocator]. The `chunk_size_hint` is used as a

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any advice on how to choose/tune this?

@morrisonlevi morrisonlevi May 1, 2024

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I clarified a bit in the docs. Let me know if you want more advice here.

Comment thread alloc/src/chain.rs Outdated
Comment thread alloc/src/chain.rs Outdated
Comment thread alloc/src/chain.rs Outdated
Comment thread alloc/src/chain.rs Outdated
@morrisonlevi morrisonlevi added the profiling Relates to the profiling* modules. label May 1, 2024
@github-actions github-actions Bot removed the profiling Relates to the profiling* modules. label May 1, 2024
@morrisonlevi morrisonlevi force-pushed the levi/alloc-crate branch 3 times, most recently from 0f98317 to 622db33 Compare May 1, 2024 21:39
This will be used with the new StringTable API for datadog-profiling.
The Chain- and LinearAllocators support querying the number of used
and reserved bytes. They are arena allocators, which means they do not
deallocate individual items, and do batched deallocation when they
drop.
@codecov-commenter

codecov-commenter commented May 4, 2024

Copy link
Copy Markdown

Codecov Report

Attention: Patch coverage is 98.37251% with 9 lines in your changes are missing coverage. Please review.

Project coverage is 66.28%. Comparing base (47143b2) to head (e798f04).

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #402      +/-   ##
==========================================
+ Coverage   65.49%   66.28%   +0.78%     
==========================================
  Files         184      187       +3     
  Lines       22565    23118     +553     
==========================================
+ Hits        14780    15324     +544     
- Misses       7785     7794       +9     
Components Coverage Δ
crashtracker 20.31% <ø> (ø)
datadog-alloc 98.37% <98.37%> (∅)
data-pipeline 51.45% <ø> (ø)
data-pipeline-ffi 0.00% <ø> (ø)
ddcommon 81.23% <ø> (ø)
ddcommon-ffi 74.93% <ø> (ø)
ddtelemetry 53.72% <ø> (ø)
ipc 81.27% <ø> (ø)
profiling 76.83% <ø> (ø)
profiling-ffi 60.05% <ø> (ø)
serverless 0.00% <ø> (ø)
sidecar 29.37% <ø> (ø)
sidecar-ffi 0.00% <ø> (ø)
spawn-worker 54.98% <ø> (ø)
trace-mini-agent 69.12% <ø> (ø)
trace-normalization 97.79% <ø> (ø)
trace-obfuscation 95.74% <ø> (ø)
trace-protobuf 25.64% <ø> (ø)
trace-utils 68.85% <ø> (ø)

@morrisonlevi morrisonlevi marked this pull request as ready for review May 9, 2024 20:25
@morrisonlevi morrisonlevi requested review from a team as code owners May 9, 2024 20:25
@morrisonlevi morrisonlevi marked this pull request as draft May 9, 2024 20:27
@morrisonlevi morrisonlevi marked this pull request as ready for review May 9, 2024 20:49
@morrisonlevi morrisonlevi added profiling Relates to the profiling* modules. enhancement New feature or request labels May 10, 2024

@r1viollet r1viollet left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM
Thanks for providing allocators!

@github-actions github-actions Bot removed the profiling Relates to the profiling* modules. label May 13, 2024
@morrisonlevi morrisonlevi merged commit e4c1985 into main May 13, 2024
@morrisonlevi morrisonlevi deleted the levi/alloc-crate branch May 13, 2024 15:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ci-build enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants