Skip to content

Commit dde8896

Browse files
committed
Merge remote-tracking branch 'upstream/master' into tag_cardinality_cache_ttl
# Conflicts: # src/transforms/tag_cardinality_limit/mod.rs # src/transforms/tag_cardinality_limit/tag_value_set.rs
2 parents 7aa7efb + c5959d7 commit dde8896

4 files changed

Lines changed: 31 additions & 41 deletions

File tree

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Reduce the memory usage of the `tag_cardinality_limit` transform when running in `exact` mode by allocating less unused memory on initialization.
2+
3+
authors: ArunPiduguDD

src/transforms/tag_cardinality_limit/mod.rs

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -221,12 +221,7 @@ impl TagCardinalityLimit {
221221

222222
let metric_accepted_tags = self.accepted_tags.entry(metric_key_owned).or_default();
223223
let tag_value_set = metric_accepted_tags.entry_ref(key).or_insert_with(|| {
224-
AcceptedTagValueSet::new(
225-
config.value_limit,
226-
&config.mode,
227-
config.ttl_secs,
228-
config.ttl_generations,
229-
)
224+
AcceptedTagValueSet::new(&config.mode, config.ttl_secs, config.ttl_generations)
230225
});
231226

232227
if tag_value_set.contains(value) {
@@ -329,12 +324,7 @@ impl TagCardinalityLimit {
329324
metric_accepted_tags
330325
.entry_ref(key)
331326
.or_insert_with(|| {
332-
AcceptedTagValueSet::new(
333-
config.value_limit,
334-
&config.mode,
335-
config.ttl_secs,
336-
config.ttl_generations,
337-
)
327+
AcceptedTagValueSet::new(&config.mode, config.ttl_secs, config.ttl_generations)
338328
})
339329
.insert(value.clone());
340330
false

src/transforms/tag_cardinality_limit/tag_value_set.rs

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -91,12 +91,12 @@ struct TtlExactStorage {
9191
}
9292

9393
impl TtlExactStorage {
94-
fn new(value_limit: usize, ttl: Duration, generations: u8) -> Self {
94+
fn new(ttl: Duration, generations: u8) -> Self {
9595
let now = Instant::now();
9696
let divisor = generations.max(1) as u32;
9797
let sweep_interval = (ttl / divisor).max(Duration::from_secs(1));
9898
Self {
99-
map: HashMap::with_capacity(value_limit),
99+
map: HashMap::new(),
100100
ttl,
101101
sweep_interval,
102102
last_sweep: now,
@@ -284,23 +284,20 @@ impl fmt::Debug for TagValueSetStorage {
284284
}
285285

286286
impl AcceptedTagValueSet {
287-
/// Construct the appropriate backend.
287+
/// Construct the appropriate backend from `(mode, ttl_secs, ttl_generations)`.
288288
///
289-
/// When `ttl_secs` is `None` (default), the backend is identical to the pre-TTL
290-
/// behavior — `HashSet` for exact, single `BloomFilter` for probabilistic — so
291-
/// existing configs see zero behavioral change.
292-
pub fn new(
293-
value_limit: usize,
294-
mode: &Mode,
295-
ttl_secs: Option<u64>,
296-
ttl_generations: u8,
297-
) -> Self {
289+
/// When `ttl_secs` is `None` or `0`, this is identical to the pre-TTL
290+
/// behavior — `HashSet` for exact, single `BloomFilter` for probabilistic —
291+
/// so existing configs see zero behavioral change. Neither backend
292+
/// pre-allocates with `value_limit` capacity (see vectordotdev/vector#25480
293+
/// for the memory rationale).
294+
pub fn new(mode: &Mode, ttl_secs: Option<u64>, ttl_generations: u8) -> Self {
298295
let ttl = ttl_secs.and_then(|s| (s > 0).then(|| Duration::from_secs(s)));
299296

300297
let storage = match (mode, ttl) {
301-
(Mode::Exact, None) => TagValueSetStorage::Set(HashSet::with_capacity(value_limit)),
298+
(Mode::Exact, None) => TagValueSetStorage::Set(HashSet::new()),
302299
(Mode::Exact, Some(ttl)) => {
303-
TagValueSetStorage::TtlSet(TtlExactStorage::new(value_limit, ttl, ttl_generations))
300+
TagValueSetStorage::TtlSet(TtlExactStorage::new(ttl, ttl_generations))
304301
}
305302
(Mode::Probabilistic(config), None) => {
306303
TagValueSetStorage::Bloom(BloomFilterStorage::new(config.cache_size_per_key))
@@ -392,7 +389,7 @@ mod tests {
392389

393390
#[test]
394391
fn exact_no_ttl_preserves_today_behavior() {
395-
let mut set = AcceptedTagValueSet::new(2, &Mode::Exact, None, 4);
392+
let mut set = AcceptedTagValueSet::new(&Mode::Exact, None, 4);
396393
assert!(!set.contains(&v("a")));
397394
assert_eq!(set.len(), 0);
398395
set.insert(v("a"));
@@ -407,7 +404,7 @@ mod tests {
407404
let mode = Mode::Probabilistic(BloomFilterConfig {
408405
cache_size_per_key: default_cache_size(),
409406
});
410-
let mut set = AcceptedTagValueSet::new(2, &mode, None, 4);
407+
let mut set = AcceptedTagValueSet::new(&mode, None, 4);
411408
set.insert(v("a"));
412409
set.insert(v("a"));
413410
assert_eq!(set.len(), 1, "duplicate insert must not bump count");
@@ -426,7 +423,7 @@ mod tests {
426423
#[test]
427424
fn ttl_exact_expires_values_past_ttl() {
428425
let ttl = Duration::from_secs(60);
429-
let mut s = TtlExactStorage::new(8, ttl, 4);
426+
let mut s = TtlExactStorage::new(ttl, 4);
430427
// t0: insert v=a manually so we control its timestamp.
431428
let t0 = Instant::now();
432429
s.map.insert(v("a"), t0);
@@ -446,7 +443,7 @@ mod tests {
446443
// forward — otherwise sweeps continue to use the old (potentially
447444
// expired) timestamp. A short sleep guarantees `Instant::now()` is
448445
// strictly after `t_insert` on every platform.
449-
let mut s = TtlExactStorage::new(8, Duration::from_secs(60), 4);
446+
let mut s = TtlExactStorage::new(Duration::from_secs(60), 4);
450447
let t_insert = Instant::now();
451448
s.map.insert(v("a"), t_insert);
452449
s.last_sweep = t_insert;
@@ -465,7 +462,7 @@ mod tests {
465462
fn ttl_exact_sweep_interval_floors_to_one_second() {
466463
// ttl=2s, generations=8 → naive slice = 250ms; we floor to 1s so sweeps
467464
// never become dominant. Verify the floor.
468-
let s = TtlExactStorage::new(8, Duration::from_secs(2), 8);
465+
let s = TtlExactStorage::new(Duration::from_secs(2), 8);
469466
assert!(s.sweep_interval >= Duration::from_secs(1));
470467
}
471468

@@ -474,7 +471,7 @@ mod tests {
474471
// Regression for the `DropEvent` pre-check bug: a read-only check
475472
// must NOT update the stored Instant.
476473
let ttl = Duration::from_secs(60);
477-
let mut s = TtlExactStorage::new(8, ttl, 4);
474+
let mut s = TtlExactStorage::new(ttl, 4);
478475
let t0 = Instant::now();
479476
s.map.insert(v("a"), t0);
480477
s.last_sweep = t0;

src/transforms/tag_cardinality_limit/tests.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1513,19 +1513,19 @@ fn contains_no_refresh_finds_inserted_values_on_all_backends() {
15131513
for (label, mut set) in [
15141514
(
15151515
"exact no-ttl",
1516-
AcceptedTagValueSet::new(4, &Mode::Exact, None, 4),
1516+
AcceptedTagValueSet::new(&Mode::Exact, None, 4),
15171517
),
15181518
(
15191519
"bloom no-ttl",
1520-
AcceptedTagValueSet::new(4, &bloom_mode, None, 4),
1520+
AcceptedTagValueSet::new(&bloom_mode, None, 4),
15211521
),
15221522
(
15231523
"exact ttl",
1524-
AcceptedTagValueSet::new(4, &Mode::Exact, Some(60), 4),
1524+
AcceptedTagValueSet::new(&Mode::Exact, Some(60), 4),
15251525
),
15261526
(
15271527
"bloom ttl",
1528-
AcceptedTagValueSet::new(4, &bloom_mode, Some(60), 4),
1528+
AcceptedTagValueSet::new(&bloom_mode, Some(60), 4),
15291529
),
15301530
] {
15311531
set.insert(v1.clone());
@@ -1551,19 +1551,19 @@ fn ttl_zero_disables_ttl() {
15511551
for (label, set) in [
15521552
(
15531553
"exact ttl=0",
1554-
AcceptedTagValueSet::new(4, &Mode::Exact, Some(0), 4),
1554+
AcceptedTagValueSet::new(&Mode::Exact, Some(0), 4),
15551555
),
15561556
(
15571557
"bloom ttl=0",
1558-
AcceptedTagValueSet::new(4, &bloom_mode, Some(0), 4),
1558+
AcceptedTagValueSet::new(&bloom_mode, Some(0), 4),
15591559
),
15601560
(
15611561
"exact ttl=None",
1562-
AcceptedTagValueSet::new(4, &Mode::Exact, None, 4),
1562+
AcceptedTagValueSet::new(&Mode::Exact, None, 4),
15631563
),
15641564
(
15651565
"bloom ttl=None",
1566-
AcceptedTagValueSet::new(4, &bloom_mode, None, 4),
1566+
AcceptedTagValueSet::new(&bloom_mode, None, 4),
15671567
),
15681568
] {
15691569
assert!(
@@ -1573,7 +1573,7 @@ fn ttl_zero_disables_ttl() {
15731573
}
15741574

15751575
// Sanity: TTL with a positive value DOES select the TTL backend.
1576-
let ttl_set = AcceptedTagValueSet::new(4, &Mode::Exact, Some(60), 4);
1576+
let ttl_set = AcceptedTagValueSet::new(&Mode::Exact, Some(60), 4);
15771577
assert!(ttl_set.ttl_enabled(), "ttl=Some(60) should enable TTL");
15781578
}
15791579

0 commit comments

Comments
 (0)