Skip to content

Conversation

@GZTimeWalker
Copy link

@GZTimeWalker GZTimeWalker commented Jul 6, 2025

Motivation

  1. When we store a inline string, the len will never reach 255, so we can store it in u8.
  2. As we store the size in u8, we can store much more bytes inline (38 on 64-bit, 30 on 32-bit).
  3. The enum tag takes u8, the length takes u8, so we got (max_size - 2) bytes to store the inline string.

I see #24, but it not support 32-bit, and did not update comments, so here is another pr.

Solution

// target_pointer_width = 64, other fields take up to 40B
#[cfg(target_pointer_width = "64")]
const INLINE_CAP: usize = 38;

// target_pointer_width = 32, other fields take up to 32B
#[cfg(target_pointer_width = "32")]
const INLINE_CAP: usize = 30;

// target_pointer_width = 16, we don't support this architecture

#[derive(Clone)]
enum Repr {
    Empty,
    Bytes(Bytes),
    ArcStr(Arc<str>),
    ArcString(Arc<String>),
    StaticStr(&'static str),
    Inline { len: u8, buf: [u8; INLINE_CAP] },
}

About other changes

  1. Make clippy v1.88 and typos happy.
  2. Upgrade criterion used in benches.

Notes

If there are more performance considerations related to pointer alignment, please let me know so that we can conduct more tests and verifications.

@CLAassistant
Copy link

CLAassistant commented Jul 6, 2025

CLA assistant check
All committers have signed the CLA.

@GZTimeWalker GZTimeWalker force-pushed the feat-make-inline-longer branch from a61b2d1 to d0e5326 Compare July 6, 2025 16:59
@GZTimeWalker
Copy link
Author

GZTimeWalker commented Jul 6, 2025

Bench Result

  • Darwin 24.5.0 Darwin Kernel Version 24.5.0
  • Apple M1 Pro

Before change

# FastStr::new("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");  // "a" * 38
long faststr            time:   [10.213 ns 10.246 ns 10.283 ns]
---
empty faststr           time:   [1.5530 ns 1.5574 ns 1.5621 ns]
empty string            time:   [2.1865 ns 2.1923 ns 2.1990 ns]
static faststr          time:   [2.8190 ns 2.9203 ns 3.0163 ns]
inline faststr          time:   [2.7831 ns 2.8797 ns 2.9760 ns]

After change

# FastStr::new("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"); // "a" * 38
inline long faststr     time:   [3.1674 ns 3.2213 ns 3.2705 ns]
---
empty faststr           time:   [1.5622 ns 1.5637 ns 1.5657 ns]
empty string            time:   [2.1805 ns 2.1845 ns 2.1890 ns]
static faststr          time:   [2.6851 ns 2.7970 ns 2.9098 ns]
inline faststr          time:   [3.2228 ns 3.2574 ns 3.2879 ns]
string hello world      time:   [20.622 ns 20.689 ns 20.765 ns]

@GZTimeWalker
Copy link
Author

I see this 342bdc9

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants