This commit is contained in:
Mateusz Gruszczyński committed 2026-10-10 08:53:55 +02:00
1 parent 1431dd47f0
commit f3dd34dae4
42 files changed
+950 -133

No files matched your search

+9 -6
View File
@@ -51,13 +51,16 @@ impl TopTable {
if raw.is_empty() || weight == 0 {
return;
}
// Probe with the borrowed label. Under production traffic most methods,
// statuses, routes and user agents repeat; allocating a fresh String
// for every hit creates avoidable allocator churn.
let value = truncate(raw, MAX_LABEL_BYTES);
if let Some(count) = self.values.get_mut(&value) {
if let Some(count) = self.values.get_mut(value) {
*count = count.saturating_add(weight);
return;
}
if self.values.len() < TOP_CAPACITY {
self.values.insert(value, weight);
self.values.insert(value.to_owned(), weight);
return;
}
let Some((smallest, minimum)) = self
@@ -69,7 +72,7 @@ impl TopTable {
return;
};
self.values.remove(&smallest);
self.values.insert(value, minimum.saturating_add(weight));
self.values.insert(value.to_owned(), minimum.saturating_add(weight));
}
fn merge(&mut self, other: &Self) {
for (value, count) in &other.values {
@@ -337,15 +340,15 @@ fn user_agent_family(value: &str) -> &'static str {
}
}
fn truncate(value: &str, max_bytes: usize) -> String {
fn truncate(value: &str, max_bytes: usize) -> &str {
if value.len() <= max_bytes {
return value.to_owned();
return value;
}
let mut end = max_bytes;
while !value.is_char_boundary(end) {
end -= 1;
}
value[..end].to_owned()
&value[..end]
}
#[cfg(test)]