use std::fmt;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[non_exhaustive]
pub enum Budget {
Delimiters,
Angles,
PrefixOperators,
ClosureBars,
ElseIf,
Recursion,
ModuleDepth,
ModulePaths,
DirectoryDepth,
FileTokens,
TotalBytes,
ItemBytes,
FactBytes,
GlobBytes,
Work,
}
const MIB: usize = 1024 * 1024;
const BUDGETS: [(Budget, usize); 15] = [
(Budget::Delimiters, 128),
(Budget::Angles, 64),
(Budget::PrefixOperators, 256),
(Budget::ClosureBars, 128),
(Budget::ElseIf, 256),
(Budget::Recursion, RECURSION),
(Budget::ModuleDepth, 128),
(Budget::ModulePaths, 262_144),
(Budget::DirectoryDepth, 128),
(Budget::FileTokens, FILE_TOKENS),
(Budget::TotalBytes, 256 * MIB),
(Budget::ItemBytes, 512 * MIB),
(Budget::FactBytes, 256 * MIB),
(Budget::GlobBytes, 512 * MIB),
(Budget::Work, WORK),
];
pub const PARSE_BYTES_PER_TOKEN: usize = 1664;
pub const SYNTAX_BYTES_PER_NODE: usize = 978;
pub const SYNTAX_BYTES_PER_BYTE_NUM: usize = 417;
pub const SYNTAX_BYTES_PER_BYTE_DEN: usize = 100;
pub const RECURSION_UNIT_BYTES: usize = 17 * 1024;
const RECURSION: usize = crate::parse::PARSE_STACK_BYTES / (2 * RECURSION_UNIT_BYTES);
const FILE_TOKENS: usize = 2_000_000;
const WORK: usize = 1 << 31;
impl Budget {
pub const ALL: [Budget; 15] = [
BUDGETS[0].0,
BUDGETS[1].0,
BUDGETS[2].0,
BUDGETS[3].0,
BUDGETS[4].0,
BUDGETS[5].0,
BUDGETS[6].0,
BUDGETS[7].0,
BUDGETS[8].0,
BUDGETS[9].0,
BUDGETS[10].0,
BUDGETS[11].0,
BUDGETS[12].0,
BUDGETS[13].0,
BUDGETS[14].0,
];
#[must_use]
pub const fn limit(self) -> usize {
BUDGETS[self as usize].1
}
#[must_use]
pub const fn name(self) -> &'static str {
match self {
Self::Delimiters => "delimiter nesting",
Self::Angles => "nested `<`",
Self::PrefixOperators => "prefix operator run",
Self::ClosureBars => "closure bar chain",
Self::ElseIf => "`else if` chain",
Self::Recursion => "parser recursion",
Self::ModuleDepth => "module nesting",
Self::ModulePaths => "module paths",
Self::DirectoryDepth => "directory nesting",
Self::FileTokens => "tokens in one file",
Self::TotalBytes => "bytes read in one load",
Self::ItemBytes => "bytes of index records",
Self::FactBytes => "bytes of file facts",
Self::GlobBytes => "bytes of glob tables",
Self::Work => "glob table work steps",
}
}
}
impl fmt::Display for Budget {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.name())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn the_table_is_indexed_by_each_budgets_position() {
for (index, (budget, _)) in BUDGETS.iter().enumerate() {
assert_eq!(*budget as usize, index);
}
assert_eq!(Budget::ALL.len(), BUDGETS.len());
assert_eq!(Budget::Delimiters.limit(), 128);
assert_eq!(Budget::Angles.limit(), 64);
assert_eq!(Budget::PrefixOperators.limit(), 256);
assert_eq!(Budget::ClosureBars.limit(), 128);
assert_eq!(Budget::ElseIf.limit(), 256);
assert_eq!(Budget::Recursion.limit(), 1927);
assert_eq!(
RECURSION,
crate::PARSE_STACK_BYTES / (2 * RECURSION_UNIT_BYTES)
);
assert_eq!(Budget::ModuleDepth.limit(), 128);
assert_eq!(Budget::ModulePaths.limit(), 262_144);
assert_eq!(Budget::DirectoryDepth.limit(), 128);
assert_eq!(Budget::FileTokens.limit(), FILE_TOKENS);
assert_eq!(Budget::TotalBytes.limit(), 256 * MIB);
assert_eq!(Budget::ItemBytes.limit(), 512 * MIB);
assert_eq!(Budget::FactBytes.limit(), 256 * MIB);
assert_eq!(Budget::GlobBytes.limit(), 512 * MIB);
assert_eq!(Budget::Work.limit(), 1 << 31);
assert_eq!(Budget::Work.name(), "glob table work steps");
assert_eq!(Budget::ALL.last(), Some(&Budget::Work));
}
}