1
0
mirror of https://github.com/m00natic/vlfi.git synced 2025-01-18 12:05:31 +00:00

Optimize save performance over the temp file if such is used. Add

customization option whether to use temp file.
This commit is contained in:
Andrey Kotlarski 2014-09-26 14:00:01 +03:00
parent 842569ae07
commit c3a308c835
2 changed files with 49 additions and 41 deletions

View File

@ -356,20 +356,21 @@ optimizing over TYPES up to MAX-IDX."
(if none-missing (if none-missing
(setq vlf-batch-size (* (1+ best-idx) vlf-tune-step))))) (setq vlf-batch-size (* (1+ best-idx) vlf-tune-step)))))
(defun vlf-tune-batch (types &optional linear) (defun vlf-tune-batch (types &optional linear file)
"Adjust `vlf-batch-size' to optimal value optimizing on TYPES. "Adjust `vlf-batch-size' to optimal value optimizing on TYPES.
TYPES is alist of elements that may be of form (type coef) or TYPES is alist of elements that may be of form (type coef) or
non list values in which case coeficient is assumed 1. non list values in which case coeficient is assumed 1.
Types can be :insert, :raw, :encode, :write, :hexl or :dehexlify. Types can be :insert, :raw, :encode, :write, :hexl or :dehexlify.
If LINEAR is non nil, use brute-force. In case requested measurement If LINEAR is non nil, use brute-force. In case requested measurement
is missing, stop search and set `vlf-batch-size' to this value. is missing, stop search and set `vlf-batch-size' to this value.
FILE if given is filename to be used, otherwise `buffer-file-name'.
Suitable for multiple batch operations." Suitable for multiple batch operations."
(if (eq vlf-tune-enabled t) (if (eq vlf-tune-enabled t)
(let ((max-idx (1- (/ (min vlf-tune-max (let ((max-idx (1- (/ (min vlf-tune-max
(/ (1+ vlf-file-size) 2)) (/ (1+ vlf-file-size) 2))
vlf-tune-step)))) vlf-tune-step))))
(cond (linear (vlf-tune-linear types max-idx)) (cond (linear (vlf-tune-linear types max-idx))
((file-remote-p buffer-file-name) ((file-remote-p (or file buffer-file-name))
(vlf-tune-conservative types)) (vlf-tune-conservative types))
((<= 1 max-idx) ((<= 1 max-idx)
(if (< max-idx 3) (if (< max-idx 3)

View File

@ -29,6 +29,13 @@
(require 'vlf-base) (require 'vlf-base)
(defcustom vlf-save-in-place 'ask
"Should VLF save in place when additional adjustment of file content\
is needed."
:group 'vlf :type '(choice (const :tag "Always when applicable" t)
(const :tag "Ask when applicable" 'ask)
(const :tag "Never" nil)))
(defun vlf-write () (defun vlf-write ()
"Write current chunk to file. Always return true to disable save. "Write current chunk to file. Always return true to disable save.
If changing size of chunk, shift remaining file content." If changing size of chunk, shift remaining file content."
@ -59,42 +66,42 @@ If changing size of chunk, shift remaining file content."
(if (zerop size-change) (if (zerop size-change)
(vlf-tune-write nil nil vlf-start-pos t (vlf-tune-write nil nil vlf-start-pos t
(- vlf-end-pos vlf-start-pos)) (- vlf-end-pos vlf-start-pos))
(let ((tramp-verbose (if (boundp 'tramp-verbose) (let ((pos (point))
(min tramp-verbose 2))) (font-lock font-lock-mode)
(pos (point)) (batch-size vlf-batch-size)
(font-lock font-lock-mode)) time)
(font-lock-mode 0) (font-lock-mode 0)
(let ((batch-size vlf-batch-size) (if (or (file-remote-p buffer-file-name)
time) (if (eq vlf-save-in-place 'ask)
(if (or (file-remote-p buffer-file-name) (y-or-n-p "File content needs be adjusted\
(y-or-n-p "File content needs be adjusted till\ till end. Use temporary copy of the whole file (slower but safer)? ")
end. Use temporary copy of the whole file (slower but safer)? ")) (not vlf-save-in-place)))
(let ((file-tmp (make-temp-file (let ((file-tmp (make-temp-file
(file-name-nondirectory (file-name-nondirectory
buffer-file-name)))) buffer-file-name))))
(setq time (float-time)) (setq time (float-time))
(copy-file buffer-file-name file-tmp t t t t) (copy-file buffer-file-name file-tmp t t t t)
(if (< 0 size-change) (if (< 0 size-change)
(vlf-file-shift-back size-change region-length (vlf-file-shift-back size-change region-length
file-tmp) file-tmp)
(vlf-file-shift-forward (- size-change) (vlf-file-shift-forward (- size-change)
region-length file-tmp)) region-length file-tmp))
(rename-file file-tmp buffer-file-name t)) (rename-file file-tmp buffer-file-name t))
(setq time (float-time)) (setq time (float-time))
(if (< 0 size-change) (if (< 0 size-change)
(vlf-file-shift-back size-change region-length) (vlf-file-shift-back size-change region-length)
(vlf-file-shift-forward (- size-change) (vlf-file-shift-forward (- size-change)
region-length))) region-length)))
(if font-lock (font-lock-mode 1)) (if font-lock (font-lock-mode 1))
(setq vlf-batch-size batch-size) (setq vlf-batch-size batch-size)
(vlf-move-to-chunk-2 vlf-start-pos (vlf-move-to-chunk-2 vlf-start-pos
(if (< (- vlf-end-pos vlf-start-pos) (if (< (- vlf-end-pos vlf-start-pos)
vlf-batch-size) vlf-batch-size)
(+ vlf-start-pos vlf-batch-size) (+ vlf-start-pos vlf-batch-size)
vlf-end-pos)) vlf-end-pos))
(vlf-update-buffer-name) (vlf-update-buffer-name)
(goto-char pos) (goto-char pos)
(message "Save took %f seconds" (- (float-time) time))))))) (message "Save took %f seconds" (- (float-time) time))))))
(if hexl (vlf-tune-hexlify))) (if hexl (vlf-tune-hexlify)))
(run-hook-with-args 'vlf-after-batch-functions 'write)) (run-hook-with-args 'vlf-after-batch-functions 'write))
t) t)
@ -129,7 +136,7 @@ back at WRITE-POS using FILE.
Return nil if EOF is reached, t otherwise." Return nil if EOF is reached, t otherwise."
(erase-buffer) (erase-buffer)
(vlf-verify-size t file) (vlf-verify-size t file)
(vlf-tune-batch '(:raw :write)) ;insert speed over temp write file may defer wildly (vlf-tune-batch '(:raw :write) nil file) ;insert speed over temp write file may defer wildly
(let ((read-end (min (+ read-pos vlf-batch-size) vlf-file-size))) ;compared to the original file (let ((read-end (min (+ read-pos vlf-batch-size) vlf-file-size))) ;compared to the original file
(vlf-tune-insert-file-contents-literally read-pos read-end file) (vlf-tune-insert-file-contents-literally read-pos read-end file)
(vlf-tune-write nil nil write-pos 0 (- read-end read-pos) file) (vlf-tune-write nil nil write-pos 0 (- read-end read-pos) file)
@ -140,7 +147,7 @@ Return nil if EOF is reached, t otherwise."
WRITE-SIZE is byte length of saved chunk. WRITE-SIZE is byte length of saved chunk.
FILE if given is filename to be used, otherwise `buffer-file-name'. FILE if given is filename to be used, otherwise `buffer-file-name'.
Done by saving content up front and then writing previous batch." Done by saving content up front and then writing previous batch."
(vlf-tune-batch '(:raw :write)) (vlf-tune-batch '(:raw :write) nil file)
(let ((read-size (max vlf-batch-size size-change)) (let ((read-size (max vlf-batch-size size-change))
(read-pos vlf-end-pos) (read-pos vlf-end-pos)
(write-pos vlf-start-pos) (write-pos vlf-start-pos)
@ -150,7 +157,7 @@ Done by saving content up front and then writing previous batch."
(vlf-with-undo-disabled (vlf-with-undo-disabled
(when (vlf-shift-batches read-size read-pos write-pos (when (vlf-shift-batches read-size read-pos write-pos
write-size t file) write-size t file)
(vlf-tune-batch '(:raw :write)) (vlf-tune-batch '(:raw :write) nil file)
(setq write-pos (+ read-pos size-change) (setq write-pos (+ read-pos size-change)
read-pos (+ read-pos read-size) read-pos (+ read-pos read-size)
write-size read-size write-size read-size
@ -159,7 +166,7 @@ Done by saving content up front and then writing previous batch."
(let ((coding-system-for-write 'no-conversion)) (let ((coding-system-for-write 'no-conversion))
(while (vlf-shift-batches read-size read-pos write-pos (while (vlf-shift-batches read-size read-pos write-pos
write-size nil file) write-size nil file)
(vlf-tune-batch '(:raw :write)) (vlf-tune-batch '(:raw :write) nil file)
(setq write-pos (+ read-pos size-change) (setq write-pos (+ read-pos size-change)
read-pos (+ read-pos read-size) read-pos (+ read-pos read-size)
write-size read-size write-size read-size