From b5a854c7c76ee99018b45e8ea2a61c2036678d7d Mon Sep 17 00:00:00 2001 From: yhx-12243 Date: Sun, 10 Mar 2024 15:39:24 -0400 Subject: [PATCH 1/4] perf & style: needless slicing after c95edd5 --- compact_str/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compact_str/src/lib.rs b/compact_str/src/lib.rs index d1f6a544..3c232768 100644 --- a/compact_str/src/lib.rs +++ b/compact_str/src/lib.rs @@ -614,7 +614,7 @@ impl CompactString { /// ``` #[inline] pub fn as_bytes(&self) -> &[u8] { - &self.0.as_slice()[..self.len()] + self.0.as_slice() } // TODO: Implement a `try_as_mut_slice(...)` that will fail if it results in cloning? From cde527c68d2ddd7309493ab912168faddd950faf Mon Sep 17 00:00:00 2001 From: yhx-12243 Date: Sun, 10 Mar 2024 15:39:45 -0400 Subject: [PATCH 2/4] perf: is_heap is always false on 64-bit --- compact_str/src/repr/capacity.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/compact_str/src/repr/capacity.rs b/compact_str/src/repr/capacity.rs index 242ac46c..49cb8fc2 100644 --- a/compact_str/src/repr/capacity.rs +++ b/compact_str/src/repr/capacity.rs @@ -101,7 +101,13 @@ impl Capacity { /// stored on the heap #[inline(always)] pub(crate) fn is_heap(self) -> bool { - self == CAPACITY_IS_ON_THE_HEAP + cfg_if::cfg_if! { + if #[cfg(target_pointer_width = "64")] { + false + } else { + self == CAPACITY_IS_ON_THE_HEAP + } + } } } From 6fff6b97f54c3e7c564a53c40246923ed968b8fe Mon Sep 17 00:00:00 2001 From: yhx-12243 Date: Sun, 10 Mar 2024 15:48:38 -0400 Subject: [PATCH 3/4] fix: pass clippy check --- compact_str/src/repr/capacity.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/compact_str/src/repr/capacity.rs b/compact_str/src/repr/capacity.rs index 49cb8fc2..68a3ce7f 100644 --- a/compact_str/src/repr/capacity.rs +++ b/compact_str/src/repr/capacity.rs @@ -23,6 +23,7 @@ const HEAP_MARKER: usize = { /// /// All bytes `255`, with the last being [`LastByte::Heap`], using the same amount of bytes /// as `usize`. Example (64-bit): `[255, 255, 255, 255, 255, 255, 255, 216]` +#[cfg(not(target_pointer_width = "64"))] const CAPACITY_IS_ON_THE_HEAP: Capacity = Capacity(VALID_MASK | HEAP_MARKER); /// The maximum value we're able to store, e.g. on 64-bit arch this is 2^56 - 2. From d0f71d40e9ef624aa0a40980356203b75dae4a7e Mon Sep 17 00:00:00 2001 From: yhx-12243 Date: Sun, 10 Mar 2024 17:15:39 -0400 Subject: [PATCH 4/4] fix: wrap the pointer in a black_box --- compact_str/src/tests.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compact_str/src/tests.rs b/compact_str/src/tests.rs index 1a15c71c..3b0a7f86 100644 --- a/compact_str/src/tests.rs +++ b/compact_str/src/tests.rs @@ -1937,7 +1937,7 @@ fn test_from_string_buffer_inlines_on_clone() { #[should_panic = "Cannot allocate memory to hold CompactString"] fn test_alloc_excessively_long_string() { // 2**56 - 2 bytes, the maximum number `Capacity` can hold - CompactString::with_capacity((1 << 56) - 2); + std::hint::black_box(CompactString::with_capacity((1 << 56) - 2)); } // This feature was enabled by which was first