Skip to content

Commit

Permalink
add cache
Browse files Browse the repository at this point in the history
  • Loading branch information
ringabout committed Apr 26, 2022
1 parent a53a81f commit 6033093
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
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 NCSTRING $#[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
20 changes: 19 additions & 1 deletion lib/system/arc.nim
Original file line number Diff line number Diff line change
Expand Up @@ -227,8 +227,11 @@ template tearDownForeignThreadGc* =
## With `--gc:arc` a nop.
discard

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

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

func endsWith*(s, suffix: cstring): bool {.inline.} =
proc memcmp(str1, str2: cstring, n: csize_t): cint {.importc, header: "<string.h>".}
if s != nil and suffix != nil:
let
sLen = s.len
Expand All @@ -240,6 +243,21 @@ func endsWith*(s, suffix: cstring): bool {.inline.} =
proc isObj(obj: PNimTypeV2, subclass: cstring): bool {.compilerRtl, inl.} =
result = endsWith(obj.name, subclass)

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

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

proc chckObj(obj: PNimTypeV2, subclass: cstring) {.compilerRtl.} =
# checks if obj is of type subclass:
if not isObj(obj, subclass): sysFatal(ObjectConversionDefect, "invalid object conversion")

0 comments on commit 6033093

Please sign in to comment.