mirror of
https://github.com/nim-lang/Nim.git
synced 2026-02-17 16:38:33 +00:00
arc optimizations (#13325)
* scope based destructors * handle 'or' and 'and' expressions properly, see the new test arc/tcontrolflow.nim * make this branch mergable, logic is disabled for now
This commit is contained in:
55
tests/arc/tcontrolflow.nim
Normal file
55
tests/arc/tcontrolflow.nim
Normal file
@@ -0,0 +1,55 @@
|
||||
discard """
|
||||
output: '''begin A
|
||||
elif
|
||||
destroyed
|
||||
end A
|
||||
begin false
|
||||
if
|
||||
destroyed
|
||||
end false
|
||||
begin true
|
||||
if
|
||||
end true
|
||||
'''
|
||||
cmd: "nim c --gc:arc -d:danger $file"
|
||||
disabled: "true"
|
||||
"""
|
||||
# we use the -d:danger switch to detect uninitialized stack
|
||||
# slots more reliably (there shouldn't be any, of course).
|
||||
|
||||
# XXX Enable once scope based destruction works!
|
||||
|
||||
type
|
||||
Foo = object
|
||||
id: int
|
||||
|
||||
proc `=destroy`(x: var Foo) =
|
||||
if x.id != 0:
|
||||
echo "destroyed"
|
||||
x.id = 0
|
||||
|
||||
proc construct(): Foo = Foo(id: 3)
|
||||
|
||||
proc elifIsEasy(cond: bool) =
|
||||
echo "begin A"
|
||||
if cond:
|
||||
echo "if"
|
||||
elif construct().id == 3:
|
||||
echo "elif"
|
||||
else:
|
||||
echo "else"
|
||||
echo "end A"
|
||||
|
||||
elifIsEasy(false)
|
||||
|
||||
|
||||
proc orIsHard(cond: bool) =
|
||||
echo "begin ", cond
|
||||
if cond or construct().id == 3:
|
||||
echo "if"
|
||||
else:
|
||||
echo "else"
|
||||
echo "end ", cond
|
||||
|
||||
orIsHard(false)
|
||||
orIsHard(true)
|
||||
@@ -32,7 +32,12 @@ proc serve(server: PAsyncHttpServer): PFutureBase =
|
||||
yield acceptAddrFut
|
||||
var fut = acceptAddrFut.value
|
||||
|
||||
# with the new scope based destruction, this cannot
|
||||
# possibly work:
|
||||
var f {.cursor.} = processClient()
|
||||
# It also seems to be the wrong way how to avoid the
|
||||
# cycle. The cycle is caused by capturing the 'env'
|
||||
# part from 'env.f'.
|
||||
when true:
|
||||
f.callback =
|
||||
proc () =
|
||||
|
||||
@@ -33,7 +33,7 @@ type
|
||||
p: pointer
|
||||
|
||||
proc `=destroy`(o: var TMyObj) =
|
||||
if o.p != nil:
|
||||
if o.p != nil:
|
||||
dealloc o.p
|
||||
o.p = nil
|
||||
echo "myobj destroyed"
|
||||
|
||||
@@ -22,7 +22,7 @@ proc `=`(dest: var Foo, src: Foo) =
|
||||
assign_counter.inc
|
||||
|
||||
proc test(): auto =
|
||||
var a,b : Foo
|
||||
var a, b: Foo
|
||||
return (a, b, Foo(boo: 5))
|
||||
|
||||
var (a, b, _) = test()
|
||||
|
||||
@@ -2,7 +2,7 @@ discard """
|
||||
valgrind: true
|
||||
cmd: '''nim c -d:nimAllocStats --newruntime $file'''
|
||||
output: '''OK 3
|
||||
(allocCount: 8, deallocCount: 3)'''
|
||||
(allocCount: 8, deallocCount: 5)'''
|
||||
"""
|
||||
|
||||
import strutils, math
|
||||
|
||||
Reference in New Issue
Block a user