Skip to content

Commit 4ef06a5

Browse files
ringaboutAraq
andauthored
fixes cast expressions introduces unnecessary copies (#24004)
It speeds up ```nim proc foo = let piece = cast[seq[char]](newSeqUninit[uint8](5220600386'i64)) foo() ``` Notes that `cast[ref](...)` is excluded because we need to keep the ref alive if the parameter is something with pointer types (e.g. `cast[ref](pointer)`or `cast[ref](makePointer(...))`) --------- Co-authored-by: Andreas Rumpf <rumpf_a@web.de>
1 parent 446501b commit 4ef06a5

File tree

3 files changed

+21
-44
lines changed

3 files changed

+21
-44
lines changed

compiler/injectdestructors.nim

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,7 @@ proc containsConstSeq(n: PNode): bool =
501501
return true
502502
result = false
503503
case n.kind
504-
of nkExprEqExpr, nkExprColonExpr, nkHiddenStdConv, nkHiddenSubConv:
504+
of nkExprEqExpr, nkExprColonExpr, nkHiddenStdConv, nkHiddenSubConv, nkCast:
505505
result = containsConstSeq(n[1])
506506
of nkObjConstr, nkClosure:
507507
for i in 1..<n.len:
@@ -829,6 +829,9 @@ proc p(n: PNode; c: var Con; s: var Scope; mode: ProcessMode; tmpFlags = {sfSing
829829
elif n.kind in {nkObjDownConv, nkObjUpConv}:
830830
result = copyTree(n)
831831
result[0] = p(n[0], c, s, sinkArg)
832+
elif n.kind == nkCast and n.typ.skipTypes(abstractInst).kind in {tyString, tySequence}:
833+
result = copyTree(n)
834+
result[1] = p(n[1], c, s, sinkArg)
832835
elif n.typ == nil:
833836
# 'raise X' can be part of a 'case' expression. Deal with it here:
834837
result = p(n, c, s, normal)

tests/destructor/tatomicptrs.nim

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,11 +143,13 @@ proc `=sink`*[T](m: var MySeq[T], m2: MySeq[T]) {.inline.} =
143143
`=destroy`(m)
144144
m.len = m2.len
145145
m.data = m2.data
146+
m.refcount = m2.refcount
146147

147148
proc len*[T](m: MySeq[T]): int {.inline.} = m.len
148149

149150
proc newMySeq*[T](size: int, initial_value: T): MySeq[T] =
150151
result.len = size
152+
result.refcount = 1
151153
if size > 0:
152154
result.data = cast[ptr UncheckedArray[T]](allocShared(sizeof(T) * size))
153155

tests/destructor/tv2_cast.nim

Lines changed: 15 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -10,69 +10,41 @@ destroying O1'''
1010
var
1111
data
1212
:tmpD
13-
:tmpD_1
14-
:tmpD_2
15-
data =
16-
:tmpD = `=dup`(cast[string](
17-
:tmpD_2 = encode(cast[seq[byte]](
18-
:tmpD_1 = newString(100)
19-
:tmpD_1))
20-
:tmpD_2))
21-
:tmpD
22-
`=destroy`(:tmpD_2)
23-
`=destroy_1`(:tmpD_1)
24-
`=destroy_1`(data)
13+
data = cast[string](encode(cast[seq[byte]](
14+
:tmpD = newString(100)
15+
:tmpD)))
16+
`=destroy`(:tmpD)
17+
`=destroy`(data)
2518
-- end of expandArc ------------------------
2619
--expandArc: main1
2720
2821
var
2922
s
3023
data
31-
:tmpD
32-
:tmpD_1
3324
s = newString(100)
34-
data =
35-
:tmpD = `=dup`(cast[string](
36-
:tmpD_1 = encode(toOpenArrayByte(s, 0, len(s) - 1))
37-
:tmpD_1))
38-
:tmpD
39-
`=destroy`(:tmpD_1)
40-
`=destroy_1`(data)
41-
`=destroy_1`(s)
25+
data = cast[string](encode(toOpenArrayByte(s, 0, len(s) - 1)))
26+
`=destroy`(data)
27+
`=destroy`(s)
4228
-- end of expandArc ------------------------
4329
--expandArc: main2
4430
4531
var
4632
s
4733
data
48-
:tmpD
49-
:tmpD_1
5034
s = newSeq(100)
51-
data =
52-
:tmpD = `=dup`(cast[string](
53-
:tmpD_1 = encode(s)
54-
:tmpD_1))
55-
:tmpD
56-
`=destroy`(:tmpD_1)
57-
`=destroy_1`(data)
58-
`=destroy`(s)
35+
data = cast[string](encode(s))
36+
`=destroy`(data)
37+
`=destroy_1`(s)
5938
-- end of expandArc ------------------------
6039
--expandArc: main3
6140
6241
var
6342
data
6443
:tmpD
65-
:tmpD_1
66-
:tmpD_2
67-
data =
68-
:tmpD = `=dup`(cast[string](
69-
:tmpD_2 = encode do:
70-
:tmpD_1 = newSeq(100)
71-
:tmpD_1
72-
:tmpD_2))
73-
:tmpD
74-
`=destroy`(:tmpD_2)
75-
`=destroy`(:tmpD_1)
44+
data = cast[string](encode do:
45+
:tmpD = newSeq(100)
46+
:tmpD)
47+
`=destroy`(:tmpD)
7648
`=destroy_1`(data)
7749
-- end of expandArc ------------------------
7850
'''

0 commit comments

Comments
 (0)