rougier / svg-tag-mode

A minor mode for Emacs that replace keywords with nice SVG labels
GNU General Public License v3.0
496 stars 28 forks source link

Add cache for SVG tags? #54

Open Elilif opened 10 months ago

Elilif commented 10 months ago

I noticed that every time a new keyword(such as "TODO") is inserted, svg-tag-make (or similar functions) is called. I think this is unnecessary because the same parameters will always generate the same image. So is it possible to add a cache to use the cached image in the case of the same parameters?

For instance:

(defvar svg-tag-cache nil)

(defun svg-tag-make-with-cache (tag &rest args)
  (unless svg-tag-cache
    (setq svg-tag-cache (make-hash-table :test 'equal)))
  (with-memoization (gethash `(tag ,@args) svg-tag-cache)
    (apply #'svg-tag-make tag args)))

In my case, I found that when using SVG tags in the tab-bar or modeline, using caching significantly reduces resource consumption.

Is it worthwhile to create a PR?

appetrosyan commented 9 months ago

I'd say it's a useful optimisation.

rougier commented 9 months ago

Totally agree and I'm doing such caching in other libraries that use svg-lib but I do it very badly compared to your code. So yes, a PR would be warmly welcome indeed. Can we do it for every tag functions or do you foresee some problems ?