org-persist: Set utf-8 coding when reading/writing

* lisp/org-persist.el (org-persist--write-elisp-file,
org-persist--read-elisp-file): Instead of letting `find-auto-coding' be
invoked to determine the coding for org-persist cache files (which can
be surprisingly expensive), we simply set utf-8 coding when
reading/writing.
This commit is contained in:
Timothy 2023-01-03 13:09:28 +08:00 committed by Ihor Radchenko
parent e9c0696f79
commit c8f88589cb
No known key found for this signature in database
GPG key ID: 6470762A7DA11D8B

View file

@ -386,14 +386,15 @@ FORMAT and ARGS are passed to `message'."
(defun org-persist--read-elisp-file (&optional buffer-or-file) (defun org-persist--read-elisp-file (&optional buffer-or-file)
"Read elisp data from BUFFER-OR-FILE or current buffer." "Read elisp data from BUFFER-OR-FILE or current buffer."
(unless buffer-or-file (setq buffer-or-file (current-buffer))) (let (;; UTF-8 is explicitly used in `org-persist--write-elisp-file'.
(coding-system-for-read 'utf-8)
(buffer-or-file (or buffer-or-file (current-buffer))))
(with-temp-buffer (with-temp-buffer
(if (bufferp buffer-or-file) (if (bufferp buffer-or-file)
(set-buffer buffer-or-file) (set-buffer buffer-or-file)
(insert-file-contents buffer-or-file)) (insert-file-contents buffer-or-file))
(condition-case err (condition-case err
(let ((coding-system-for-read 'utf-8) (let ((read-circle t)
(read-circle t)
(start-time (float-time))) (start-time (float-time)))
;; FIXME: Reading sometimes fails to read circular objects. ;; FIXME: Reading sometimes fails to read circular objects.
;; I suspect that it happens when we have object reference ;; I suspect that it happens when we have object reference
@ -416,7 +417,7 @@ FORMAT and ARGS are passed to `message'."
buffer-or-file (error-message-string err)) buffer-or-file (error-message-string err))
(warn "Emacs reader failed to read data in %S. The error was: %S" (warn "Emacs reader failed to read data in %S. The error was: %S"
buffer-or-file (error-message-string err))) buffer-or-file (error-message-string err)))
nil)))) nil)))))
(defun org-persist--write-elisp-file (file data &optional no-circular pp) (defun org-persist--write-elisp-file (file data &optional no-circular pp)
"Write elisp DATA to FILE." "Write elisp DATA to FILE."
@ -432,6 +433,9 @@ FORMAT and ARGS are passed to `message'."
;; To read more about this, see the comments in Emacs' fileio.c, in ;; To read more about this, see the comments in Emacs' fileio.c, in
;; particular the large comment block in init_fileio. ;; particular the large comment block in init_fileio.
(let ((write-region-inhibit-fsync t) (let ((write-region-inhibit-fsync t)
;; We set UTF-8 here and in `org-persist--read-elisp-file'
;; to avoid the overhead from `find-auto-coding'.
(coding-system-for-write 'utf-8)
(print-circle (not no-circular)) (print-circle (not no-circular))
print-level print-level
print-length print-length