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

Refactor builder arena #362

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

marmeladema
Copy link
Contributor

Each commit should be self explainable.
This makes the code simpler and should help the compiler optimize things a bit better.

@marmeladema
Copy link
Contributor Author

There is a small bug in the first commit because the vec of segments is leaked. It is not strictly unsound bit I'll fix it tomorrow anyway.

@marmeladema
Copy link
Contributor Author

@dwrensha this is ready for review :)

Unless capnp/src/private is officially part of the public API, it should not be a breaking change.

self.segments.push(BuilderSegment {
ptr: seg.0,
capacity: seg.1,
allocated: 0,
allocated: minimum_size,
Copy link
Member

Choose a reason for hiding this comment

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

This looks wrong to me. allocated is supposed to indicate how many words of the segment are already in use, which should be zero when the segment is first allocated. It seems to me that what you have here would result in a lot of unused memory.

I pushed some new doc comments for BuilderSegment that might make things clearer: 8888443

I suppose one things that's potentially confusing about all this is that the word "allocate" is being used for different things:

  1. the message::Allocator, whose job is to provide memory from the system
  2. BuilderArena::allocate(), which takes takes memory already-"allocated" memory from (1) and reserves it for usage by private::layout.

Copy link
Member

Choose a reason for hiding this comment

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

Oh, I see. This has to do with commit b6f6931. It happens to work because there are only a two usages of the allocate_segment() method, and they follow a particular pattern.

I'd prefer not to make this change. If we do make it, we should at least add some comments about the new behavior, and maybe also change some naming.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Maybe we can introduce a dedicated method for this then? I think having a non fallible method helps with readability and optimizations

Copy link
Contributor Author

@marmeladema marmeladema Jan 16, 2023

Choose a reason for hiding this comment

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

@dwrensha would allocate_full_segment be a suitable name for this method? I am not very good with names 😅 Given that this is a private method, maybe it doesn't matter too much? Once decided I will also update the doc comment

@@ -247,7 +261,12 @@ where
pub fn into_allocator(self) -> A {
let mut inner = self.inner.into_inner();
inner.deallocate_all();
inner.allocator.take().unwrap()
// See https://rust-lang.zulipchat.com/#narrow/stream/122651-general/topic/.E2.9C.94.20Extracting.20field.20from.20struct.20that.20implement.20.60Drop.60
let (allocator, _segments) = unsafe {
Copy link
Member

@dwrensha dwrensha Jan 16, 2023

Choose a reason for hiding this comment

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

I'm hesitant to add more unsafe like this. I'm tempted to pull in the changes from #363 without the changes from this PR, to see if that still works well. Getting rid of that RefCell to me is the big win of all this.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

As you wish :-) I will likely be testing all those changes in production in the next couple of weeks.

I do think simplifying the codebase, is worth one unsafe block though.

Copy link
Member

Choose a reason for hiding this comment

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

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.

2 participants