From 318b2cfc5ebeee5e27982bb5a76fa4c00d523772 Mon Sep 17 00:00:00 2001 From: heterodoxic <122719743+heterodoxic@users.noreply.github.com> Date: Thu, 18 Apr 2024 21:58:01 +0200 Subject: [PATCH] =?UTF-8?q?allow=20having=20{.noinit.}=20on=20a=20complex?= =?UTF-8?q?=20type=20avoid=20memsets=20to=200=20for=20its=20=E2=80=A6=20(#?= =?UTF-8?q?23388)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit …instantiations (C/C++ backend) AFAIK, #22802 expanded `noinit`'s utility by allowing the pragma to be attached to types (thanks @jmgomez !). I suggest broadening the scope a bit further: try to avoid `nimZeroMem`s on a type level beyond imported C/C++ types[^1], saving us from annotating the type instantiations with `noinit`. If this change is deemed acceptable, I will also adjust the docs, of course. Adding tests for this change seems a bit problematic, as the effect of this type annotation will be to work with uninitialized memory, which *might* match 0 patterns. [^1]: "complex value types" as already defined here: https://github.com/nim-lang/Nim/blob/94c599687796f4ee3872c8aa866827b9ed33f52b/compiler/cgen.nim#L470-L471 --- compiler/ccgtypes.nim | 3 +++ compiler/cgen.nim | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/compiler/ccgtypes.nim b/compiler/ccgtypes.nim index 3590a17bcb..1366caab30 100644 --- a/compiler/ccgtypes.nim +++ b/compiler/ccgtypes.nim @@ -245,6 +245,9 @@ proc isImportedCppType(t: PType): bool = proc isOrHasImportedCppType(typ: PType): bool = searchTypeFor(typ.skipTypes({tyRef}), isImportedCppType) +proc hasNoInit(t: PType): bool = + result = t.sym != nil and sfNoInit in t.sym.flags + proc getTypeDescAux(m: BModule; origTyp: PType, check: var IntSet; kind: TypeDescKind): Rope proc isObjLackingTypeField(typ: PType): bool {.inline.} = diff --git a/compiler/cgen.nim b/compiler/cgen.nim index 043c014a21..e4ba416145 100644 --- a/compiler/cgen.nim +++ b/compiler/cgen.nim @@ -532,7 +532,7 @@ proc constructLoc(p: BProc, loc: var TLoc, isTemp = false) = linefmt(p, cpsStmts, "$1 = ($2)0;$n", [rdLoc(loc), getTypeDesc(p.module, typ, descKindFromSymKind mapTypeChooser(loc))]) else: - if not isTemp or containsGarbageCollectedRef(loc.t): + if (not isTemp or containsGarbageCollectedRef(loc.t)) and not hasNoInit(loc.t): # don't use nimZeroMem for temporary values for performance if we can # avoid it: if not isOrHasImportedCppType(typ):