Make newSeqOfCap not initialize memory. (#21842)

It's used in `newSeqUninitialized`.

---------

Co-authored-by: ringabout <43030857+ringabout@users.noreply.github.com>
This commit is contained in:
Amjad Ben Hedhili
2023-09-09 20:11:45 +01:00
committed by GitHub
parent 5717a4843d
commit 8853fb0775
4 changed files with 13 additions and 4 deletions

View File

@@ -1463,7 +1463,7 @@ proc genNewSeqOfCap(p: BProc; e: PNode; d: var TLoc) =
var a: TLoc = initLocExpr(p, e[1])
if optSeqDestructors in p.config.globalOptions:
if d.k == locNone: d = getTemp(p, e.typ, needsInit=false)
linefmt(p, cpsStmts, "$1.len = 0; $1.p = ($4*) #newSeqPayload($2, sizeof($3), NIM_ALIGNOF($3));$n",
linefmt(p, cpsStmts, "$1.len = 0; $1.p = ($4*) #newSeqPayloadUninit($2, sizeof($3), NIM_ALIGNOF($3));$n",
[d.rdLoc, a.rdLoc, getTypeDesc(p.module, seqtype.lastSon),
getSeqPayloadType(p.module, seqtype),
])

View File

@@ -925,7 +925,7 @@ proc reset*[T](obj: var T) {.noSideEffect.} =
obj = default(typeof(obj))
proc setLen*[T](s: var seq[T], newlen: Natural) {.
magic: "SetLengthSeq", noSideEffect.}
magic: "SetLengthSeq", noSideEffect, nodestroy.}
## Sets the length of seq `s` to `newlen`. `T` may be any sequence type.
##
## If the current length is greater than the new length,

View File

@@ -109,7 +109,7 @@ proc newSeqRC1(typ: PNimType, len: int): pointer {.compilerRtl.} =
writebarrierptr(addr(result), newSeq(typ, len))
proc nimNewSeqOfCap(typ: PNimType, cap: int): pointer {.compilerproc.} =
result = newObj(typ, align(GenericSeqSize, typ.base.align) + cap * typ.base.size)
result = newObjNoInit(typ, align(GenericSeqSize, typ.base.align) + cap * typ.base.size)
cast[PGenericSeq](result).len = 0
cast[PGenericSeq](result).reserved = cap
cast[PGenericSeq](result).elemSize = typ.base.size

View File

@@ -50,6 +50,15 @@ proc newSeqPayload(cap, elemSize, elemAlign: int): pointer {.compilerRtl, raises
else:
result = nil
proc newSeqPayloadUninit(cap, elemSize, elemAlign: int): pointer {.compilerRtl, raises: [].} =
# Used in `newSeqOfCap()`.
if cap > 0:
var p = cast[ptr NimSeqPayloadBase](alignedAlloc(align(sizeof(NimSeqPayloadBase), elemAlign) + cap * elemSize, elemAlign))
p.cap = cap
result = p
else:
result = nil
template `+!`(p: pointer, s: int): pointer =
cast[pointer](cast[int](p) +% s)
@@ -125,7 +134,7 @@ proc add*[T](x: var seq[T]; y: sink T) {.magic: "AppendSeqElem", noSideEffect, n
# We also save the `wasMoved + destroy` pair for the sink parameter.
xu.p.data[oldLen] = y
proc setLen[T](s: var seq[T], newlen: Natural) =
proc setLen[T](s: var seq[T], newlen: Natural) {.nodestroy.} =
{.noSideEffect.}:
if newlen < s.len:
shrink(s, newlen)