Mahmood over at λ mental has a surprising post concerning the time to load and parse Org files. What was surprising to me is that a method I would have expected to take longer was actually much faster. Here’s a couple of statistics from Mahmood’s post:
| Command | Time |
|---|---|
(find-file-noselect "/home/mahmooz/brain/notes/1707069432.org") |
0.212753316 |
(quick-way) |
0.003604351 |
where quick-way is
(defun quick-way () (with-temp-buffer ;; the final `t' is to replace contents of the temp buffer (insert-file-contents "/home/mahmooz/brain/notes/1707069432.org" nil nil nil t) (let ((major-mode 'org-mode)) (org-element-parse-buffer)) nil))
As far as I can see, after a quick scan, find-file-noselect calls insert-file-contents after a bit of preprocessing but nowhere near enough reprocessing to account for the huge time difference. My unconsidered expectation was that it would be the other way around (insert-file-contents calls find-file-noselct), which explains my confusion. But I still don’t understand where the difference is coming from. Doubtless if I bore down a bit or (gasp) actually did some profiling I could find out but I’m enjoying being mystified and—truth to tell—am too lazy to track it down.
In any event, that little mystery is only part of Mahmood’s post. His main complaint was that loading his Org agenda and Org Roam files was taking too long. The above observation turned out to be the crux of the matter and Mahmood provides some code to load and parse Org files much faster than the default methods.