From 7bc7a8c1c8e145fa923ec37741d30f3088935498 Mon Sep 17 00:00:00 2001 From: Sam Gwilym Date: Wed, 11 Sep 2024 14:02:03 +0100 Subject: [PATCH 1/2] Rename all get_* methods --- data-model/src/path.rs | 88 ++++++++++++++-------------- data-model/src/relative_encodings.rs | 32 ++++------ fuzz/src/path.rs | 16 ++--- 3 files changed, 64 insertions(+), 72 deletions(-) diff --git a/data-model/src/path.rs b/data-model/src/path.rs index 240576a..002aa3b 100644 --- a/data-model/src/path.rs +++ b/data-model/src/path.rs @@ -294,7 +294,7 @@ impl Path { /// /// Runs in `O(n)`, where `n` is the total length of the new path in bytes. Performs a single allocation of `O(n)` bytes. pub fn append(&self, comp: Component) -> Result { - let total_length = self.get_path_length() + comp.len(); + let total_length = self.path_length() + comp.len(); return Self::new_from_iter( total_length, &mut ExactLengthChain::new(self.components(), iter::once(comp)), @@ -309,7 +309,7 @@ impl Path { /// /// Runs in `O(n)`, where `n` is the total length of the new path in bytes. Performs a single allocation of `O(n)` bytes. pub fn append_slice(&self, components: &[Component]) -> Result { - let mut total_length = self.get_path_length(); + let mut total_length = self.path_length(); for comp in components { total_length += comp.len(); } @@ -327,7 +327,7 @@ impl Path { /// #### Complexity /// /// Runs in `O(1)`, performs no allocations. - pub fn get_component_count(&self) -> usize { + pub fn component_count(&self) -> usize { self.component_count } @@ -337,7 +337,7 @@ impl Path { /// /// Runs in `O(1)`, performs no allocations. pub fn is_empty(&self) -> bool { - self.get_component_count() == 0 + self.component_count() == 0 } /// Get the sum of the lengths of all components in this path. @@ -347,11 +347,11 @@ impl Path { /// #### Complexity /// /// Runs in `O(1)`, performs no allocations. - pub fn get_path_length(&self) -> usize { + pub fn path_length(&self) -> usize { if self.component_count == 0 { 0 } else { - return HeapEncoding::::get_sum_of_lengths_for_component( + return HeapEncoding::::sum_of_lengths_for_component( self.data.as_ref(), self.component_count - 1, ) @@ -364,8 +364,8 @@ impl Path { /// #### Complexity /// /// Runs in `O(1)`, performs no allocations. - pub fn get_component(&self, i: usize) -> Option> { - return HeapEncoding::::get_component(self.data.as_ref(), i); + pub fn component(&self, i: usize) -> Option> { + return HeapEncoding::::component(self.data.as_ref(), i); } /// Get an owned handle to the `i`-th [`Component`] of this path. @@ -373,7 +373,7 @@ impl Path { /// #### Complexity /// /// Runs in `O(1)`, performs no allocations. - pub fn get_owned_component(&self, i: usize) -> Option> { + pub fn owned_component(&self, i: usize) -> Option> { let start = HeapEncoding::::start_offset_of_component(self.data.0.as_ref(), i)?; let end = HeapEncoding::::end_offset_of_component(self.data.0.as_ref(), i)?; Some(OwnedComponent(self.data.0.slice(start..end))) @@ -405,8 +405,8 @@ impl Path { i: usize, ) -> impl DoubleEndedIterator> + ExactSizeIterator> { - (i..self.get_component_count()).map(|i| { - self.get_component(i).unwrap() // Only `None` if `i >= self.get_component_count()` + (i..self.component_count()).map(|i| { + self.component(i).unwrap() // Only `None` if `i >= self.component_count()` }) } @@ -438,8 +438,8 @@ impl Path { ) -> impl DoubleEndedIterator> + ExactSizeIterator> + '_ { - (i..self.get_component_count()).map(|i| { - self.get_owned_component(i).unwrap() // Only `None` if `i >= self.get_component_count()` + (i..self.component_count()).map(|i| { + self.owned_component(i).unwrap() // Only `None` if `i >= self.component_count()` }) } @@ -451,7 +451,7 @@ impl Path { /// /// Runs in `O(1)`, performs no allocations. pub fn create_prefix(&self, length: usize) -> Option { - if length > self.get_component_count() { + if length > self.component_count() { None } else { Some(unsafe { self.create_prefix_unchecked(length) }) @@ -462,7 +462,7 @@ impl Path { /// /// #### Safety /// - /// Undefined behaviour if `length` is greater than `self.get_component_count()`. May manifest directly, or at any later + /// Undefined behaviour if `length` is greater than `self.component_count()`. May manifest directly, or at any later /// function invocation that operates on the resulting [`Path`]. /// /// #### Complexity @@ -483,9 +483,9 @@ impl Path { /// /// Runs in `O(1)`, performs no allocations. pub fn all_prefixes(&self) -> impl DoubleEndedIterator + '_ { - (0..=self.get_component_count()).map(|i| { + (0..=self.component_count()).map(|i| { unsafe { - self.create_prefix_unchecked(i) // safe to call for i <= self.get_component_count() + self.create_prefix_unchecked(i) // safe to call for i <= self.component_count() } }) } @@ -503,7 +503,7 @@ impl Path { } } - self.get_component_count() <= other.get_component_count() + self.component_count() <= other.component_count() } /// Test whether this path is prefixed by the given path. @@ -532,7 +532,7 @@ impl Path { lcp_len += 1 } - self.create_prefix(lcp_len).unwrap() // zip ensures that lcp_len <= self.get_component_count() + self.create_prefix(lcp_len).unwrap() // zip ensures that lcp_len <= self.component_count() } /// Return the least path which is strictly greater than `self`, or return `None` if `self` is the greatest possible path. @@ -557,7 +557,7 @@ impl Path { // If it is possible to append a zero byte to a component, then doing so yields its successor. if component.len() < MCL - && HeapEncoding::::get_sum_of_lengths_for_component(self.data.as_ref(), i) + && HeapEncoding::::sum_of_lengths_for_component(self.data.as_ref(), i) .unwrap() // i < self.component_count < MPL { @@ -605,7 +605,7 @@ impl Path { for (i, component) in self.components().enumerate().rev() { // If it is possible to append a zero byte to a component, then doing so yields its successor. if component.len() < MCL - && HeapEncoding::::get_sum_of_lengths_for_component(self.data.as_ref(), i) + && HeapEncoding::::sum_of_lengths_for_component(self.data.as_ref(), i) .unwrap() // i < self.component_count < MPL { @@ -629,7 +629,7 @@ impl Path { let mut buf = clone_prefix_and_lengthen_final_component(self, i, 0); let length_of_prefix = - HeapEncoding::::get_sum_of_lengths_for_component(&buf, i).unwrap(); + HeapEncoding::::sum_of_lengths_for_component(&buf, i).unwrap(); // Update the length of the final component. buf_set_final_component_length( @@ -722,17 +722,17 @@ struct HeapEncoding(Bytes); // All offsets are in bytes, unless otherwise specified. // Arguments named `i` are the index of a component in the string, *not* a byte offset. impl HeapEncoding { - fn get_component_count(buf: &[u8]) -> usize { - Self::get_usize_at_offset(buf, 0).unwrap() // We always store at least 8byte for the component count. + fn component_count(buf: &[u8]) -> usize { + Self::usize_at_offset(buf, 0).unwrap() // We always store at least 8byte for the component count. } // None if i is outside the slice. - fn get_sum_of_lengths_for_component(buf: &[u8], i: usize) -> Option { + fn sum_of_lengths_for_component(buf: &[u8], i: usize) -> Option { let start_offset_in_bytes = Self::start_offset_of_sum_of_lengths_for_component(i); - Self::get_usize_at_offset(buf, start_offset_in_bytes) + Self::usize_at_offset(buf, start_offset_in_bytes) } - fn get_component(buf: &[u8], i: usize) -> Option> { + fn component(buf: &[u8], i: usize) -> Option> { let start = Self::start_offset_of_component(buf, i)?; let end = Self::end_offset_of_component(buf, i)?; return Some(unsafe { Component::new_unchecked(&buf[start..end]) }); @@ -744,21 +744,21 @@ impl HeapEncoding { } fn start_offset_of_component(buf: &[u8], i: usize) -> Option { - let metadata_length = (Self::get_component_count(buf) + 1) * size_of::(); + let metadata_length = (Self::component_count(buf) + 1) * size_of::(); if i == 0 { Some(metadata_length) } else { - Self::get_sum_of_lengths_for_component(buf, i - 1) // Length of everything up until the previous component. + Self::sum_of_lengths_for_component(buf, i - 1) // Length of everything up until the previous component. .map(|length| length + metadata_length) } } fn end_offset_of_component(buf: &[u8], i: usize) -> Option { - let metadata_length = (Self::get_component_count(buf) + 1) * size_of::(); - Self::get_sum_of_lengths_for_component(buf, i).map(|length| length + metadata_length) + let metadata_length = (Self::component_count(buf) + 1) * size_of::(); + Self::sum_of_lengths_for_component(buf, i).map(|length| length + metadata_length) } - fn get_usize_at_offset(buf: &[u8], offset_in_bytes: usize) -> Option { + fn usize_at_offset(buf: &[u8], offset_in_bytes: usize) -> Option { let end = offset_in_bytes + size_of::(); // We cannot interpret the memory in the slice as a usize directly, because the alignment might not match. @@ -802,7 +802,7 @@ mod encoding { where C: BulkConsumer, { - encode_max_power(self.get_component_count(), MCC, consumer).await?; + encode_max_power(self.component_count(), MCC, consumer).await?; for component in self.components() { encode_max_power(component.len(), MCL, consumer).await?; @@ -1034,7 +1034,7 @@ fn clone_prefix_and_lengthen_final_component< ) -> BytesMut { let original_slice = original.data.as_ref(); let successor_path_length = - HeapEncoding::::get_sum_of_lengths_for_component(original_slice, i).unwrap() + HeapEncoding::::sum_of_lengths_for_component(original_slice, i).unwrap() + extra_capacity; let buf_capacity = size_of::() * (i + 2) + successor_path_length; let mut buf = BytesMut::with_capacity(buf_capacity); @@ -1098,7 +1098,7 @@ impl ScratchSpacePathDecoding { /// # Safety /// /// Memory must have been initialised with a prior call to set_component_accumulated_length for the same `i`. - unsafe fn get_component_accumulated_length(&self, i: usize) -> usize { + unsafe fn component_accumulated_length(&self, i: usize) -> usize { self.component_accumulated_lengths[i] } @@ -1107,7 +1107,7 @@ impl ScratchSpacePathDecoding { /// # Safety /// /// Memory must have been initialised with prior call to set_component_accumulated_length for all `j <= i` - pub unsafe fn get_accumumulated_component_lengths(&self, i: usize) -> &[u8] { + pub unsafe fn accumulated_component_lengths(&self, i: usize) -> &[u8] { core::slice::from_raw_parts( self.component_accumulated_lengths[..i].as_ptr() as *const u8, i * size_of::(), @@ -1123,9 +1123,9 @@ impl ScratchSpacePathDecoding { let start = if i == 0 { 0 } else { - self.get_component_accumulated_length(i - 1) + self.component_accumulated_length(i - 1) }; - let end = self.get_component_accumulated_length(i); + let end = self.component_accumulated_length(i); &mut self.path_data[start..end] } @@ -1135,7 +1135,7 @@ impl ScratchSpacePathDecoding { /// /// Accumulated component lengths for `i - 1` must have been set (unless `i == 0`). pub unsafe fn path_data_until_as_mut(&mut self, i: usize) -> &mut [u8] { - let end = self.get_component_accumulated_length(i - 1); + let end = self.component_accumulated_length(i - 1); &mut self.path_data[0..end] } @@ -1145,11 +1145,11 @@ impl ScratchSpacePathDecoding { /// /// Memory must have been initialised with a prior call to set_component_accumulated_length for `i - 1` (ignored if `i == 0`). /// Also, the path data must have been initialised via a reference obtained from `self.path_data_as_mut()`. - unsafe fn get_path_data(&self, i: usize) -> &[u8] { + unsafe fn path_data(&self, i: usize) -> &[u8] { let end = if i == 0 { 0 } else { - self.get_component_accumulated_length(i - 1) + self.component_accumulated_length(i - 1) }; &self.path_data[..end] } @@ -1166,13 +1166,13 @@ impl ScratchSpacePathDecoding { let total_length = if i == 0 { 0 } else { - self.get_component_accumulated_length(i - 1) + self.component_accumulated_length(i - 1) }; let mut buf = BytesMut::with_capacity((size_of::() * (i + 1)) + total_length); buf.extend_from_slice(&i.to_ne_bytes()); - buf.extend_from_slice(self.get_accumumulated_component_lengths(i)); - buf.extend_from_slice(self.get_path_data(i)); + buf.extend_from_slice(self.accumulated_component_lengths(i)); + buf.extend_from_slice(self.path_data(i)); Path::from_buffer_and_component_count(buf.freeze(), i) } diff --git a/data-model/src/relative_encodings.rs b/data-model/src/relative_encodings.rs index 29728b6..1b7ddb4 100644 --- a/data-model/src/relative_encodings.rs +++ b/data-model/src/relative_encodings.rs @@ -48,10 +48,10 @@ pub(super) mod encoding { Consumer: BulkConsumer, { let lcp = self.longest_common_prefix(reference); - let lcp_component_count = lcp.get_component_count(); + let lcp_component_count = lcp.component_count(); encode_max_power(lcp_component_count, MCC, consumer).await?; - let suffix_component_count = self.get_component_count() - lcp_component_count; + let suffix_component_count = self.component_count() - lcp_component_count; encode_max_power(suffix_component_count, MCC, consumer).await?; for component in self.suffix_components(lcp_component_count) { @@ -87,10 +87,7 @@ pub(super) mod encoding { let decoded = Path::::decode(producer).await?; // === Necessary to produce canonic encodings. === - if lcp_component_count - != decoded - .longest_common_prefix(reference) - .get_component_count() + if lcp_component_count != decoded.longest_common_prefix(reference).component_count() { return Err(DecodeError::InvalidInput); } @@ -120,10 +117,9 @@ pub(super) mod encoding { // safe because we just copied the accumulated component lengths for the first `lcp_component_count` components. buf.path_data_until_as_mut(lcp_component_count) .copy_from_slice( - &reference.raw_buf()[size_of::() - * (reference.get_component_count() + 1) - ..size_of::() * (reference.get_component_count() + 1) - + prefix.get_path_length()], + &reference.raw_buf()[size_of::() * (reference.component_count() + 1) + ..size_of::() * (reference.component_count() + 1) + + prefix.path_length()], ); } @@ -134,7 +130,7 @@ pub(super) mod encoding { return Err(DecodeError::InvalidInput); } - let mut accumulated_component_length: usize = prefix.get_path_length(); // Always holds the acc length of all components we copied so far. + let mut accumulated_component_length: usize = prefix.path_length(); // Always holds the acc length of all components we copied so far. for i in lcp_component_count..total_component_count { let component_len: usize = decode_max_power(MCL, producer).await?.try_into()?; if component_len > MCL { @@ -160,11 +156,7 @@ pub(super) mod encoding { let decoded = unsafe { buf.to_path(total_component_count) }; // === Necessary to produce canonic encodings. === - if lcp_component_count - != decoded - .longest_common_prefix(reference) - .get_component_count() - { + if lcp_component_count != decoded.longest_common_prefix(reference).component_count() { return Err(DecodeError::InvalidInput); } // =============================================== @@ -533,7 +525,7 @@ pub(super) mod encoding { let start_lcp = self.path().longest_common_prefix(&out.paths().start); let end_lcp = self.path().longest_common_prefix(end_path); - start_lcp.get_component_count() >= end_lcp.get_component_count() + start_lcp.component_count() >= end_lcp.component_count() } RangeEnd::Open => true, }; @@ -665,7 +657,7 @@ pub(super) mod encoding { let start_lcp = path.longest_common_prefix(&out.paths().start); let end_lcp = path.longest_common_prefix(end_path); - start_lcp.get_component_count() >= end_lcp.get_component_count() + start_lcp.component_count() >= end_lcp.component_count() } RangeEnd::Open => true, }; @@ -1043,7 +1035,7 @@ pub(super) mod encoding { .longest_common_prefix(&reference.paths().start); let lcp_start_end = self.paths().start.longest_common_prefix(ref_path_end); - if lcp_start_start.get_component_count() >= lcp_start_end.get_component_count() { + if lcp_start_start.component_count() >= lcp_start_end.component_count() { header_1 |= 0b0000_1000; } } else { @@ -1062,7 +1054,7 @@ pub(super) mod encoding { self_path_end.longest_common_prefix(&reference.paths().start); let lcp_end_end = self_path_end.longest_common_prefix(ref_path_end); - if lcp_end_start.get_component_count() > lcp_end_end.get_component_count() { + if lcp_end_start.component_count() > lcp_end_end.component_count() { header_1 |= 0b0000_0010; } } diff --git a/fuzz/src/path.rs b/fuzz/src/path.rs index 2aa8bee..8b2a522 100644 --- a/fuzz/src/path.rs +++ b/fuzz/src/path.rs @@ -238,7 +238,7 @@ impl PathRc self.0.len() } - pub fn get_component(&self, i: usize) -> Option<&PathComponentBox> { + pub fn component(&self, i: usize) -> Option<&PathComponentBox> { return self.0.get(i); } @@ -930,24 +930,24 @@ pub fn assert_isomorphic_paths, p2: &Path, ) { - assert_eq!(ctrl1.component_count(), p1.get_component_count()); + assert_eq!(ctrl1.component_count(), p1.component_count()); assert_eq!(ctrl1.component_count() == 0, p1.is_empty()); assert_eq!( ctrl1.components().fold(0, |acc, comp| acc + comp.len()), - p1.get_path_length() + p1.path_length() ); for i in 0..ctrl1.component_count() { assert_eq!( - ctrl1.get_component(i).map(|comp| comp.as_ref()), - p1.get_component(i).map(|comp| comp.into_inner()) + ctrl1.component(i).map(|comp| comp.as_ref()), + p1.component(i).map(|comp| comp.into_inner()) ); - match p1.get_owned_component(i) { - None => assert_eq!(ctrl1.get_component(i), None), - Some(comp) => assert_eq!(ctrl1.get_component(i).unwrap().as_ref(), comp.as_ref()), + match p1.owned_component(i) { + None => assert_eq!(ctrl1.component(i), None), + Some(comp) => assert_eq!(ctrl1.component(i).unwrap().as_ref(), comp.as_ref()), } } From acf0c6596a404c232500549734844a5f1de04664 Mon Sep 17 00:00:00 2001 From: Sam Gwilym Date: Wed, 23 Oct 2024 13:57:20 +0200 Subject: [PATCH 2/2] fix --- data-model/src/relative_encodings.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/data-model/src/relative_encodings.rs b/data-model/src/relative_encodings.rs index 3a3882c..08d25a4 100644 --- a/data-model/src/relative_encodings.rs +++ b/data-model/src/relative_encodings.rs @@ -1650,8 +1650,8 @@ pub(super) mod encoding { path_start.longest_common_prefix(&reference.paths().start); let lcp_start_end = path_start.longest_common_prefix(ref_path_end); - let expected_is_start_rel_to_start = lcp_start_start.get_component_count() - >= lcp_start_end.get_component_count(); + let expected_is_start_rel_to_start = + lcp_start_start.component_count() >= lcp_start_end.component_count(); if expected_is_start_rel_to_start != is_path_start_rel_to_start { return Err(DecodeError::InvalidInput); @@ -1687,8 +1687,8 @@ pub(super) mod encoding { let lcp_end_start = p_end.longest_common_prefix(&reference.paths().start); let lcp_end_end = p_end.longest_common_prefix(ref_end); - let expected_is_path_end_rel_to_start = lcp_end_start.get_component_count() - >= lcp_end_end.get_component_count(); + let expected_is_path_end_rel_to_start = + lcp_end_start.component_count() >= lcp_end_end.component_count(); if expected_is_path_end_rel_to_start != is_path_end_rel_to_start { return Err(DecodeError::InvalidInput);