Declaring an uninitialized seq & avoid unnecessary zero-mem's

2023-05-13 Thread Araq
Depends on the use case, when you build up the custom seq directly there would be no copying.

Declaring an uninitialized seq & avoid unnecessary zero-mem's

2023-05-13 Thread arnetheduck
> No, these only mean that you need to use a custom seq. But I agree it's not > ideal. all other idiomatic nim code uses a seq -> one would have to copy from custom seq to seq -> defeats the purpose of avoiding zero:ing.

Declaring an uninitialized seq & avoid unnecessary zero-mem's

2023-05-13 Thread Araq
> these prevent writing code that is possible to write efficiently in other > languages. No, these only mean that you need to use a custom `seq`. But I agree it's not ideal.

Declaring an uninitialized seq & avoid unnecessary zero-mem's

2023-05-13 Thread arnetheduck
See also and \- these prevent writing code that is possible to write efficiently in other languages.

Declaring an uninitialized seq & avoid unnecessary zero-mem's

2023-05-12 Thread Yardanico
To have a seq that has already allocated len and cap, but is not zero-allocated, in case you will assign all elements yourself by an index, for example.

Declaring an uninitialized seq & avoid unnecessary zero-mem's

2023-05-12 Thread juancarlospaco
But then whats the reason of `newSeqUninitialized` to exist.

Declaring an uninitialized seq & avoid unnecessary zero-mem's

2023-05-12 Thread juancarlospaco
To not read/write `nil`/garbage.

Declaring an uninitialized seq & avoid unnecessary zero-mem's

2023-05-12 Thread Yardanico
Maybe you have mistaken `newSeqOfCap` for `newSeq`? `newSeqOfCap` doesn't allow you to access the elements, since the capacity is allocated, but length is still 0. You have to add elements to the sequence, so they will never be garbage, so it makes total sense not to zero-allocate data for

Declaring an uninitialized seq & avoid unnecessary zero-mem's

2023-05-12 Thread AmjadBHD
Here's a [PR](https://github.com/nim-lang/Nim/pull/21842), but it feels missing.

Declaring an uninitialized seq & avoid unnecessary zero-mem's

2023-05-12 Thread AmjadBHD
Why should `newSeqOfCap` be zeroed ?

Declaring an uninitialized seq & avoid unnecessary zero-mem's

2023-05-12 Thread juancarlospaco
`newSeqOfCap` should be zeroed, `newSeqUninitialized` should not.

Declaring an uninitialized seq & avoid unnecessary zero-mem's

2023-05-12 Thread Araq
I don't know, just patch it cleanly so that it works.

Declaring an uninitialized seq & avoid unnecessary zero-mem's

2023-05-12 Thread AmjadBHD
Should I change it [here](https://github.com/nim-lang/Nim/tree/devel/compiler/ccgexprs.nim#L1497) ? Then `newSeqOfCap` would not initialize the memory, which IMHO is logical.

Declaring an uninitialized seq & avoid unnecessary zero-mem's

2023-05-11 Thread Araq
Nah, instead create a `newSeqPayloadUninitialized` which `newSeqUninitialized` calls.

Declaring an uninitialized seq & avoid unnecessary zero-mem's

2023-05-11 Thread AmjadBHD
Both `newSeq` and `newSeqUninitialized` produce the same `seq` and the same [asm code](https://godbolt.org/z/PTTEf361n) . They both call `newSeqPayload` which calls [alignedAlloc0](https://github.com/nim-lang/Nim/tree/devel/lib/system/seqs_v2.nim#L38). Should a `initialize` parameter be added

Declaring an uninitialized seq & avoid unnecessary zero-mem's

2022-10-24 Thread ElegantBeef
Yea it likely could be relaxed to something like import std/typetraits type StackType* = concept type ST supportsCopyMem(ST) Run

Declaring an uninitialized seq & avoid unnecessary zero-mem's

2022-10-24 Thread drkameleon
Thanks a lot for the answer and the explanation! Extremely helpful and thorough as usual! :)

Declaring an uninitialized seq & avoid unnecessary zero-mem's

2022-10-24 Thread cblake
Possibly of interest is `newSeqNoInit` at the end of My use case was wanting to do it for `seq[bool]` in which was annoyingly not included in `SomeNumber`, but is very much plain old data /

Declaring an uninitialized seq & avoid unnecessary zero-mem's

2022-10-24 Thread mratsim
The issue with ref objects is that destructors assume an uninitialized ref is set to 0. The `=copy` and `=move` procs will have a logic similar to: proc `=`(dst: var MyObj, src: MyObj) = # Nim refcounting starts at 0 if not dst.isNil: if dst.refcount == 0:

Declaring an uninitialized seq & avoid unnecessary zero-mem's

2022-10-24 Thread drkameleon
My problem with this is: > Only available for numbers types. So, I'm wondering what the safest solution would be in my case...

Declaring an uninitialized seq & avoid unnecessary zero-mem's

2022-10-24 Thread drkameleon
Let's say I want to declare a seq of ref objects (which I'm about to fill in the next few lines of code - so, we _know_ the array will be filled). However, if I do it like this, the array is unnecessarily initialized and zeroMem/memset: var myArr: seq[someRefObject] # doSth

Declaring an uninitialized seq & avoid unnecessary zero-mem's

2022-10-24 Thread auxym
This maybe?