From d7a60642ce843d3a45f1f52a6d315f8189bccb9f Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Wed, 11 Mar 2026 12:17:45 +0300 Subject: [PATCH] builtin: fil-C complains about the test map_generic_call_test.v (fixes #25757) --- vlib/builtin/map.v | 12 +++++++++--- vlib/builtin/map_test.v | 14 ++++++++++++++ vlib/v/tests/builtin_maps/map_generic_call_test.v | 1 - 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/vlib/builtin/map.v b/vlib/builtin/map.v index 6b3c2f1f9..55704b869 100644 --- a/vlib/builtin/map.v +++ b/vlib/builtin/map.v @@ -516,11 +516,10 @@ fn (mut m map) expand() { // the max_load_factor in an operation. fn (mut m map) rehash() { meta_bytes := sizeof(u32) * (m.even_index + 2 + m.extra_metas) - m.reserve(meta_bytes) + m.reserve_metas(meta_bytes) } -// reserve memory for the map meta data. -pub fn (mut m map) reserve(meta_bytes u32) { +fn (mut m map) reserve_metas(meta_bytes u32) { unsafe { // TODO: use realloc_data here too x := v_realloc(byteptr(m.metas), int(meta_bytes)) @@ -538,6 +537,13 @@ pub fn (mut m map) reserve(meta_bytes u32) { } } +// reserve ensures that the map can store at least `n` entries without rehashing. +pub fn (mut m map) reserve(n u32) { + for u64(n) * 5 > u64(m.even_index) * 2 { + m.expand() + } +} + // cached_rehashd works like rehash. However, instead of rehashing the // key completely, it uses the bits cached in `metas`. fn (mut m map) cached_rehash(old_cap u32) { diff --git a/vlib/builtin/map_test.v b/vlib/builtin/map_test.v index 538f6eb2c..ffb1a4407 100644 --- a/vlib/builtin/map_test.v +++ b/vlib/builtin/map_test.v @@ -566,6 +566,20 @@ fn test_map_clone() { assert nums2['bar'] == 8 } +fn test_map_reserve_keeps_empty_map_valid() { + mut m := { + 'abc': 42 + } + mut moved := m.move() + moved.clear() + moved.reserve(6) + moved.delete('def') + assert moved.keys().len == 0 + assert moved.values().len == 0 + assert moved.clone().len == 0 + unsafe { moved.free() } +} + struct MValue { name string misc map[string]string diff --git a/vlib/v/tests/builtin_maps/map_generic_call_test.v b/vlib/v/tests/builtin_maps/map_generic_call_test.v index 919003857..61731dc60 100644 --- a/vlib/v/tests/builtin_maps/map_generic_call_test.v +++ b/vlib/v/tests/builtin_maps/map_generic_call_test.v @@ -1,4 +1,3 @@ -// vtest build: !sanitize-address-clang-without-gc struct FooParams[K, V] { k K v V -- 2.39.5