Skip to content

Commit

Permalink
Replace arcstr cache with weak hash set of arc str
Browse files Browse the repository at this point in the history
  • Loading branch information
kxxt committed Jan 7, 2025
1 parent 7b8f8ee commit 8dbfede
Show file tree
Hide file tree
Showing 13 changed files with 273 additions and 61 deletions.
106 changes: 95 additions & 11 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ tui-prompts = "0.5.0"
unicode-segmentation = "1.11.0"
unicode-width = "0.1.12"
serial_test = { version = "3.1.1", features = ["file_locks"] }
arcstr = { version = "1.2.0", features = ["serde"] }
clap_complete = "4.5.2"
regex-cursor = { version = "0.1.4", default-features = false }
shell-words = "1.1.0"
Expand All @@ -78,12 +77,15 @@ libbpf-rs = { version = "0.24.6", optional = true, default-features = false }
# libbpf-sys exists here because we want to control its features
libbpf-sys = { version = "1", optional = true, default-features = false }
libseccomp = { version = "0.3.0", optional = true }
weak-table = { version = "0.3.2", default-features = false, features = ["ahash"] }
rand = "0.8.5"
# tui-prompts = { version = "0.3.11", path = "../../contrib/tui-prompts" }
# tui-popup = { version = "0.3.0", path = "../../contrib/tui-popup" }

[dev-dependencies]
assert_cmd = "2.0.14"
predicates = "3.1.0"

rstest = "0.24.0"
tracing-test = "0.2.4"

Expand Down Expand Up @@ -126,3 +128,7 @@ path = "fixtures/empty-argv.rs"
name = "corrupted-envp"
path = "fixtures/corrupted-envp.rs"


[[bin]]
name = "exec-stress"
path = "fixtures/exec-stress.rs"
33 changes: 33 additions & 0 deletions fixtures/exec-stress.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use std::ffi::CString;

use nix::unistd::execv;
use rand::{distributions::Alphanumeric, Rng};

// A stress test.
// It will exec itself with random strings as arguments for n times
fn main() {
let mut args = std::env::args().skip(1);
let n: u64 = args
.next()
.expect("missing n")
.parse()
.expect("cannot parse n");
if n == 0 {
// nix::sys::signal::raise(nix::sys::signal::SIGSTOP);
return;
}
let mut rand_args = vec![
CString::new("Hello").unwrap(),
CString::new((n - 1).to_string()).unwrap(),
];
rand_args.extend((0..10).map(|_| unsafe {
CString::from_vec_unchecked(
rand::thread_rng()
.sample_iter(&Alphanumeric)
.take(512)
.chain(Some(0))
.collect::<Vec<u8>>(),
)
}));
execv(c"/proc/self/exe", &rand_args).unwrap();
}
11 changes: 5 additions & 6 deletions src/bpf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ use std::{
process,
sync::{
atomic::{AtomicBool, Ordering},
Arc, OnceLock, RwLock,
Arc, LazyLock, RwLock,
},
time::Duration,
};

use arcstr::ArcStr;
use crate::cache::ArcStr;
use color_eyre::{eyre::eyre, Section};
use enumflags2::{BitFlag, BitFlags};
use event::EventStorage;
Expand Down Expand Up @@ -818,11 +818,10 @@ fn utf8_lossy_cow_from_bytes_with_nul(data: &[u8]) -> Cow<str> {
}

fn cached_cow(cow: Cow<str>) -> ArcStr {
let cache = CACHE.get_or_init(|| Arc::new(RwLock::new(StringCache::new())));
match cow {
Cow::Borrowed(s) => cache.write().unwrap().get_or_insert(s),
Cow::Owned(s) => cache.write().unwrap().get_or_insert_owned(s),
Cow::Borrowed(s) => CACHE.write().unwrap().get_or_insert(s),
Cow::Owned(s) => CACHE.write().unwrap().get_or_insert_owned(s),
}
}

static CACHE: OnceLock<Arc<RwLock<StringCache>>> = OnceLock::new();
static CACHE: LazyLock<RwLock<StringCache>> = LazyLock::new(|| RwLock::new(StringCache::new()));
Loading

0 comments on commit 8dbfede

Please sign in to comment.