Skip to content

feat(data_structures): add CodeBuffer::*_unchecked_cap methods #10075

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

overlookmotel
Copy link
Contributor

@overlookmotel overlookmotel commented Mar 27, 2025

Add 3 methods to CodeBuffer:

  • print_byte_unchecked_cap
  • print_bytes_unchecked_cap
  • print_str_unchecked_cap

All 3 are unsafe methods which require caller to ensure there is sufficient capacity in the buffer. This allows minimizing bounds checks when pushing multiple bytes sequences to the buffer.

let mut buf = CodeBuffer::new();

let s: &str = "Hello, ";
let b: &[u8] = b"world";
let c: u8 = b'!';

buf.reserve(s.len() + b.len() + 1);

// No bounds checks.
// SAFETY: Have reserved sufficient capacity
unsafe {
    buf.print_str_unchecked_cap(s);
    buf.print_bytes_unchecked_cap(b);
    buf.print_byte_unchecked_cap(c);
}

Copy link
Contributor Author

overlookmotel commented Mar 27, 2025


How to use the Graphite Merge Queue

Add either label to this PR to merge it via the merge queue:

  • 0-merge - adds this PR to the back of the merge queue
  • hotfix - for urgent hot fixes, skip the queue and merge this PR next

You must have a Graphite account in order to use the merge queue. Sign up using this link.

An organization admin has enabled the Graphite Merge Queue in this repository.

Please do not merge from GitHub as this will restart CI on PRs being processed by the merge queue.

This stack of pull requests is managed by Graphite. Learn more about stacking.

@overlookmotel overlookmotel marked this pull request as ready for review March 27, 2025 10:24
Copy link

codspeed-hq bot commented Mar 27, 2025

CodSpeed Instrumentation Performance Report

Merging #10075 will not alter performance

Comparing 03-27-feat_data_structures_add_codebuffer__unchecked_cap_methods (d53e323) with main (5e14fe9)

Summary

✅ 33 untouched benchmarks

@overlookmotel overlookmotel changed the base branch from 03-26-refactor_codegen_print_string_literals_containing_lone_surrogates_without_reference_to_raw_ to graphite-base/10075 March 27, 2025 15:46
@overlookmotel overlookmotel force-pushed the 03-27-feat_data_structures_add_codebuffer__unchecked_cap_methods branch from b6529c8 to d53e323 Compare March 27, 2025 15:46
@overlookmotel overlookmotel changed the base branch from graphite-base/10075 to main March 27, 2025 15:46
@overlookmotel
Copy link
Contributor Author

I wrote these originally to use in #10046, but turns out they're unnecessary there. But these are still useful APIs, so I think worthwhile adding them.

@Boshen
Copy link
Member

Boshen commented Mar 28, 2025

I'm not keen to add code that's never going to be used though. Do we have scenarios for these 3 methods?

@Boshen Boshen marked this pull request as draft March 28, 2025 04:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-enhancement Category - New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants