Skip to content

Commit

Permalink
Optimize advice entry lookup on every dispatch call
Browse files Browse the repository at this point in the history
  • Loading branch information
jaidetree committed Sep 11, 2021
1 parent 82c97d5 commit 2a50d8a
Showing 1 changed file with 10 additions and 13 deletions.
23 changes: 10 additions & 13 deletions lib/advice/init.fnl
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ Advising API to register functions

(fn register-advisable
[key f]
;; @TODO Replace with if-let or similar macro but doesn't work in an
;; isolated fennel file
(let [advice-entry (. advice key)]
(when (and advice-entry
advice-entry.original
Expand All @@ -42,7 +40,8 @@ Advising API to register functions
:original f)
(tset advice key
{:original f
:advice []}))))
:advice []}))
(. advice key)))

(fn get-or-create-advice-entry
[key]
Expand Down Expand Up @@ -141,13 +140,10 @@ Advising API to register functions
0)))

(fn dispatch-advice
[key [_tbl & args]]
(let [entry (. advice key)]
(if (> (count entry.advice) 0)
(do
(apply-advice entry args))
(do
(entry.original (table.unpack args))))))
[entry [_tbl & args]]
(if (> (count entry.advice) 0)
(apply-advice entry args)
(entry.original (table.unpack args))))


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Expand All @@ -174,12 +170,13 @@ Advising API to register functions
"
(let [module (get-module-name)
key (.. module "/" fn-name)
advice-reg (register-advisable key f)
ret {:key key}]
advice-entry (register-advisable key f)
ret {:key key
:advice advice-entry}]
(setmetatable ret
{:__name fn-name
:__call (fn [...]
(dispatch-advice key [...]))
(dispatch-advice advice-entry [...]))
:__index (fn [tbl key]
(. tbl key))})
(each [k v (pairs (or (. fennel.metadata f) []))]
Expand Down

0 comments on commit 2a50d8a

Please sign in to comment.