Nim正在转向更有效的内存管理模型:ARC和ORC。让我们确切地了解它们将如何改变内存的工作方式。
介绍
你好!在本文中,我将尝试解释ARC和ORC是什么以及它们如何影响性能或Nim的其他部分。我不会深入探讨软件部分的各个方面,但会尝试给出或多或少的高级解释。
让我们从远处开始:Nim一直是垃圾收集器(GC)语言。当然,可以关闭GC,但是当使用大多数标准库(而且很大)时,内存将泄漏。
GC Nim refc
( mark & sweep ), , markAndSweep
, boehm
, go
, regions
.
Nim' , , (owned ref) :
, Nim ARC
ARC?
ARC , (Automatic Reference Counting) (move semantics). , ARC Nim , ARC Swift, — Nim ARC .
. ( runtime) , . , .
ARC GC Nim , ARC — (, , , .), . ARC C++ (RAII)
, , ARC expandArc
, Nim 1.4.
Nim:
proc main =
let mystr = stdin.readLine()
case mystr
of "":
echo "!"
of "":
echo "!"
quit()
else:
discard
main()
nim c --gc:arc --expandArc:main example.nim
.
--expandArc: main
var mystr
try:
mystr = readLine(stdin)
case mystr
of "":
echo ["!"]
of "":
echo ["!"]
quit(0)
else:
discard
finally:
`=destroy`(mystr)
-- end of expandArc ------------------------
— Nim main
try: finally
( finally
, try
) =destroy
mystr
, .
ARC: (scope-based MM). — . , , , . Nim' : , , , , block
, for
while
.
ARC hooks — , , // . , , , FFI.
refc
ARC ( ):
( ) — .
— , .
—
refc
, (thread-local heap), ARC . — . RFC , ARC.
FFI — ,
refc
"" (.. ) , ARC. , ARC , (.dll, .so, Python' )
-
(copy elision), Nim (cursor inference) — ()
, ARC Nim , , , .
, ARC , , , --gc:arc
, (.nims
.cfg
).
! - ? ARC , , , . — , , . : 3 (A, B, C), , :
, , — , , .
Nim' mark & sweep refc
GC, ARC - . :
ORC — Nim
ORC , ARC. GC, (local tracing phase) GC, (global tracing).
async Nim ORC, Nim' , .
ORC ARC () — ORC , hard realtime ( ) — .
ORC --gc:orc
, , ORC GC Nim'
! ?
— — Nim 1.4. .
! — , !
/ :