Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

org-ql-select function could be faster by caching the buffers variable #447

Closed
molok opened this issue Jul 24, 2024 · 1 comment
Closed

Comments

@molok
Copy link

molok commented Jul 24, 2024

OS/platform

Linux

Emacs version and provenance

Emacs 29.4 compiled by myself

Org version and provenance

9.7 shipped with emacs

org-ql package version and provenance

v0.8.5 (shipped with doom-emacs)

Description

I've got a usecase where I call org-ql-select 500 times and I've noticed that the caching could be improved since the buffers variable is kind-of expensive to evaluate (unless buffers-or-files is already a list of buffers).

org-ql/org-ql.el

Lines 377 to 393 in b7d4856

(-let* ((buffers (->> (cl-typecase buffers-or-files
(null (list (current-buffer)))
(function (funcall buffers-or-files))
(list buffers-or-files)
(otherwise (list buffers-or-files)))
(--map (cl-etypecase it
;; NOTE: This etypecase is essential to opening links safely,
;; as it rejects, e.g. lambdas in the buffers-files argument.
(buffer it)
(string (or (find-buffer-visiting it)
(when (file-readable-p it)
;; It feels unintuitive that `find-file-noselect' returns
;; a buffer if the filename doesn't exist.
(find-file-noselect it))
(display-warning 'org-ql-select (format "Can't open file: %s" it) :error)))))
;; Ignore special/hidden buffers.
(--remove (string-prefix-p " " (buffer-name it)))))

I suspect many uses of org-ql-select pass a string or (org-agenda-files) as the argument of buffers-or-files so caching in the org-ql-select might make sense, but I'm not sure if the obvious implementation will handle all the edge-cases.

On my machine 500 function calls to org-ql-select passing the output of (org-agenda-files) take 5.8s, when passing a list of buffers it takes 1.9s so the difference is quite significant.

Again, I could adapt my code to simply pass a list of buffers, but it seems to be a low-hanging performance improvement that might be beneficial for all callers.

Etc.

No response

@molok molok changed the title org-ql-select function could be faster caching the buffers variable org-ql-select function could be faster by caching the buffers variable Jul 24, 2024
@alphapapa
Copy link
Owner

I doubt that caching that would be appropriate, because caching means adding a new "can of worms" to the whole procedure: how would we know when to invalidate the cache? What would the cache even bypass? The validations you cited are there for good reason.

This seems like an obvious case where, if you are calling org-ql-select 500 times in rapid succession, you should keep the list of buffers in the calling code.

Are you sure that the bottleneck is in org-ql-select and not in org-agenda-files?

Also, please explain more about your use case; it seems unusual, and I wonder if there might be other ways to improve performance.

Repository owner locked and limited conversation to collaborators Jul 24, 2024
@alphapapa alphapapa converted this issue into discussion #448 Jul 24, 2024

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Projects
None yet
Development

No branches or pull requests

2 participants