[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[nongnu] elpa/emacsql 109664b88b 060/427: Memoize emacsql-expand.
From: |
ELPA Syncer |
Subject: |
[nongnu] elpa/emacsql 109664b88b 060/427: Memoize emacsql-expand. |
Date: |
Tue, 13 Dec 2022 02:59:28 -0500 (EST) |
branch: elpa/emacsql
commit 109664b88b2920eb35bb5807e36dc342552e943d
Author: Christopher Wellons <wellons@nullprogram.com>
Commit: Christopher Wellons <wellons@nullprogram.com>
Memoize emacsql-expand.
---
emacsql.el | 30 ++++++++++++++++++++----------
1 file changed, 20 insertions(+), 10 deletions(-)
diff --git a/emacsql.el b/emacsql.el
index fb7e700120..e2c646d578 100644
--- a/emacsql.el
+++ b/emacsql.el
@@ -289,6 +289,9 @@ Each row must be a sequence of values to store into TABLE.
(defvar emacsql-expanders ()
"Alist of all expansion functions.")
+(defvar emacsql-expander-cache (make-hash-table :test 'equal)
+ "Cache used to memoize `emacsql-expand'.")
+
(defun emacsql-add-expander (keyword arity function)
"Register FUNCTION for KEYWORD as a SQL expander.
FUNCTION should accept the keyword's arguments and should return
@@ -296,6 +299,7 @@ a list of (<string> [arg-pos] ...).
See also `emacsql-with-errors'."
(prog1 keyword
+ (clrhash emacsql-expander-cache)
(push (list keyword arity function) emacsql-expanders)))
(defmacro emacsql-defexpander (keyword args &rest body)
@@ -305,16 +309,22 @@ See also `emacsql-with-errors'."
(defun emacsql-expand (sql)
"Expand SQL into a SQL-consumable string, with variables."
- (cl-loop with items = (cl-coerce sql 'list)
- while (not (null items))
- for keyword = (pop items)
- for (arity expander) = (cdr (assoc keyword emacsql-expanders))
- when expander
- collect (apply expander (cl-subseq items 0 arity)) into parts
- else do (error "Unrecognized keyword %s" keyword)
- do (setf items (cl-subseq items arity))
- finally (cl-return (cons (concat (mapconcat #'car parts " ") ";")
- (apply #'nconc (mapcar #'cdr parts))))))
+ (let* ((cache emacsql-expander-cache)
+ (cached (and cache (gethash sql cache))))
+ (or cached
+ (cl-loop with items = (cl-coerce sql 'list)
+ while (not (null items))
+ for keyword = (pop items)
+ for (arity expander) = (cdr (assoc keyword emacsql-expanders))
+ when expander
+ collect (apply expander (cl-subseq items 0 arity)) into parts
+ else do (error "Unrecognized keyword %s" keyword)
+ do (setf items (cl-subseq items arity))
+ finally
+ (let ((string (concat (mapconcat #'car parts " ") ";"))
+ (vars (apply #'nconc (mapcar #'cdr parts))))
+ (cl-return (setf (gethash sql cache)
+ (cons string vars))))))))
(defun emacsql-format (expansion &rest args)
"Fill in the variables EXPANSION with ARGS."
- [nongnu] elpa/emacsql 801bc6239f 222/427: Swap the tuple order., (continued)
- [nongnu] elpa/emacsql 801bc6239f 222/427: Swap the tuple order., ELPA Syncer, 2022/12/13
- [nongnu] elpa/emacsql 9e6ea00f01 243/427: Add note about PostgreSQL., ELPA Syncer, 2022/12/13
- [nongnu] elpa/emacsql ad6c09681e 261/427: Oops, print the tuple., ELPA Syncer, 2022/12/13
- [nongnu] elpa/emacsql ff59ca9336 265/427: Clear out binaries in distclean, too., ELPA Syncer, 2022/12/13
- [nongnu] elpa/emacsql 00bb81ff2b 266/427: Fix Makefile typo., ELPA Syncer, 2022/12/13
- [nongnu] elpa/emacsql c8de0ff1fd 293/427: Adjust the EmacSQL protocol., ELPA Syncer, 2022/12/13
- [nongnu] elpa/emacsql 51224aab7a 288/427: Add unit test for last change., ELPA Syncer, 2022/12/13
- [nongnu] elpa/emacsql c5845884f4 021/427: Rename type to column., ELPA Syncer, 2022/12/13
- [nongnu] elpa/emacsql 7caa63c11e 023/427: Allow any sequence for table spec., ELPA Syncer, 2022/12/13
- [nongnu] elpa/emacsql 963f221b82 049/427: Add expr expansion function., ELPA Syncer, 2022/12/13
- [nongnu] elpa/emacsql 109664b88b 060/427: Memoize emacsql-expand.,
ELPA Syncer <=
- [nongnu] elpa/emacsql 1fb97f7d4a 092/427: Drop unusable macro emacsql-with-connection., ELPA Syncer, 2022/12/13
- [nongnu] elpa/emacsql add06c47b9 117/427: Bring back :into for :replace (oops!)., ELPA Syncer, 2022/12/13
- [nongnu] elpa/emacsql d7a05db54e 080/427: Add :replace expander., ELPA Syncer, 2022/12/13
- [nongnu] elpa/emacsql 0f0840ffbf 128/427: Add :default column constraint., ELPA Syncer, 2022/12/13
- [nongnu] elpa/emacsql bbe3031a50 130/427: Add table constraints., ELPA Syncer, 2022/12/13
- [nongnu] elpa/emacsql 53b2e98515 131/427: Fill out README for new schema table constraints., ELPA Syncer, 2022/12/13
- [nongnu] elpa/emacsql 364c3cacb6 133/427: More advanced :from sources, again., ELPA Syncer, 2022/12/13
- [nongnu] elpa/emacsql 99a8069389 135/427: Add support for foreign keys., ELPA Syncer, 2022/12/13
- [nongnu] elpa/emacsql e6ab07e16b 140/427: Switch to a friendlier name., ELPA Syncer, 2022/12/13
- [nongnu] elpa/emacsql bec6af6940 148/427: Add VACUUM., ELPA Syncer, 2022/12/13