From c8f88589cb8891cbe0f70aa6969837619cf05f41 Mon Sep 17 00:00:00 2001 From: TEC Date: Tue, 3 Jan 2023 13:09:28 +0800 Subject: [PATCH] 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. --- lisp/org-persist.el | 66 ++++++++++++++++++++++++--------------------- 1 file changed, 35 insertions(+), 31 deletions(-) diff --git a/lisp/org-persist.el b/lisp/org-persist.el index 6fbc8d87b..385e11811 100644 --- a/lisp/org-persist.el +++ b/lisp/org-persist.el @@ -386,37 +386,38 @@ FORMAT and ARGS are passed to `message'." (defun org-persist--read-elisp-file (&optional buffer-or-file) "Read elisp data from BUFFER-OR-FILE or current buffer." - (unless buffer-or-file (setq buffer-or-file (current-buffer))) - (with-temp-buffer - (if (bufferp buffer-or-file) - (set-buffer buffer-or-file) - (insert-file-contents buffer-or-file)) - (condition-case err - (let ((coding-system-for-read 'utf-8) - (read-circle t) - (start-time (float-time))) - ;; FIXME: Reading sometimes fails to read circular objects. - ;; I suspect that it happens when we have object reference - ;; #N# read before object definition #N=. If it is really - ;; so, it should be Emacs bug - either in `read' or in - ;; `prin1'. Meanwhile, just fail silently when `read' - ;; fails to parse the saved cache object. - (prog1 - (read (current-buffer)) - (org-persist--display-time - (- (float-time) start-time) - "Reading from %S" buffer-or-file))) - ;; Recover gracefully if index file is corrupted. - (error - ;; Remove problematic file. - (unless (bufferp buffer-or-file) (delete-file buffer-or-file)) - ;; Do not report the known error to user. - (if (string-match-p "Invalid read syntax" (error-message-string err)) - (message "Emacs reader failed to read data in %S. The error was: %S" - buffer-or-file (error-message-string err)) - (warn "Emacs reader failed to read data in %S. The error was: %S" - buffer-or-file (error-message-string err))) - nil)))) + (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 + (if (bufferp buffer-or-file) + (set-buffer buffer-or-file) + (insert-file-contents buffer-or-file)) + (condition-case err + (let ((read-circle t) + (start-time (float-time))) + ;; FIXME: Reading sometimes fails to read circular objects. + ;; I suspect that it happens when we have object reference + ;; #N# read before object definition #N=. If it is really + ;; so, it should be Emacs bug - either in `read' or in + ;; `prin1'. Meanwhile, just fail silently when `read' + ;; fails to parse the saved cache object. + (prog1 + (read (current-buffer)) + (org-persist--display-time + (- (float-time) start-time) + "Reading from %S" buffer-or-file))) + ;; Recover gracefully if index file is corrupted. + (error + ;; Remove problematic file. + (unless (bufferp buffer-or-file) (delete-file buffer-or-file)) + ;; Do not report the known error to user. + (if (string-match-p "Invalid read syntax" (error-message-string err)) + (message "Emacs reader failed to read data in %S. The error was: %S" + buffer-or-file (error-message-string err)) + (warn "Emacs reader failed to read data in %S. The error was: %S" + buffer-or-file (error-message-string err))) + nil))))) (defun org-persist--write-elisp-file (file data &optional no-circular pp) "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 ;; particular the large comment block in init_fileio. (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-level print-length