ARC: optimize complete object constructors to use nimNewObjUninit

This commit is contained in:
Araq 2020-01-26 10:50:21 +01:00 • committed by Andreas Rumpf
commit a10cc18247
5 changed files with 40 additions and 12 deletions

View file

@ -72,6 +72,23 @@ proc nimNewObj(size: int): pointer {.compilerRtl.} =
when traceCollector:
cprintf("[Allocated] %p\n", result -! sizeof(RefHeader))
proc nimNewObjUninit(size: int): pointer {.compilerRtl.} =
# Same as 'newNewObj' but do not initialize the memory to zero.
# The codegen proved for us that this is not necessary.
let s = size + sizeof(RefHeader)
when defined(nimscript):
discard
elif defined(useMalloc):
var orig = cast[ptr RefHeader](c_malloc(cuint s))
elif compileOption("threads"):
var orig = cast[ptr RefHeader](allocShared(s))
else:
var orig = cast[ptr RefHeader](alloc(s))
orig.rc = 0
result = orig +! sizeof(RefHeader)
when traceCollector:
cprintf("[Allocated] %p\n", result -! sizeof(RefHeader))
proc nimDecWeakRef(p: pointer) {.compilerRtl, inl.} =
dec head(p).rc, rcIncrement