Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixes #18612; apply cache and memcmp for methods in arc/orc #19749

Merged
merged 8 commits into from
Apr 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions compiler/ccgexprs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1615,8 +1615,11 @@ proc genNewFinalize(p: BProc, e: PNode) =

proc genOfHelper(p: BProc; dest: PType; a: Rope; info: TLineInfo): Rope =
if optTinyRtti in p.config.globalOptions:
result = ropecg(p.module, "#isObj($1.m_type, $2)",
[a, genTypeInfo2Name(p.module, dest)])
let ti = genTypeInfo2Name(p.module, dest)
inc p.module.labels
let cache = "Nim_OfCheck_CACHE" & p.module.labels.rope
p.module.s[cfsVars].addf("static TNimTypeV2* $#[2];$n", [cache])
result = ropecg(p.module, "#isObjWithCache($#.m_type, $#, $#)", [a, ti, cache])
else:
# unfortunately 'genTypeInfoV1' sets tfObjHasKids as a side effect, so we
# have to call it here first:
Expand Down
28 changes: 26 additions & 2 deletions lib/system/arc.nim
Original file line number Diff line number Diff line change
Expand Up @@ -227,10 +227,34 @@ template tearDownForeignThreadGc* =
## With `--gc:arc` a nop.
discard

type ObjCheckCache = array[0..1, PNimTypeV2]

proc memcmp(str1, str2: cstring, n: csize_t): cint {.importc, header: "<string.h>".}

func endsWith(s, suffix: cstring): bool {.inline.} =
let
sLen = s.len
suffixLen = suffix.len

if suffixLen <= sLen:
result = memcmp(cstring(addr s[sLen - suffixLen]), suffix, csize_t(suffixLen)) == 0

proc isObj(obj: PNimTypeV2, subclass: cstring): bool {.compilerRtl, inl.} =
proc strstr(s, sub: cstring): cstring {.header: "<string.h>", importc.}
result = endsWith(obj.name, subclass)

result = strstr(obj.name, subclass) != nil
proc isObjSlowPath(obj: PNimTypeV2, subclass: cstring, cache: var ObjCheckCache): bool {.compilerRtl, inline.} =
if endsWith(obj.name, subclass):
cache[1] = obj
result = true
else:
cache[0] = obj
result = false

proc isObjWithCache(obj: PNimTypeV2, subclass: cstring, cache: var ObjCheckCache): bool {.compilerRtl.} =
if cache[0] == obj: result = false
elif cache[1] == obj: result = true
else:
result = isObjSlowPath(obj, subclass, cache)

proc chckObj(obj: PNimTypeV2, subclass: cstring) {.compilerRtl.} =
# checks if obj is of type subclass:
Expand Down
3 changes: 2 additions & 1 deletion tests/metatype/ttypedesc3.nim
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
discard """
output: '''
matrix: "--mm:arc; --mm:refc"
output: '''
proc Base
proc Child
method Base
Expand Down
1 change: 1 addition & 0 deletions tests/method/tgeneric_methods.nim
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
discard """
matrix: "--mm:arc; --mm:refc"
output: '''wow2
X 1
X 3'''
Expand Down
1 change: 1 addition & 0 deletions tests/method/tmethod_issues.nim
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
discard """
matrix: "--mm:arc; --mm:refc"
output: '''
wof!
wof!
Expand Down
1 change: 1 addition & 0 deletions tests/method/tmethod_various.nim
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
discard """
matrix: "--mm:arc; --mm:refc"
output: '''
do nothing
HELLO WORLD!
Expand Down
2 changes: 1 addition & 1 deletion tests/method/tsingle_methods.nim
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
discard """
cmd: "nim c --multimethods:off $file"
matrix: "--mm:arc --multimethods:off; --mm:refc --multimethods:off"
output: '''base
base
base
Expand Down
4 changes: 4 additions & 0 deletions tests/misc/parsecomb.nim
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
discard """
matrix: "--mm:arc; --mm:refc"
"""

type Input[T] = object
toks: seq[T]
index: int
Expand Down
3 changes: 3 additions & 0 deletions tests/pragmas/tlocks.nim
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
discard """
matrix: "--mm:arc; --mm:refc"
"""

type SomeBase* = ref object of RootObj
type SomeDerived* = ref object of SomeBase
Expand Down