Skip to content

Instantly share code, notes, and snippets.

@tesfabpel
Last active January 4, 2025 11:25
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
use ahash::{AHasher, RandomState};
use fxhash::FxBuildHasher;
use std::collections::HashMap;
use std::hint::black_box;
//type Map = HashMap<&'static [u8], &'static [u8]>;
//type Map = HashMap<&'static [u8], &'static [u8], RandomState>;
type Map = HashMap<&'static [u8], &'static [u8], FxBuildHasher>;
pub struct ConfigParser {
config: Map,
}
impl ConfigParser {
pub fn new() -> Self {
ConfigParser {
config: Map::default(),
}
}
#[inline(never)]
pub fn parse_opt(&mut self, input: &'static [u8]) {
self.config.clear();
let cap = self.config.capacity();
if cap < 64 {
self.config.reserve(64 - cap);
}
for line in input.split(|x| *x == b'\n') {
let trimmed = line.trim_ascii();
if trimmed.is_empty() || trimmed.starts_with(&[b'#']) {
continue;
}
let mut segs = trimmed.split(|x| *x == b'=');
let key = segs.next();
let value = segs.next();
let (Some(key), Some(value)) = (key, value) else {
continue;
};
self.config.insert(key, value);
}
}
}
static DATA: &[u8] = br#"
key1=value2
key2=value1
# this is a comment
key3=Hello, World!
"#;
pub fn foo() {
let mut parser = ConfigParser::new();
parser.parse_opt(DATA);
parser.config.clear();
}
#[inline(never)]
pub fn bar(parser: &mut ConfigParser) {
parser.config.clear();
black_box(parser.parse_opt(DATA));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment