/flamegraph.svg Secret
Last active
January 4, 2025 11:25
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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