Skip to content

Commit

Permalink
Fast compile mode improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Nycto committed Dec 19, 2024
1 parent 97d1176 commit 24eb55f
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 29 deletions.
14 changes: 11 additions & 3 deletions src/necsus/compiletime/attachDetachGen.nim
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,6 @@ proc attachDetachProcBody(
): tuple[procBody: NimNode, convertProcs: NimNode] =
## Generates the logic needed to attach and detach components from an existing entity

if isFastCompileMode(fastAttachDetach):
return (newStmtList(), newStmtList())

result.convertProcs = newStmtList()

let toRemove = asBits(detachComps, optDetachComps)
Expand Down Expand Up @@ -158,6 +155,10 @@ proc attachFields(name: string, dir: TupleDirective): seq[WorldField] =

proc generateAttach(details: GenerateContext, arg: SystemArg, name: string, attach: TupleDirective): NimNode =
## Generates the code for instantiating queries

if isFastCompileMode(fastAttachDetach):
return newEmptyNode()

let attachProc = details.globalName(name)
let componentTuple = attach.args.asTupleType

Expand Down Expand Up @@ -207,6 +208,9 @@ proc detachFields(name: string, dir: TupleDirective): seq[WorldField] =
proc generateDetach(details: GenerateContext, arg: SystemArg, name: string, detach: TupleDirective): NimNode =
## Generates the code for instantiating queries

if isFastCompileMode(fastAttachDetach):
return newEmptyNode()

let detachProc = details.globalName(name)

case details.hook
Expand Down Expand Up @@ -237,6 +241,10 @@ let detachGenerator* {.compileTime.} = newGenerator(

proc generateSwap(details: GenerateContext, arg: SystemArg, name: string, dir: DualDirective): NimNode =
## Generates the code for instantiating queries

if isFastCompileMode(fastAttachDetach):
return newEmptyNode()

let swapProc = details.globalName(name)
let componentTuple = dir.first.asTupleType

Expand Down
8 changes: 3 additions & 5 deletions src/necsus/compiletime/converters.nim
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ else:
proc buildConverter*(convert: ConverterDef): NimNode =
## Builds a single converter proc
let sig = convert.signature
if sig in built:
if sig in built or isFastCompileMode(fastConverters):
return newStmtList()

let name = convert.name
Expand All @@ -110,10 +110,8 @@ proc buildConverter*(convert: ConverterDef): NimNode =
convert.adding.asTupleType
let outputTuple = convert.output.asTupleType

let body = if isFastCompileMode(fastConverters):
newStmtList()
else:
let copier = copyTuple(convert.input, convert.adding, convert.output)
let copier = copyTuple(convert.input, convert.adding, convert.output)
let body =
if convert.sinkParams:
quote:
`copier`
Expand Down
3 changes: 3 additions & 0 deletions src/necsus/compiletime/deleteGen.nim
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ proc deleteAllBody(details: GenerateContext, dir: TupleDirective): NimNode =

proc generateDeleteAll(details: GenerateContext, arg: SystemArg, name: string, dir: TupleDirective): NimNode =

if isFastCompileMode(fastDeleteGen):
return newEmptyNode()

let deleteAllImpl = details.globalName(name)

case details.hook
Expand Down
19 changes: 10 additions & 9 deletions src/necsus/compiletime/lookupGen.nim
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ proc converters(ctx: GenerateContext, dir: TupleDirective): seq[ConverterDef] =

proc generate(details: GenerateContext, arg: SystemArg, name: string, lookup: TupleDirective): NimNode =
## Generates the code for instantiating queries
if isFastCompileMode(fastLookup):
return newEmptyNode()

let lookupProc = details.globalName(name)
let tupleType = lookup.args.toSeq.asTupleType
Expand All @@ -44,17 +46,16 @@ proc generate(details: GenerateContext, arg: SystemArg, name: string, lookup: Tu
let appStateTypeName = details.appStateTypeName

var cases: NimNode = newEmptyNode()
if not isFastCompileMode(fastLookup):
if details.archetypes.len > 0:
cases = nnkCaseStmt.newTree(newDotExpr(entityIndex, ident("archetype")))
if details.archetypes.len > 0:
cases = nnkCaseStmt.newTree(newDotExpr(entityIndex, ident("archetype")))

# Create a case statement where each branch is one of the archetypes
for (ofBranch, archetype) in archetypeCases(details):
if archetype.matches(lookup.filter):
cases.add(nnkOfBranch.newTree(ofBranch, details.buildArchetypeLookup(lookup, archetype)))
# Create a case statement where each branch is one of the archetypes
for (ofBranch, archetype) in archetypeCases(details):
if archetype.matches(lookup.filter):
cases.add(nnkOfBranch.newTree(ofBranch, details.buildArchetypeLookup(lookup, archetype)))

# Add a fall through 'else' branch for any archetypes that don't fit this lookup
cases.add(nnkElse.newTree(nnkReturnStmt.newTree(newLit(false))))
# Add a fall through 'else' branch for any archetypes that don't fit this lookup
cases.add(nnkElse.newTree(nnkReturnStmt.newTree(newLit(false))))

return quote:
proc `lookupProc`(
Expand Down
4 changes: 4 additions & 0 deletions src/necsus/compiletime/sharedGen.nim
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ proc worldFields(name: string, dir: MonoDirective): seq[WorldField] =
@[ (name, nnkBracketExpr.newTree(bindSym("SystemVarData"), dir.argType)) ]

proc generateShared(details: GenerateContext, arg: SystemArg, name: string, dir: MonoDirective): NimNode =

if isFastCompileMode(fastSharedGen):
return newEmptyNode()

result = newStmtList()
case details.hook
of Standard:
Expand Down
4 changes: 4 additions & 0 deletions src/necsus/compiletime/spawnGen.nim
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ proc buildSpawnProc(details: GenerateContext, dir: TupleDirective): NimNode =
spawnProcs[sig] = true.newLit

proc generate(details: GenerateContext, arg: SystemArg, name: string, dir: TupleDirective): NimNode =

if isFastCompileMode(fastSpawnGen):
return newEmptyNode()

case details.hook
of Outside:
return details.buildSpawnProc(dir)
Expand Down
4 changes: 4 additions & 0 deletions src/necsus/compiletime/tickIdGen.nim
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ proc sysArg(name: string): NimNode =
`appStateIdent`.`getTickId`

proc generate(details: GenerateContext, arg: SystemArg, name: string): NimNode =

if isFastCompileMode(fastTickId):
return newEmptyNode()

let tickGenProc = details.globalName(name)
case details.hook
of Outside:
Expand Down
8 changes: 8 additions & 0 deletions src/necsus/compiletime/timeGen.nim
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ proc deltaFields(name: string): seq[WorldField] =
@[ (name, bindSym("TimeDelta")), (lastTime.strVal, bindSym("BiggestFloat")) ]

proc generateDelta(details: GenerateContext, arg: SystemArg, name: string): NimNode =

if isFastCompileMode(fastTime):
return newEmptyNode()

let timeDelta = name.ident
let timeDeltaProc = details.globalName(name)
case details.hook
Expand Down Expand Up @@ -39,6 +43,10 @@ let deltaGenerator* {.compileTime.} = newGenerator(
proc elapsedFields(name: string): seq[WorldField] = @[ (name, bindSym("TimeElapsed")) ]

proc generateElapsed(details: GenerateContext, arg: SystemArg, name: string): NimNode =

if isFastCompileMode(fastTime):
return newEmptyNode()

let timeElapsed = name.ident
let timeElapsedProc = details.globalName(name)
case details.hook
Expand Down
29 changes: 17 additions & 12 deletions src/necsus/compiletime/worldGen.nim
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ proc fields(genInfo: CodeGenInfo): seq[(NimNode, NimNode)] =
let typ = nnkBracketExpr.newTree(bindSym("seq"), system.callbackSysType)
result.add (system.callbackSysMailboxName, typ)

for archetype in genInfo.archetypes:
let storageType = archetype.asStorageTuple
let typ = nnkBracketExpr.newTree(bindSym("ArchetypeStore"), storageType)
result.add (archetype.ident, typ)
if not isFastCompileMode(fastFields):
for archetype in genInfo.archetypes:
let storageType = archetype.asStorageTuple
let typ = nnkBracketExpr.newTree(bindSym("ArchetypeStore"), storageType)
result.add (archetype.ident, typ)

for (name, typ) in genInfo.worldFields:
result.add (name.ident, typ)
Expand Down Expand Up @@ -245,16 +246,20 @@ proc createSendProcs*(details: CodeGenInfo): NimNode =
else:
discard

if body.len == 0:
body.add(nnkDiscardStmt.newTree(newEmptyNode()))
if body.len == 0:
body.add(nnkDiscardStmt.newTree(newEmptyNode()))

result.add quote do:
proc `internalName`(`appStateIdent`: pointer, `event`: `eventType`) {.used, fastcall.} =
let `appStateIdent` {.used.} = cast[ptr `appStateType`](`appStateIdent`)
`body`
result.add quote do:
proc `internalName`(`appStateIdent`: pointer, `event`: `eventType`) {.used, fastcall.} =
let `appStateIdent` {.used.} = cast[ptr `appStateType`](`appStateIdent`)
`body`

proc `externalName`(`appStateIdent`: var `appStateType`, `event`: `eventType`) {.used, fastcall.} =
`internalName`(addr `appStateIdent`, `event`)
proc `externalName`(`appStateIdent`: var `appStateType`, `event`: `eventType`) {.used, fastcall.} =
`internalName`(addr `appStateIdent`, `event`)
else:
result.add quote do:
proc `externalName`(`appStateIdent`: var `appStateType`, `event`: `eventType`) {.used, fastcall.} =
discard

proc createConverterProcs*(details: CodeGenInfo): NimNode =
## Creates a list of procs for converting from one tuple type to another
Expand Down

0 comments on commit 24eb55f

Please sign in to comment.