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

perf: Refactor underlying buffers for branchless access #229

Merged
merged 15 commits into from
Jan 16, 2023

Conversation

ParkMyCar
Copy link
Owner

This PR refactors the underlying InlineString and BoxString structs in a major way to allow for branch-less string accesses. It also renames these structs to InlineBuffer and HeapBuffer, treating these types strictly as "buffers". We also move string operations into Repr (e.g. pushing and popping), which de-dupes code.

@ParkMyCar ParkMyCar force-pushed the perf/refactor-underlying-buffers branch from e52c187 to 68c5e74 Compare January 3, 2023 01:00
compact_str/src/repr2/heap.rs Outdated Show resolved Hide resolved
compact_str/src/repr2/heap.rs Outdated Show resolved Hide resolved
compact_str/src/repr2/heap.rs Outdated Show resolved Hide resolved
compact_str/src/lib.rs Outdated Show resolved Hide resolved
compact_str/src/repr2/heap.rs Outdated Show resolved Hide resolved
// SAFTEY: Our needed_capacity is >= our length, which is <= than MAX_SIZE
let inline = unsafe { InlineBuffer::new(self.as_str()) };
// SAFETY: `InlineBuffer` and `Repr` are the same size
*self = unsafe { mem::transmute(inline) };
Copy link
Contributor

Choose a reason for hiding this comment

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

How about implementing impl From<InlineBuffer> for Repr and impl From<HeapBuffer> for Repr so that we don't need these unsafe transmute blocks everywhere?

Copy link
Owner Author

Choose a reason for hiding this comment

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

For now I'm going to stick with this pattern because I believe it'll be more performant. Otherwise we'll need to call through fn from(...) which would incur a performance penalty

}
} else {
let pointer = &self as *const _ as *const u8;
let length = core::cmp::min((last_byte.wrapping_sub(LENGTH_MASK)) as usize, MAX_SIZE);
Copy link
Contributor

Choose a reason for hiding this comment

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

I've seen this a couple of time, maybe it can be extracted as a separate function?

Copy link
Owner Author

Choose a reason for hiding this comment

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

Not a bad idea, I'll do this in a follow up PR

#[repr(C)]
pub struct Repr(
// We have a pointer in the repesentation to properly carry provenance
*const (),
Copy link
Contributor

Choose a reason for hiding this comment

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

I think it's possible to use ptr::NonNull<u8> here if we modify push_str, extend and etc to ignore null bytes inserted into the string.

Copy link
Owner Author

Choose a reason for hiding this comment

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

Good thought! For now I'll just keep it *const () but we could change this in the future

@ParkMyCar
Copy link
Owner Author

Thanks for the review @NobodyXu! I'll update the PR soon with the feedback :)

@ParkMyCar ParkMyCar changed the title [WIP] perf: Refactor underlying buffers for branchless access perf: Refactor underlying buffers for branchless access Jan 15, 2023
@ParkMyCar ParkMyCar merged commit c95edd5 into main Jan 16, 2023
@ParkMyCar ParkMyCar deleted the perf/refactor-underlying-buffers branch January 16, 2023 16:58
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

2 participants