summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Thompson <dthompson2@worcester.edu>2023-05-04 18:42:07 -0400
committerDavid Thompson <dthompson2@worcester.edu>2023-05-04 18:42:57 -0400
commitffd9d5164dcb103258bbc0e788c9b04c3ce52aca (patch)
tree0b9ce35f0ca42a71c536e3cc5ee9f0120b16fec9
parentbdb741ebb3a04adb1889a25c8f33c3ac9dff3b8c (diff)
Lazily create agendas for scripted nodes.
Most nodes do not end up running scripts, so this saves some precious time and reduces GC pressure.
-rw-r--r--catbird/mixins.scm22
1 files changed, 17 insertions, 5 deletions
diff --git a/catbird/mixins.scm b/catbird/mixins.scm
index 3945aab..6e1cd4a 100644
--- a/catbird/mixins.scm
+++ b/catbird/mixins.scm
@@ -150,7 +150,16 @@
(define-class <scriptable> (<updatable>)
(paused? #:accessor paused? #:init-form #f #:init-keyword #:paused?)
- (agenda #:getter agenda #:init-thunk make-agenda))
+ ;; The agenda gets created lazily when the first script is run.
+ ;; This reduces overhead for scriptable objects that don't end up
+ ;; running any scripts.
+ (agenda #:accessor agenda #:init-value #f))
+
+(define-method (ensure-agenda (obj <scriptable>))
+ (or (agenda obj)
+ (let ((fresh-agenda (make-agenda)))
+ (set! (agenda obj) fresh-agenda)
+ fresh-agenda)))
(define-method (on-pause (obj <scriptable>))
#t)
@@ -170,15 +179,18 @@
(define-method (update/around (obj <scriptable>) dt)
(unless (paused? obj)
- (with-agenda (agenda obj)
- (update-agenda dt)
+ (let ((agenda (agenda obj)))
+ (when agenda
+ (with-agenda agenda
+ (update-agenda dt)))
(next-method))))
(define-syntax-rule (run-script obj body ...)
- (with-agenda (agenda obj) (script body ...)))
+ (with-agenda (ensure-agenda obj) (script body ...)))
(define-method (stop-scripts obj)
- (with-agenda (agenda obj) (clear-agenda)))
+ (when (agenda obj)
+ (with-agenda (agenda obj) (clear-agenda))))
;;;