Skip to content

Commit 894e089

Browse files
committed
chore(sampling): pr review feedback
1 parent b0c652c commit 894e089

2 files changed

Lines changed: 12 additions & 11 deletions

File tree

libdd-sampling/src/bounded_byte_cache.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,12 @@ use std::num::NonZeroUsize;
1616
/// Default maximum entry count.
1717
pub const DEFAULT_MAX_ENTRIES: usize = 256;
1818

19-
/// Default maximum tracked byte size (256 KiB).
20-
pub const DEFAULT_MAX_BYTES: usize = 256 * 1024;
19+
/// Default maximum tracked byte size (64 KiB).
20+
///
21+
/// Under normal use (short keys such as service or resource names), the entry count cap binds
22+
/// first. This byte budget acts as a safety valve against memory exhaustion from unexpectedly
23+
/// large keys due to misconfiguration or adversarial input.
24+
pub const DEFAULT_MAX_BYTES: usize = 256 * 256;
2125

2226
/// LRU cache bounded by both entry count and total tracked byte size.
2327
///
@@ -57,7 +61,6 @@ where
5761

5862
/// Insert `key -> value`. Entries larger than `max_bytes` are dropped silently. Otherwise
5963
/// LRU entries are evicted until the new entry fits.
60-
#[inline]
6164
pub fn put(&mut self, key: K, value: V) {
6265
let entry_bytes = Self::entry_size(&key);
6366

@@ -91,6 +94,7 @@ where
9194
}
9295

9396
#[cfg(test)]
97+
#[inline]
9498
pub fn current_bytes(&self) -> usize {
9599
self.current_bytes
96100
}

libdd-sampling/src/glob_matcher.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ pub struct GlobMatcher {
4343
pattern_is_star: bool,
4444
/// Shared LRU cache of matched subjects, keyed on raw bytes (ASCII path) or lowercased
4545
/// UTF-8 (Unicode path). Only consulted for wildcard patterns. Bounded by byte size to
46-
/// cap memory under arbitrary key sizes.
46+
/// cap memory under arbitrarily large attribute values.
4747
cache: Arc<Mutex<BoundedByteCache<Vec<u8>, bool>>>,
4848
}
4949

@@ -60,15 +60,12 @@ impl GlobMatcher {
6060
/// Creates a new GlobMatcher with the given pattern.
6161
pub fn new(pattern: &str) -> Self {
6262
let pattern_lower = pattern.to_lowercase();
63-
let pattern_is_ascii = pattern_lower.is_ascii();
64-
let pattern_has_wildcards = pattern_lower.contains('*') || pattern_lower.contains('?');
65-
// Any non-empty run of only `*` matches everything (e.g. `*`, `**`, `***`).
66-
let pattern_is_star = !pattern_lower.is_empty() && pattern_lower.bytes().all(|b| b == b'*');
6763
GlobMatcher {
64+
pattern_is_ascii: pattern_lower.is_ascii(),
65+
pattern_has_wildcards: pattern_lower.contains('*') || pattern_lower.contains('?'),
66+
// Any non-empty run of only `*` matches everything (e.g. `*`, `**`, `***`).
67+
pattern_is_star: !pattern_lower.is_empty() && pattern_lower.bytes().all(|b| b == b'*'),
6868
pattern_lower,
69-
pattern_is_ascii,
70-
pattern_has_wildcards,
71-
pattern_is_star,
7269
cache: Arc::new(Mutex::new(BoundedByteCache::new(
7370
DEFAULT_MAX_ENTRIES,
7471
DEFAULT_MAX_BYTES,

0 commit comments

Comments
 (0)