diff --git a/vlib/builtin/map.v b/vlib/builtin/map.v index 85c9cced794e20..358441f1e88a6e 100644 --- a/vlib/builtin/map.v +++ b/vlib/builtin/map.v @@ -249,10 +249,20 @@ fn new_map_1(value_bytes int) map { } fn new_map_init(n int, value_bytes int, keys &string, values voidptr) map { - mut out := new_map_1(value_bytes) + return new_map_init_1(n, value_bytes, keys, values) +} + +fn new_map_init_1(n int, value_bytes int, keys voidptr, values voidptr) map { + mut out := new_map(value_bytes) // TODO pre-allocate n slots - for i in 0 .. n { - unsafe {out.set(keys[i], byteptr(values) + i * value_bytes)} + mut pkey := byteptr(keys) + mut pval := byteptr(values) + for _ in 0 .. n { + unsafe { + out.set_1(pkey, pval) + pkey += out.key_bytes + pval += value_bytes + } } return out }