diff options
author | ringabout <43030857+ringabout@users.noreply.github.com> | 2024-06-21 21:07:45 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-06-21 15:07:45 +0200 |
commit | 2bef08774f00692963f2a00243e4287caa7572ee (patch) | |
tree | ff5c0686c2968338858b681e88119c7ceeb21d45 /lib/system/sysstr.nim | |
parent | 646bd99d461469f08e656f92ae278d6695b35778 (diff) | |
download | Nim-2bef08774f00692963f2a00243e4287caa7572ee.tar.gz |
fixes #23742; setLen(0) no longer allocates memory for uninitialized strs/seqs for refc (#23745)
fixes #23742 Before my PR, `setLen(0)` doesn't free buffer if `s != nil`, but it allocated unnecessary memory for `strs`. This PR rectifies this behavior. `setLen(0)` no longer allocates memory for uninitialized strs/seqs
Diffstat (limited to 'lib/system/sysstr.nim')
-rw-r--r-- | lib/system/sysstr.nim | 10 |
1 files changed, 8 insertions, 2 deletions
diff --git a/lib/system/sysstr.nim b/lib/system/sysstr.nim index e4d6479ec..3621c4960 100644 --- a/lib/system/sysstr.nim +++ b/lib/system/sysstr.nim @@ -220,7 +220,10 @@ proc appendChar(dest: NimString, c: char) {.compilerproc, inline.} = proc setLengthStr(s: NimString, newLen: int): NimString {.compilerRtl.} = let n = max(newLen, 0) if s == nil: - result = mnewString(n) + if n == 0: + return s + else: + result = mnewString(n) elif n <= s.space: result = s else: @@ -301,7 +304,10 @@ proc setLengthSeqV2(s: PGenericSeq, typ: PNimType, newLen: int): PGenericSeq {. compilerRtl.} = sysAssert typ.kind == tySequence, "setLengthSeqV2: type is not a seq" if s == nil: - result = cast[PGenericSeq](newSeq(typ, newLen)) + if newLen == 0: + result = s + else: + result = cast[PGenericSeq](newSeq(typ, newLen)) else: let elemSize = typ.base.size let elemAlign = typ.base.align |