tamer: xir::escape::CachingEscaper: Use new sym::st::ST_COUNT

This adds a constant `ST_COUNT` representing the number of statically
allocated symbols, and uses that to estimate an initial capacity for the
`CachingEscaper`.

This is just a guess (and is certainly too low), but we can adjust later on
after profiling, if it ever comes up.
main
Mike Gerwitz 2021-11-15 21:40:42 -05:00
parent d710437ee4
commit 8723ca154d
2 changed files with 34 additions and 6 deletions

View File

@ -221,7 +221,13 @@ macro_rules! static_symbol_consts {
};
// Terminating condition.
(@i $i:expr; <$size:ty>) => {}
(@i $i:expr; <$size:ty>) => {
/// Number of statically allocated symbols.
///
/// This can be used to help determine a base capacity for
/// collections holding [`SymbolId`]s.
pub const ST_COUNT: usize = $i - 1;
}
}
/// Statically allocate [`SymbolId`]s for the provided symbols,
@ -606,4 +612,15 @@ mod test {
fn decimal1_gt_9_panics() {
st::decimal1(10);
}
#[test]
fn st_count_matches_actual_count() {
// This assumes that static symbols begin at 1 and end at
// END_STATIC.
assert_eq!(
st::END_STATIC.as_usize(),
st::ST_COUNT,
"st::ST_COUNT does not match the number of static symbols"
);
}
}

View File

@ -57,8 +57,8 @@
use fxhash::FxHashMap;
use crate::sym::{
GlobalSymbolInternBytes, GlobalSymbolInternUnchecked, GlobalSymbolResolve,
SymbolId,
st::ST_COUNT, GlobalSymbolInternBytes, GlobalSymbolInternUnchecked,
GlobalSymbolResolve, SymbolId,
};
use std::{borrow::Cow, cell::RefCell, collections::hash_map::Entry};
@ -205,11 +205,22 @@ pub struct CachingEscaper<S: Escaper> {
impl<S: Escaper> CachingEscaper<S> {
pub fn new(inner: S) -> Self {
// TODO: Capacity that is at least double the length of the static
// symbols.
// We know we'll encounter more than the statically allocated
// symbols,
// given that we'll be reading parsing XML documents.
// This can be adjusted as needed after profiling.
let capacity = ST_COUNT * 2;
Self {
inner,
..Default::default()
toesc: RefCell::new(FxHashMap::with_capacity_and_hasher(
capacity,
Default::default(),
)),
tounesc: RefCell::new(FxHashMap::with_capacity_and_hasher(
capacity,
Default::default(),
)),
}
}