Skip to content

Commit 5c96140

Browse files
committed
impl FromStr
1 parent 4653e12 commit 5c96140

3 files changed

Lines changed: 32 additions & 67 deletions

File tree

README.md

Lines changed: 9 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -53,63 +53,17 @@ ptr --> <var int length> <data>
5353

5454
![string_memory](https://github.com/user-attachments/assets/25f5acf8-9a3e-4a4c-b2f1-b2fb972cc9c8)
5555

56-
## Measured from System Memory
56+
## Memory Usage Comparison (RSS per String)
5757

58-
### 0..=4
59-
```text,ignore
60-
Crate, len 0..=4 | RSS (B) | Virtual (B)
61-
-------------------|--------------|-------------
62-
std | 36.9 | 38.4
63-
smol_str | 24.0 | 24.0
64-
compact_str | 24.0 | 24.0
65-
compact_string | 24.1 | 26.2
66-
cold-string | 8.0 | 8.0
67-
```
68-
69-
### 0..=8
70-
```text,ignore
71-
Crate, len 0..=8 | RSS (B) | Virtual (B)
72-
-------------------|--------------|-------------
73-
std | 38.4 | 40.0
74-
smol_str | 24.0 | 24.0
75-
compact_str | 24.0 | 24.0
76-
compact_string | 25.8 | 27.8
77-
cold-string | 11.2 | 11.7
78-
```
79-
80-
### 0..=16
81-
```text,ignore
82-
Crate, len 0..=16 | RSS (B) | Virtual (B)
83-
-------------------|--------------|-------------
84-
std | 46.8 | 48.6
85-
smol_str | 24.0 | 24.1
86-
compact_str | 24.0 | 24.0
87-
compact_string | 32.6 | 34.9
88-
cold-string | 24.9 | 26.7
89-
```
90-
91-
### 0..=32
92-
```text,ignore
93-
Crate, len 0..=32 | RSS (B) | Virtual (B)
94-
-------------------|--------------|-------------
95-
std | 55.3 | 57.4
96-
smol_str | 41.1 | 42.1
97-
compact_str | 35.4 | 36.6
98-
compact_string | 40.5 | 42.9
99-
cold-string | 36.5 | 38.8
100-
```
101-
102-
### 0..=64
103-
```text,ignore
104-
Crate, len 0..=64 | RSS (B) | Virtual (B)
105-
-------------------|--------------|-------------
106-
std | 71.4 | 73.7
107-
smol_str | 72.2 | 74.3
108-
compact_str | 61.0 | 63.3
109-
compact_string | 56.5 | 59.1
110-
cold-string | 53.5 | 56.3
111-
```
58+
| Crate | 0–4 chars | 0–8 chars | 0–16 chars | 0–32 chars | 0–64 chars |
59+
| :--- | :---: | :---: | :---: | :---: | :---: |
60+
| `std` | 36.9 B | 38.4 B | 46.8 B | 55.3 B | 71.4 B |
61+
| `smol_str` | 24.0 B | 24.0 B | 24.0 B | 41.1 B | 72.2 B |
62+
| `compact_str` | 24.0 B | 24.0 B | 24.0 B | 35.4 B | 61.0 B |
63+
| `compact_string` | 24.1 B | 25.8 B | 32.6 B | 40.5 B | 56.5 B |
64+
| **`cold-string`** | **8.0 B** | **11.2 B** | **24.9 B** | **36.5 B** | **53.5 B** |
11265

66+
**Note:** Columns represent string length (bytes/chars). Values represent average Resident Set Size (RSS) in bytes per string instance. Measurements taken with 10M iterations.
11367

11468
## License
11569

src/lib.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,13 @@ impl AsRef<[u8]> for ColdString {
373373
}
374374
}
375375

376+
impl alloc::str::FromStr for ColdString {
377+
type Err = core::convert::Infallible;
378+
fn from_str(s: &str) -> Result<ColdString, Self::Err> {
379+
Ok(ColdString::new(s))
380+
}
381+
}
382+
376383
#[cfg(feature = "serde")]
377384
impl serde::Serialize for ColdString {
378385
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {

tests/memory.rs

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
#![allow(unsafe_op_in_unsafe_fn)]
22

3+
use std::alloc::{GlobalAlloc, Layout, System};
4+
use std::fmt::Debug;
35
use std::io::Write;
4-
use std::iter::repeat_with;
6+
use std::str::FromStr;
7+
use std::sync::atomic::{AtomicUsize, Ordering};
58
use std::time::Instant;
69
use sysinfo::Pid;
710

8-
use std::alloc::{GlobalAlloc, Layout, System};
9-
use std::sync::atomic::{AtomicUsize, Ordering};
10-
1111
#[global_allocator]
1212
static A: CountingAlloc = CountingAlloc;
1313

@@ -27,18 +27,22 @@ unsafe impl GlobalAlloc for CountingAlloc {
2727
}
2828
}
2929

30-
fn random_string<T: TryFrom<String>>(min: usize, max: usize) -> T
30+
fn random_string<T: FromStr>(min: usize, max: usize) -> T
3131
where
32-
<T as TryFrom<String>>::Error: std::fmt::Debug,
32+
<T as FromStr>::Err: Debug,
3333
{
3434
let len = fastrand::usize(min..=max);
35-
let s: String = repeat_with(fastrand::alphanumeric).take(len).collect();
36-
s.try_into().unwrap()
35+
let mut scratch = [0u8; 128];
36+
for i in 0..len {
37+
scratch[i] = fastrand::alphanumeric() as u8;
38+
}
39+
let s = unsafe { std::str::from_utf8_unchecked(&scratch[..len]) };
40+
s.parse().unwrap()
3741
}
3842

39-
fn allocator_memory<T: TryFrom<String>>(name: &str)
43+
fn allocator_memory<T: FromStr>(name: &str)
4044
where
41-
<T as TryFrom<String>>::Error: std::fmt::Debug,
45+
<T as FromStr>::Err: Debug,
4246
{
4347
const SIZE: usize = 48;
4448
const TRIALS: usize = 100;
@@ -69,9 +73,9 @@ where
6973
);
7074
}
7175

72-
fn system_memory<T: TryFrom<String>>(name: &str, min: usize, max: usize)
76+
fn system_memory<T: FromStr>(name: &str, min: usize, max: usize)
7377
where
74-
<T as TryFrom<String>>::Error: std::fmt::Debug,
78+
<T as FromStr>::Err: Debug,
7579
{
7680
let mut sys = sysinfo::System::new_all();
7781
let pid = Pid::from(std::process::id() as usize);

0 commit comments

Comments
 (0)