Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Use Cell::update, slice::{split_first_chunk,split_off}
  • Loading branch information
coolreader18 committed Jan 30, 2026
commit 11b1e43a7b2d0d23230ee07d6c09ea5d7d4a458b
2 changes: 1 addition & 1 deletion crates/common/src/lock/cell_lock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ unsafe impl RawRwLock for RawCellRwLock {

#[inline]
unsafe fn unlock_shared(&self) {
self.state.set(self.state.get() - ONE_READER)
self.state.update(|x| x - ONE_READER)
}

#[inline]
Expand Down
10 changes: 7 additions & 3 deletions crates/compiler-core/src/marshal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,13 +158,17 @@ impl Read for &[u8] {
fn read_slice(&mut self, n: u32) -> Result<&[u8]> {
self.read_slice_borrow(n)
}

fn read_array<const N: usize>(&mut self) -> Result<&[u8; N]> {
let (chunk, rest) = self.split_first_chunk::<N>().ok_or(MarshalError::Eof)?;
*self = rest;
Ok(chunk)
}
}

impl<'a> ReadBorrowed<'a> for &'a [u8] {
fn read_slice_borrow(&mut self, n: u32) -> Result<&'a [u8]> {
let data = self.get(..n as usize).ok_or(MarshalError::Eof)?;
*self = &self[n as usize..];
Ok(data)
self.split_off(..n as usize).ok_or(MarshalError::Eof)
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/vm/src/codecs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ impl<'a> DecodeErrorHandler<PyDecodeContext<'a>> for SurrogatePass {
let p = &s[byte_range.start..];

fn slice<const N: usize>(p: &[u8]) -> Option<[u8; N]> {
p.get(..N).map(|x| x.try_into().unwrap())
p.first_chunk().copied()
}

let c = match standard_encoding {
Expand Down
7 changes: 3 additions & 4 deletions crates/vm/src/vm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -745,10 +745,9 @@ impl VirtualMachine {
return Err(self.new_recursion_error(_where.to_string()));
}

self.recursion_depth.set(self.recursion_depth.get() + 1);
let result = f();
self.recursion_depth.set(self.recursion_depth.get() - 1);
result
self.recursion_depth.update(|d| d + 1);
scopeguard::defer! { self.recursion_depth.update(|d| d - 1) }
f()
}

pub fn with_frame<R, F: FnOnce(FrameRef) -> PyResult<R>>(
Expand Down
22 changes: 1 addition & 21 deletions crates/wtf8/src/core_str_count.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ fn do_count_chars(s: &Wtf8) -> usize {
// a subset of the sum of this chunk, like a `[u8; size_of::<usize>()]`.
let mut counts = 0;

let (unrolled_chunks, remainder) = slice_as_chunks::<_, UNROLL_INNER>(chunk);
let (unrolled_chunks, remainder) = chunk.as_chunks::<UNROLL_INNER>();
for unrolled in unrolled_chunks {
for &word in unrolled {
// Because `CHUNK_SIZE` is < 256, this addition can't cause the
Expand Down Expand Up @@ -137,26 +137,6 @@ const fn usize_repeat_u16(x: u16) -> usize {
}
r
}

fn slice_as_chunks<T, const N: usize>(slice: &[T]) -> (&[[T; N]], &[T]) {
assert!(N != 0, "chunk size must be non-zero");
let len_rounded_down = slice.len() / N * N;
// SAFETY: The rounded-down value is always the same or smaller than the
// original length, and thus must be in-bounds of the slice.
let (multiple_of_n, remainder) = unsafe { slice.split_at_unchecked(len_rounded_down) };
// SAFETY: We already panicked for zero, and ensured by construction
// that the length of the subslice is a multiple of N.
let array_slice = unsafe { slice_as_chunks_unchecked(multiple_of_n) };
(array_slice, remainder)
}

unsafe fn slice_as_chunks_unchecked<T, const N: usize>(slice: &[T]) -> &[[T; N]] {
let new_len = slice.len() / N;
// SAFETY: We cast a slice of `new_len * N` elements into
// a slice of `new_len` many `N` elements chunks.
unsafe { std::slice::from_raw_parts(slice.as_ptr().cast(), new_len) }
}

const fn unlikely(x: bool) -> bool {
x
}