In my quest for speed I want to create a new seq but setting it to all zeros is not necessary because I will be filling it in the next for loop anyways. I want t see if its faster if it just starts with garbage.
You can do this for arrays and pointers etc...
a {.noInit.}: array [0..1023, char]
But how to do that for a plain seq? I don't want to change all of my code to some other seq just to see if this micro optimization is helpful.
Thank you for your response!
Based on the source code of for the proc above I made this:
proc newSeqNoInit*[T](len: Natural): seq[T] =
## Creates a new sequence of type ``seq[T]`` with length ``len``.
## Skips initialization of memory to zero.
result = newSeqOfCap[T](len)
when defined(nimSeqsV2):
cast[ptr int](addr result)[] = len
else:
type GenericSeq = ref object
len, reserved: int
var s = cast[GenericSeq](result)
s.len = len
I think it will be fine if I only use this for basic non ref types that don't contain pointers or ref types.
This give me small but measurable performance boost of about 1-3%.
I need to optimize more things before this becomes a problem. Then I'll look at this some more.