+ enable lexical-scope everywhere (lexical-binding = t): ~5-10% faster startup; ~5-20% general boost + reduce consing, function calls & garbage collection by preferring cl-loop & dolist over lambda closures (for mapc[ar], add-hook, and various cl-lib filter/map/reduce functions) -- where possible + prefer functions with dedicated opcodes, like assq (see byte-defop's in bytecomp.el for more) + prefer pcase & cond (faster) over cl-case + general refactor for code readability + ensure naming & style conventions are adhered to + appease byte-compiler by marking unused variables with underscore + defer minor mode activation to after-init, emacs-startup or window-setup hooks; a customization opportunity for users + ensures custom functionality won't interfere with startup.
31 lines
1.1 KiB
EmacsLisp
31 lines
1.1 KiB
EmacsLisp
;;; core/autoload/memoize.el -*- lexical-binding: t; -*-
|
|
|
|
;;;###autoload
|
|
(defvar doom-memoized-table (make-hash-table :test 'equal :size 10)
|
|
"A lookup table containing memoized functions. The keys are argument lists,
|
|
and the value is the function's return value.")
|
|
|
|
;;;###autoload
|
|
(defun doom-memoize (name)
|
|
"Memoizes an existing function. NAME is a symbol."
|
|
(let ((func (symbol-function name)))
|
|
(put name 'function-documentation
|
|
(concat (documentation func) " (memoized)"))
|
|
(fset name
|
|
`(lambda (&rest args)
|
|
(let ((key (cons ',name args)))
|
|
(or (gethash key doom-memoized-table)
|
|
(puthash key (apply ',func args)
|
|
doom-memoized-table)))))))
|
|
|
|
;;;###autoload
|
|
(defmacro def-memoized! (name arglist &rest body)
|
|
"Create a memoize'd function. NAME, ARGLIST, DOCSTRING and BODY
|
|
have the same meaning as in `defun'."
|
|
(declare (indent defun) (doc-string 3))
|
|
`(,(if (bound-and-true-p byte-compile-current-file)
|
|
'with-no-warnings
|
|
'progn)
|
|
(defun ,name ,arglist ,@body)
|
|
(doom-memoize ',name)))
|
|
|