Skip to content

Commit c9f31cd

Browse files
authored
Some slightly performance & style updates (#370)
* perf & style: needless slicing after c95edd5 * perf: is_heap is always false on 64-bit * fix: pass clippy check * fix: wrap the pointer in a black_box
1 parent 0e48d4a commit c9f31cd

File tree

3 files changed

+10
-3
lines changed

3 files changed

+10
-3
lines changed

compact_str/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -614,7 +614,7 @@ impl CompactString {
614614
/// ```
615615
#[inline]
616616
pub fn as_bytes(&self) -> &[u8] {
617-
&self.0.as_slice()[..self.len()]
617+
self.0.as_slice()
618618
}
619619

620620
// TODO: Implement a `try_as_mut_slice(...)` that will fail if it results in cloning?

compact_str/src/repr/capacity.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ const HEAP_MARKER: usize = {
2323
///
2424
/// All bytes `255`, with the last being [`LastByte::Heap`], using the same amount of bytes
2525
/// as `usize`. Example (64-bit): `[255, 255, 255, 255, 255, 255, 255, 216]`
26+
#[cfg(not(target_pointer_width = "64"))]
2627
const CAPACITY_IS_ON_THE_HEAP: Capacity = Capacity(VALID_MASK | HEAP_MARKER);
2728

2829
/// The maximum value we're able to store, e.g. on 64-bit arch this is 2^56 - 2.
@@ -101,7 +102,13 @@ impl Capacity {
101102
/// stored on the heap
102103
#[inline(always)]
103104
pub(crate) fn is_heap(self) -> bool {
104-
self == CAPACITY_IS_ON_THE_HEAP
105+
cfg_if::cfg_if! {
106+
if #[cfg(target_pointer_width = "64")] {
107+
false
108+
} else {
109+
self == CAPACITY_IS_ON_THE_HEAP
110+
}
111+
}
105112
}
106113
}
107114

compact_str/src/tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1972,7 +1972,7 @@ fn test_from_string_buffer_inlines_on_clone() {
19721972
#[should_panic = "Cannot allocate memory to hold CompactString"]
19731973
fn test_alloc_excessively_long_string() {
19741974
// 2**56 - 2 bytes, the maximum number `Capacity` can hold
1975-
CompactString::with_capacity((1 << 56) - 2);
1975+
std::hint::black_box(CompactString::with_capacity((1 << 56) - 2));
19761976
}
19771977

19781978
// This feature was enabled by <https://github.com/rust-lang/rust/pull/94075> which was first

0 commit comments

Comments
 (0)