From ffd9d5164dcb103258bbc0e788c9b04c3ce52aca Mon Sep 17 00:00:00 2001 From: David Thompson Date: Thu, 4 May 2023 18:42:07 -0400 Subject: Lazily create agendas for scripted nodes. Most nodes do not end up running scripts, so this saves some precious time and reduces GC pressure. --- catbird/mixins.scm | 22 +++++++++++++++++----- 1 file 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 () (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 )) + (or (agenda obj) + (let ((fresh-agenda (make-agenda))) + (set! (agenda obj) fresh-agenda) + fresh-agenda))) (define-method (on-pause (obj )) #t) @@ -170,15 +179,18 @@ (define-method (update/around (obj ) 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)))) ;;; -- cgit v1.2.3