Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support allocator api #426

Open
wants to merge 19 commits into
base: main
Choose a base branch
from

Conversation

SchrodingerZhu
Copy link
Contributor

@SchrodingerZhu SchrodingerZhu commented Nov 21, 2021

export FFIs to enable snmalloc being used as a separate allocator
https://doc.rust-lang.org/stable/std/alloc/trait.Allocator.html

Signed-off-by: SchrodingerZhu <i@zhuyi.fan>
Signed-off-by: SchrodingerZhu <i@zhuyi.fan>
Signed-off-by: SchrodingerZhu <i@zhuyi.fan>
Signed-off-by: SchrodingerZhu <i@zhuyi.fan>
@SchrodingerZhu SchrodingerZhu force-pushed the rust-heap-alloc branch 2 times, most recently from 54f977b to b43efc5 Compare November 21, 2021 13:42
Signed-off-by: SchrodingerZhu <i@zhuyi.fan>
Signed-off-by: SchrodingerZhu <i@zhuyi.fan>
Signed-off-by: SchrodingerZhu <i@zhuyi.fan>
Signed-off-by: SchrodingerZhu <i@zhuyi.fan>
Copy link
Member

@mjp41 mjp41 left a comment

Choose a reason for hiding this comment

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

So I am wondering if we should add a test to give basic coverage of this API in the snmalloc repo, so we detect breaks before it reaches snmalloc-rs?

src/override/rust.cc Show resolved Hide resolved
src/override/rust.cc Outdated Show resolved Hide resolved
src/override/rust.cc Outdated Show resolved Hide resolved
Signed-off-by: SchrodingerZhu <i@zhuyi.fan>
Signed-off-by: SchrodingerZhu <i@zhuyi.fan>
Signed-off-by: SchrodingerZhu <i@zhuyi.fan>
Signed-off-by: SchrodingerZhu <i@zhuyi.fan>
Signed-off-by: SchrodingerZhu <i@zhuyi.fan>
Signed-off-by: SchrodingerZhu <i@zhuyi.fan>
Signed-off-by: SchrodingerZhu <i@zhuyi.fan>
Signed-off-by: SchrodingerZhu <i@zhuyi.fan>
Signed-off-by: SchrodingerZhu <i@zhuyi.fan>
Signed-off-by: SchrodingerZhu <i@zhuyi.fan>
@SchrodingerZhu
Copy link
Contributor Author

it there a way to trigger the build again?

@mjp41
Copy link
Member

mjp41 commented Nov 24, 2021

So on

https://github.com/microsoft/snmalloc/runs/4311202740?check_suite_focus=true

I have a Re-runall checks:

image**

I'll hit it for you. Do you need some help with the Windows tests?

@SchrodingerZhu
Copy link
Contributor Author

Ha, I guess sth interesting is happening on Win. I will have a look later when I can clear some space for MSVC on my PC ;).

@mjp41
Copy link
Member

mjp41 commented Nov 24, 2021

Running your test gives:

        _STL_ASSERT(_Left == _Right, "containers incompatible for swap");

failing on

  std::swap(cloned, moved);

in void test_allocator_vector()

Signed-off-by: SchrodingerZhu <i@zhuyi.fan>

void test_allocator_vector()
{
using Inner = std::vector<size_t, RAllocator<size_t>>;
Copy link
Member

Choose a reason for hiding this comment

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

Could you add some comments to this test explaining what it is testing?

{
return ThreadAlloc::get().alloc(aligned_size(alignment, size));
}
enum class SizeChange
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please can you add a comment explaining (and, ideally, linking to) the Rust APIs that this is intended to expose?

if (
size_to_sizeclass_full(aligned_old_size).raw() ==
size_to_sizeclass_full(aligned_new_size).raw())
return ptr;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
return ptr;
{
return ptr;
}

As a general style point we should have braces around all if statement bodies because it reduces bugs introduced when someone edits the code later, but this case is a particularly good example of needing them for readability because the wrapped line in the condition is at the same indent level as the body of the conditiona.

size_to_sizeclass_full(aligned_new_size).raw())
return ptr;
// this may memset more than it needs, but doing so increases
// the possibility for large area to be efficiently zeroed by special
Copy link
Collaborator

Choose a reason for hiding this comment

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

Those methods are exposed in the PAL, they could be called here.

@SchrodingerZhu
Copy link
Contributor Author

Sure! I am still sorting out the design.

@SchrodingerZhu
Copy link
Contributor Author

@mjp41 @davidchisnall I begin to wonder whether we should only expose sth like sn_rust_grow, sn_rust_shrink.

I wanted to support sth like:

 fn allocator_supports_vector() {
        let allocator = SnAllocator::new();
        let mut vec = std::vec::Vec::new_in(&allocator);
        let mut sum: usize = 0;
        for i in 1..512usize {
            vec.push(i);
            sum += i * i;
        }

        let res = vec.into_iter().flat_map(|x| {
            let mut v = std::vec::Vec::new_in(&allocator);
            for _ in 0..x {
                v.push(x);
            }
            v
        }).sum();

        assert_eq!(sum, res);
    }

But I begin to wonder whether it is a bad decision to let user use a Alloc* instead of make Alloc thread_local.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants