I posted a solution a while back to a similar problem. The way I tried it is by allocating the array with an initial size BLOCK_SIZE
, and then I keep growing it by BLOCK_SIZE
as needed (whenever there's less than 10%*BLOCK_SIZE
free slots).
Note that with an adequate block size, performance is comparable to pre-allocating the entire array from the beginning. Please see the other post for a simple benchmark I did.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…