mojo
mojo copied to clipboard
[Stdlib] Add `Dict.__init__` overload with `power_of_two_initial_capacity`
Hello, here is a new __init__
that let user specify the initial reservation.
Skipping the un-necessary growth phases when the final size is known to be big.
2.2x speedup on insertion, lookup should be faster aswel (during overcapacity).
1056082202 PR 35184367894528
2388001454 35184367894528
from time import now
def main():
var dict_size = 1<<23
var start = now()
var stop = now()
start = now()
x = Dict[Int,Int](power_of_two_initial_capacity = 1<<24)
for i in range(dict_size):
x[i] = i
stop = now()
print(stop-start)
var result = 0
for i in range(len(x)):
result+=x[i]
print(result)
We started with 24 and not 23 because the dict grows at 2/3 of capacity, but reserving 23 is a huge speedup too.
💡 Could this ameliorate variadic keyword arguments ? If mojo knows there are 8 arguments passed, maybe reserve 16 (and skip the dict growth phases).
@gabrieldemarmiesse suggested a power_of_two
type in a PR,
thought about it too during this one ! :+1: