Skip to content

Commit fcf4f10

Browse files
ringaboutnarimiran
authored andcommitted
fixes #19728; setLen slow when shrinking seq due to zero-filling of released area (#24683)
fixes #19728 don't zero-filling memory for "trivial types" without destructor in refc. I tested locally with internal apis. (cherry picked from commit b421d0f)
1 parent 5394c68 commit fcf4f10

File tree

3 files changed

+40
-4
lines changed

3 files changed

+40
-4
lines changed

compiler/ccgexprs.nim

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1925,6 +1925,13 @@ proc genArrayLen(p: BProc, e: PNode, d: var TLoc, op: TMagic) =
19251925
else: putIntoDest(p, d, e, rope(lengthOrd(p.config, typ)))
19261926
else: internalError(p.config, e.info, "genArrayLen()")
19271927

1928+
proc isTrivialTypesToSnippet(t: PType): Rope =
1929+
if containsGarbageCollectedRef(t) or
1930+
hasDestructor(t):
1931+
result = rope"NIM_FALSE"
1932+
else:
1933+
result = rope"NIM_TRUE"
1934+
19281935
proc genSetLengthSeq(p: BProc, e: PNode, d: var TLoc) =
19291936
if optSeqDestructors in p.config.globalOptions:
19301937
e[1] = makeAddr(e[1], p.module.idgen)
@@ -1945,10 +1952,11 @@ proc genSetLengthSeq(p: BProc, e: PNode, d: var TLoc) =
19451952
genTypeInfoV1(p.module, t.skipTypes(abstractInst), e.info)])
19461953

19471954
else:
1948-
const setLenPattern = "($3) #setLengthSeqV2($1, $4, $2)"
1955+
const setLenPattern = "($3) #setLengthSeqV2($1, $4, $2, $5)"
19491956
call.snippet = ropecg(p.module, setLenPattern, [
19501957
rdLoc(a), rdLoc(b), getTypeDesc(p.module, t),
1951-
genTypeInfoV1(p.module, t.skipTypes(abstractInst), e.info)])
1958+
genTypeInfoV1(p.module, t.skipTypes(abstractInst), e.info),
1959+
isTrivialTypesToSnippet(t.skipTypes(abstractInst)[0])])
19521960

19531961
genAssignment(p, a, call, {})
19541962
gcUsage(p.config, e)

lib/system/sysstr.nim

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ proc setLengthSeq(seq: PGenericSeq, elemSize, elemAlign, newLen: int): PGenericS
300300
zeroMem(dataPointer(result, elemAlign, elemSize, newLen), (result.len-%newLen) *% elemSize)
301301
result.len = newLen
302302

303-
proc setLengthSeqV2(s: PGenericSeq, typ: PNimType, newLen: int): PGenericSeq {.
303+
proc setLengthSeqV2(s: PGenericSeq, typ: PNimType, newLen: int, isTrivial: bool): PGenericSeq {.
304304
compilerRtl.} =
305305
sysAssert typ.kind == tySequence, "setLengthSeqV2: type is not a seq"
306306
if s == nil:
@@ -334,7 +334,8 @@ proc setLengthSeqV2(s: PGenericSeq, typ: PNimType, newLen: int): PGenericSeq {.
334334
# presence of user defined destructors, the user will expect the cell to be
335335
# "destroyed" thus creating the same problem. We can destroy the cell in the
336336
# finalizer of the sequence, but this makes destruction non-deterministic.
337-
zeroMem(dataPointer(result, elemAlign, elemSize, newLen), (result.len-%newLen) *% elemSize)
337+
if not isTrivial: # optimization for trivial types
338+
zeroMem(dataPointer(result, elemAlign, elemSize, newLen), (result.len-%newLen) *% elemSize)
338339
else:
339340
result = s
340341
zeroMem(dataPointer(result, elemAlign, elemSize, result.len), (newLen-%result.len) *% elemSize)

tests/stdlib/tmisc_issues.nim

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,30 @@ block: # bug #16771
3737
a.foo b
3838
doAssert a.n == 42
3939
doAssert b.n == 1
40+
41+
block: # bug #24683
42+
block:
43+
var v = newSeq[int](100)
44+
v[99]= 444
45+
v.setLen(5)
46+
v.setLen(100)
47+
doAssert v[99] == 0
48+
49+
when not defined(js):
50+
block:
51+
var
52+
x = @[1, 2, 3, 4, 45, 56, 67, 999, 88, 777]
53+
54+
x.setLen(0) # zero-fills 1mb of released data
55+
56+
type
57+
TGenericSeq = object
58+
len, reserved: int
59+
PGenericSeq = ptr TGenericSeq
60+
61+
when defined(gcRefc):
62+
cast[PGenericSeq](x).len = 10
63+
else:
64+
cast[ptr int](addr x)[] = 10
65+
66+
doAssert x == @[1, 2, 3, 4, 45, 56, 67, 999, 88, 777]

0 commit comments

Comments
 (0)