Reuse exact hash input for shared macro meanings
This commit is contained in:
1 parent
c4003c8dad
commit
ed105bdd39
3 files changed
+324
-90
No files matched your search
@@ -0,0 +1,195 @@
|
||||
use super::{Macro, Meaning};
|
||||
use std::{
|
||||
collections::HashMap,
|
||||
fmt,
|
||||
hash::{DefaultHasher, Hash, Hasher},
|
||||
sync::{Arc, Weak},
|
||||
};
|
||||
|
||||
// Batch the many small token and integer writes in a state fingerprint.
|
||||
// The ordered bytes still go through the native state fingerprint's hasher.
|
||||
pub(super) struct FingerprintHasher {
|
||||
hasher: DefaultHasher,
|
||||
buffer: [u8; 4096],
|
||||
used: usize,
|
||||
}
|
||||
|
||||
impl Default for FingerprintHasher {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
hasher: DefaultHasher::new(),
|
||||
buffer: [0; 4096],
|
||||
used: 0,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl FingerprintHasher {
|
||||
#[inline]
|
||||
fn write_fixed<const N: usize>(&mut self, bytes: [u8; N]) {
|
||||
if N > self.buffer.len() - self.used {
|
||||
self.hasher.write(&self.buffer[..self.used]);
|
||||
self.used = 0;
|
||||
}
|
||||
let end = self.used + N;
|
||||
self.buffer[self.used..end].copy_from_slice(&bytes);
|
||||
self.used = end;
|
||||
}
|
||||
}
|
||||
|
||||
impl Hasher for FingerprintHasher {
|
||||
#[inline]
|
||||
fn write_u8(&mut self, value: u8) {
|
||||
self.write_fixed(value.to_ne_bytes());
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn write_u16(&mut self, value: u16) {
|
||||
self.write_fixed(value.to_ne_bytes());
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn write_u32(&mut self, value: u32) {
|
||||
self.write_fixed(value.to_ne_bytes());
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn write_u64(&mut self, value: u64) {
|
||||
self.write_fixed(value.to_ne_bytes());
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn write_usize(&mut self, value: usize) {
|
||||
self.write_fixed(value.to_ne_bytes());
|
||||
}
|
||||
|
||||
fn write(&mut self, bytes: &[u8]) {
|
||||
if bytes.len() > self.buffer.len() - self.used {
|
||||
self.hasher.write(&self.buffer[..self.used]);
|
||||
self.used = 0;
|
||||
}
|
||||
if bytes.len() > self.buffer.len() {
|
||||
self.hasher.write(bytes);
|
||||
} else {
|
||||
let end = self.used + bytes.len();
|
||||
self.buffer[self.used..end].copy_from_slice(bytes);
|
||||
self.used = end;
|
||||
}
|
||||
}
|
||||
|
||||
fn finish(&self) -> u64 {
|
||||
let mut hasher = self.hasher.clone();
|
||||
hasher.write(&self.buffer[..self.used]);
|
||||
hasher.finish()
|
||||
}
|
||||
}
|
||||
|
||||
const MAX_CACHED_BYTES: usize = 16 * 1024 * 1024;
|
||||
const MAX_CACHED_MACROS: usize = 8192;
|
||||
const MAX_RECORDED_BYTES: usize = 64 * 1024;
|
||||
const MIN_REPLACEMENT_TOKENS: usize = 8;
|
||||
|
||||
struct MacroInput {
|
||||
// Keep the allocation address reserved while its bytes are cached,
|
||||
// without keeping the definition or its token storage alive.
|
||||
body: Weak<Macro>,
|
||||
bytes: Option<Box<[u8]>>,
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
pub(super) struct MacroFingerprintInputs {
|
||||
inputs: HashMap<usize, MacroInput>,
|
||||
bytes: usize,
|
||||
}
|
||||
|
||||
impl fmt::Debug for MacroFingerprintInputs {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
f.debug_struct("MacroFingerprintInputs")
|
||||
.field("entries", &self.inputs.len())
|
||||
.field("bytes", &self.bytes)
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
|
||||
impl MacroFingerprintInputs {
|
||||
fn full(&self) -> bool {
|
||||
self.bytes >= MAX_CACHED_BYTES || self.inputs.len() >= MAX_CACHED_MACROS
|
||||
}
|
||||
|
||||
pub(super) fn prune_if_full(&mut self) {
|
||||
if self.full() {
|
||||
self.inputs.retain(|_, input| {
|
||||
let live = input.body.strong_count() > 0;
|
||||
if !live {
|
||||
self.bytes -= input.bytes.as_deref().map_or(0, <[u8]>::len);
|
||||
}
|
||||
live
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn hash_meaning(&mut self, meaning: &Meaning, hasher: &mut FingerprintHasher) {
|
||||
let Meaning::Macro(body) = meaning else {
|
||||
meaning.hash(hasher);
|
||||
return;
|
||||
};
|
||||
if body.replacement.len() < MIN_REPLACEMENT_TOKENS {
|
||||
meaning.hash(hasher);
|
||||
return;
|
||||
}
|
||||
let key = Arc::as_ptr(body) as usize;
|
||||
if let Some(input) = self.inputs.get(&key) {
|
||||
if let Some(bytes) = &input.bytes {
|
||||
hasher.write(bytes);
|
||||
} else {
|
||||
meaning.hash(hasher);
|
||||
}
|
||||
return;
|
||||
}
|
||||
if self.full() {
|
||||
meaning.hash(hasher);
|
||||
return;
|
||||
}
|
||||
|
||||
// Record the complete Meaning hash, including its enum tag. Replay
|
||||
// only to FingerprintHasher, whose integer, length, and string writes
|
||||
// consume the same ordered bytes as the recorder's default methods.
|
||||
let mut record = HashInput::default();
|
||||
meaning.hash(&mut record);
|
||||
let mut bytes = None;
|
||||
if record.overflow {
|
||||
meaning.hash(hasher);
|
||||
} else {
|
||||
hasher.write(&record.bytes);
|
||||
if record.bytes.len() <= MAX_CACHED_BYTES - self.bytes {
|
||||
self.bytes += record.bytes.len();
|
||||
bytes = Some(record.bytes.into_boxed_slice());
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
}
|
||||
self.inputs.insert(key, MacroInput { body: Arc::downgrade(body), bytes });
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
struct HashInput {
|
||||
bytes: Vec<u8>,
|
||||
overflow: bool,
|
||||
}
|
||||
|
||||
impl Hasher for HashInput {
|
||||
fn write(&mut self, bytes: &[u8]) {
|
||||
if !self.overflow {
|
||||
if bytes.len() > MAX_RECORDED_BYTES - self.bytes.len() {
|
||||
self.overflow = true;
|
||||
} else {
|
||||
self.bytes.extend_from_slice(bytes);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn finish(&self) -> u64 {
|
||||
unreachable!("recording a macro writes hash input without finishing a digest")
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user