2014-08-31 02:10:28 +03:00
|
|
|
;;; vlf-tune.el --- VLF tuning operations -*- lexical-binding: t -*-
|
|
|
|
|
|
|
|
;; Copyright (C) 2014 Free Software Foundation, Inc.
|
|
|
|
|
|
|
|
;; Keywords: large files, batch size, performance
|
|
|
|
;; Author: Andrey Kotlarski <m00naticus@gmail.com>
|
|
|
|
;; URL: https://github.com/m00natic/vlfi
|
|
|
|
|
|
|
|
;; This file is free software; you can redistribute it and/or modify
|
|
|
|
;; it under the terms of the GNU General Public License as published by
|
|
|
|
;; the Free Software Foundation; either version 3, or (at your option)
|
|
|
|
;; any later version.
|
|
|
|
|
|
|
|
;; This file is distributed in the hope that it will be useful,
|
|
|
|
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
;; GNU General Public License for more details.
|
|
|
|
|
|
|
|
;; You should have received a copy of the GNU General Public License
|
|
|
|
;; along with GNU Emacs; see the file COPYING. If not, write to
|
|
|
|
;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
;; Boston, MA 02111-1307, USA.
|
|
|
|
|
|
|
|
;;; Commentary:
|
|
|
|
;; This package provides wrappers for basic chunk operations that add
|
2014-09-08 02:21:09 +03:00
|
|
|
;; profiling and automatic tuning of `vlf-batch-size'.
|
2014-08-31 02:10:28 +03:00
|
|
|
|
|
|
|
;;; Code:
|
|
|
|
|
2014-09-08 02:21:09 +03:00
|
|
|
(defgroup vlf nil "View Large Files in Emacs."
|
|
|
|
:prefix "vlf-" :group 'files)
|
|
|
|
|
|
|
|
(defcustom vlf-batch-size 1000000
|
|
|
|
"Defines how large each batch of file data initially is (in bytes)."
|
|
|
|
:group 'vlf :type 'integer)
|
|
|
|
(put 'vlf-batch-size 'permanent-local t)
|
|
|
|
|
2014-08-31 02:10:28 +03:00
|
|
|
(defcustom vlf-tune-enabled t
|
|
|
|
"Whether to allow automatic change of batch size.
|
|
|
|
If nil, completely disable. If `stats', maintain measure statistics,
|
|
|
|
but don't change batch size. If t, measure and change."
|
|
|
|
:group 'vlf :type '(choice (const :tag "Enabled" t)
|
|
|
|
(const :tag "Just statistics" stats)
|
|
|
|
(const :tag "Disabled" nil)))
|
|
|
|
|
2014-09-05 02:49:55 +03:00
|
|
|
(defvar vlf-file-size 0 "Total size in bytes of presented file.")
|
2014-09-03 02:34:37 +03:00
|
|
|
(make-variable-buffer-local 'vlf-file-size)
|
|
|
|
(put 'vlf-file-size 'permanent-local t)
|
|
|
|
|
2014-08-31 02:10:28 +03:00
|
|
|
(defun vlf-tune-ram-size ()
|
|
|
|
"Try to determine RAM size in bytes."
|
2014-09-05 02:49:55 +03:00
|
|
|
(if (executable-find "free")
|
|
|
|
(let* ((free (shell-command-to-string "free"))
|
|
|
|
(match-from (string-match "[[:digit:]]+" free)))
|
|
|
|
(if match-from
|
|
|
|
(* 1000 (string-to-number (substring free match-from
|
|
|
|
(match-end 0))))))))
|
2014-08-31 02:10:28 +03:00
|
|
|
|
2014-11-18 12:02:24 +02:00
|
|
|
(defcustom vlf-tune-max (max (let ((ram-size (vlf-tune-ram-size)))
|
|
|
|
(if ram-size
|
|
|
|
(/ ram-size 20)
|
|
|
|
0))
|
|
|
|
large-file-warning-threshold)
|
2014-10-08 19:33:59 +03:00
|
|
|
"Maximum batch size in bytes when auto tuning.
|
|
|
|
Avoid increasing this after opening file with VLF."
|
2014-08-31 02:10:28 +03:00
|
|
|
:group 'vlf :type 'integer)
|
|
|
|
|
2014-10-02 13:49:42 +03:00
|
|
|
(defcustom vlf-tune-step (/ vlf-tune-max 10000)
|
2014-10-08 19:33:59 +03:00
|
|
|
"Step used for tuning in bytes.
|
|
|
|
Avoid decreasing this after opening file with VLF."
|
2014-08-31 02:10:28 +03:00
|
|
|
:group 'vlf :type 'integer)
|
|
|
|
|
2014-09-07 22:10:56 +03:00
|
|
|
(defcustom vlf-tune-load-time 1.0
|
|
|
|
"How many seconds should batch take to load for best user experience."
|
|
|
|
:group 'vlf :type 'float)
|
|
|
|
|
2014-08-31 02:10:28 +03:00
|
|
|
(defvar vlf-tune-insert-bps nil
|
|
|
|
"Vector of bytes per second insert measurements.")
|
|
|
|
(make-variable-buffer-local 'vlf-tune-insert-bps)
|
2014-09-03 02:34:37 +03:00
|
|
|
(put 'vlf-tune-insert-bps 'permanent-local t)
|
2014-08-31 02:10:28 +03:00
|
|
|
|
|
|
|
(defvar vlf-tune-insert-raw-bps nil
|
|
|
|
"Vector of bytes per second non-decode insert measurements.")
|
|
|
|
(make-variable-buffer-local 'vlf-tune-insert-raw-bps)
|
|
|
|
(put 'vlf-tune-insert-raw-bps 'permanent-local t)
|
|
|
|
|
|
|
|
(defvar vlf-tune-encode-bps nil
|
|
|
|
"Vector of bytes per second encode measurements.")
|
2014-12-13 18:39:35 +02:00
|
|
|
(make-variable-buffer-local 'vlf-tune-encode-bps)
|
|
|
|
(put 'vlf-tune-encode-bps 'permanent-local t)
|
2014-08-31 02:10:28 +03:00
|
|
|
|
|
|
|
(defvar vlf-tune-write-bps nil
|
|
|
|
"Vector of bytes per second write measurements.")
|
|
|
|
|
|
|
|
(defvar vlf-tune-hexl-bps nil
|
|
|
|
"Vector of bytes per second hexlify measurements.")
|
|
|
|
|
|
|
|
(defvar vlf-tune-dehexlify-bps nil
|
|
|
|
"Vector of bytes per second dehexlify measurements.")
|
|
|
|
|
2014-12-17 01:42:05 +02:00
|
|
|
(defvar vlf-start-pos)
|
2014-10-16 02:55:02 +03:00
|
|
|
(defvar hexl-bits)
|
|
|
|
(defvar hexl-max-address)
|
|
|
|
(declare-function hexl-line-displen "hexl")
|
|
|
|
(declare-function dehexlify-buffer "hexl")
|
|
|
|
|
2014-12-17 01:42:05 +02:00
|
|
|
(defun vlf-tune-copy-profile (from-buffer &optional to-buffer)
|
|
|
|
"Copy specific profile vectors of FROM-BUFFER to TO-BUFFER.
|
|
|
|
If TO-BUFFER is nil, copy to current buffer."
|
|
|
|
(let (insert-bps insert-raw-bps encode-bps)
|
|
|
|
(with-current-buffer from-buffer
|
|
|
|
(setq insert-bps vlf-tune-insert-bps
|
|
|
|
insert-raw-bps vlf-tune-insert-raw-bps
|
|
|
|
encode-bps vlf-tune-encode-bps))
|
|
|
|
(if to-buffer
|
|
|
|
(with-current-buffer to-buffer
|
|
|
|
(setq vlf-tune-insert-bps insert-bps
|
|
|
|
vlf-tune-insert-raw-bps insert-raw-bps
|
|
|
|
vlf-tune-encode-bps encode-bps))
|
|
|
|
(setq vlf-tune-insert-bps insert-bps
|
|
|
|
vlf-tune-insert-raw-bps insert-raw-bps
|
|
|
|
vlf-tune-encode-bps encode-bps))))
|
|
|
|
|
2014-09-03 02:34:37 +03:00
|
|
|
(defun vlf-tune-closest-index (size)
|
|
|
|
"Get closest measurement index corresponding to SIZE."
|
2014-09-04 15:24:55 +03:00
|
|
|
(let ((step (float vlf-tune-step)))
|
|
|
|
(max 0 (1- (min (round size step) (round vlf-tune-max step))))))
|
2014-09-03 02:34:37 +03:00
|
|
|
|
2014-09-05 02:49:55 +03:00
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
2014-09-08 02:21:09 +03:00
|
|
|
;;; profiling
|
2014-09-05 02:49:55 +03:00
|
|
|
|
|
|
|
(defun vlf-tune-initialize-measurement ()
|
|
|
|
"Initialize measurement vector."
|
2014-10-07 01:33:21 +03:00
|
|
|
(make-vector (1- (/ vlf-tune-max vlf-tune-step)) nil))
|
2014-09-05 02:49:55 +03:00
|
|
|
|
2014-08-31 02:10:28 +03:00
|
|
|
(defmacro vlf-tune-add-measurement (vec size time)
|
|
|
|
"Add at an appropriate position in VEC new SIZE TIME measurement.
|
|
|
|
VEC is a vector of (mean time . count) elements ordered by size."
|
2014-09-04 15:24:55 +03:00
|
|
|
`(when (and vlf-tune-enabled (not (zerop ,size)))
|
2014-08-31 02:10:28 +03:00
|
|
|
(or ,vec (setq ,vec (vlf-tune-initialize-measurement)))
|
2014-09-03 02:34:37 +03:00
|
|
|
(let* ((idx (vlf-tune-closest-index ,size))
|
2014-09-07 00:02:14 +03:00
|
|
|
(existing (aref ,vec idx)))
|
|
|
|
(aset ,vec idx (if (consp existing)
|
|
|
|
(let ((count (1+ (cdr existing)))) ;recalculate mean
|
|
|
|
(cons (/ (+ (* (1- count) (car existing))
|
|
|
|
(/ ,size ,time))
|
|
|
|
count)
|
|
|
|
count))
|
|
|
|
(cons (/ ,size ,time) 1))))))
|
2014-08-31 02:10:28 +03:00
|
|
|
|
|
|
|
(defmacro vlf-time (&rest body)
|
|
|
|
"Get timing consed with result of BODY execution."
|
2014-09-08 02:21:09 +03:00
|
|
|
`(if vlf-tune-enabled
|
|
|
|
(let* ((time (float-time))
|
|
|
|
(result (progn ,@body)))
|
|
|
|
(cons (- (float-time) time) result))
|
|
|
|
(let ((result (progn ,@body)))
|
|
|
|
(cons nil result))))
|
2014-08-31 02:10:28 +03:00
|
|
|
|
|
|
|
(defun vlf-tune-insert-file-contents (start end)
|
|
|
|
"Extract decoded file bytes START to END and save time it takes."
|
|
|
|
(let ((result (vlf-time (insert-file-contents buffer-file-name
|
|
|
|
nil start end))))
|
|
|
|
(vlf-tune-add-measurement vlf-tune-insert-bps
|
|
|
|
(- end start) (car result))
|
|
|
|
(cdr result)))
|
|
|
|
|
2014-09-26 01:56:12 +03:00
|
|
|
(defun vlf-tune-insert-file-contents-literally (start end &optional file)
|
|
|
|
"Insert raw file bytes START to END and save time it takes.
|
|
|
|
FILE if given is filename to be used, otherwise `buffer-file-name'."
|
2014-08-31 02:10:28 +03:00
|
|
|
(let ((result (vlf-time (insert-file-contents-literally
|
2014-09-26 01:56:12 +03:00
|
|
|
(or file buffer-file-name) nil start end))))
|
2014-08-31 02:10:28 +03:00
|
|
|
(vlf-tune-add-measurement vlf-tune-insert-raw-bps
|
|
|
|
(- end start) (car result))
|
|
|
|
(cdr result)))
|
|
|
|
|
|
|
|
(defun vlf-tune-encode-length (start end)
|
|
|
|
"Get length of encoded region START to END and save time it takes."
|
|
|
|
(let ((result (vlf-time (length (encode-coding-region
|
|
|
|
start end
|
|
|
|
buffer-file-coding-system t)))))
|
|
|
|
(vlf-tune-add-measurement vlf-tune-encode-bps
|
2014-09-03 02:34:37 +03:00
|
|
|
(cdr result) (car result))
|
2014-08-31 02:10:28 +03:00
|
|
|
(cdr result)))
|
|
|
|
|
2014-10-08 19:33:59 +03:00
|
|
|
(defun vlf-tune-write (start end append visit size &optional file-name)
|
2014-08-31 02:10:28 +03:00
|
|
|
"Save buffer and save time it takes.
|
|
|
|
START, END, APPEND, VISIT have same meaning as in `write-region'.
|
2014-09-26 01:56:12 +03:00
|
|
|
SIZE is number of bytes that are saved.
|
2014-10-08 19:33:59 +03:00
|
|
|
FILE-NAME if given is to be used instead of `buffer-file-name'."
|
|
|
|
(let* ((file (or file-name buffer-file-name))
|
|
|
|
(time (car (vlf-time (write-region start end file append
|
|
|
|
visit)))))
|
|
|
|
(or (file-remote-p file) ;writing to remote files can include network copying
|
|
|
|
(vlf-tune-add-measurement vlf-tune-write-bps size time))))
|
2014-08-31 02:10:28 +03:00
|
|
|
|
2014-10-16 02:51:37 +03:00
|
|
|
(defun vlf-hexl-adjust-addresses ()
|
|
|
|
"Adjust hexl address indicators according to `vlf-start-pos'."
|
|
|
|
(let ((pos (point))
|
|
|
|
(address vlf-start-pos))
|
|
|
|
(goto-char (point-min))
|
|
|
|
(while (re-search-forward "^[0-9a-f]+" nil t)
|
|
|
|
(replace-match (format "%08x" address))
|
|
|
|
(setq address (+ address hexl-bits)))
|
|
|
|
(goto-char pos)))
|
|
|
|
|
2014-08-31 02:10:28 +03:00
|
|
|
(defun vlf-tune-hexlify ()
|
|
|
|
"Activate `hexl-mode' and save time it takes."
|
2014-10-16 02:51:37 +03:00
|
|
|
(let* ((no-adjust (zerop vlf-start-pos))
|
|
|
|
(time (car (vlf-time (hexlify-buffer)
|
|
|
|
(or no-adjust
|
|
|
|
(vlf-hexl-adjust-addresses))))))
|
2014-09-25 01:23:08 +03:00
|
|
|
(setq hexl-max-address (+ (* (/ (1- (buffer-size))
|
|
|
|
(hexl-line-displen)) 16) 15))
|
2014-10-16 02:51:37 +03:00
|
|
|
(or no-adjust
|
|
|
|
(vlf-tune-add-measurement vlf-tune-hexl-bps
|
|
|
|
hexl-max-address time))))
|
2014-08-31 02:10:28 +03:00
|
|
|
|
|
|
|
(defun vlf-tune-dehexlify ()
|
|
|
|
"Exit `hexl-mode' and save time it takes."
|
2014-09-25 01:23:08 +03:00
|
|
|
(let ((time (car (vlf-time (dehexlify-buffer)))))
|
|
|
|
(vlf-tune-add-measurement vlf-tune-dehexlify-bps
|
|
|
|
hexl-max-address time)))
|
2014-08-31 02:10:28 +03:00
|
|
|
|
2014-09-05 02:49:55 +03:00
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;;; tuning
|
|
|
|
|
2014-09-05 13:52:46 +03:00
|
|
|
(defun vlf-tune-approximate-nearby (vec index)
|
|
|
|
"VEC has value for INDEX, approximate to closest available."
|
|
|
|
(let ((val 0)
|
|
|
|
(left-idx (1- index))
|
|
|
|
(right-idx (1+ index))
|
2014-09-07 00:02:14 +03:00
|
|
|
(min-idx (max 0 (- index 5)))
|
|
|
|
(max-idx (min (+ index 6)
|
|
|
|
(1- (/ (min vlf-tune-max
|
|
|
|
(/ (1+ vlf-file-size) 2))
|
|
|
|
vlf-tune-step)))))
|
|
|
|
(while (and (zerop val) (or (<= min-idx left-idx)
|
|
|
|
(< right-idx max-idx)))
|
|
|
|
(if (<= min-idx left-idx)
|
|
|
|
(let ((left (aref vec left-idx)))
|
|
|
|
(cond ((consp left) (setq val (car left)))
|
|
|
|
((numberp left) (setq val left)))))
|
|
|
|
(if (< right-idx max-idx)
|
|
|
|
(let ((right (aref vec right-idx)))
|
|
|
|
(if (consp right)
|
|
|
|
(setq right (car right)))
|
|
|
|
(and (numberp right) (not (zerop right))
|
|
|
|
(setq val (if (zerop val)
|
|
|
|
right
|
|
|
|
(/ (+ val right) 2))))))
|
2014-09-05 14:44:24 +03:00
|
|
|
(setq left-idx (1- left-idx)
|
|
|
|
right-idx (1+ right-idx)))
|
2014-09-05 13:52:46 +03:00
|
|
|
val))
|
|
|
|
|
2014-09-07 00:02:14 +03:00
|
|
|
(defmacro vlf-tune-get-value (vec index &optional dont-approximate)
|
|
|
|
"Get value from VEC for INDEX.
|
|
|
|
If missing, approximate from nearby measurement,
|
|
|
|
unless DONT-APPROXIMATE is t."
|
2014-09-05 13:52:46 +03:00
|
|
|
`(if ,vec
|
2014-09-07 00:02:14 +03:00
|
|
|
(let ((val (aref ,vec ,index)))
|
|
|
|
(cond ((consp val) (car val))
|
|
|
|
((null val)
|
|
|
|
,(if dont-approximate
|
|
|
|
`(aset ,vec ,index 0)
|
|
|
|
`(vlf-tune-approximate-nearby ,vec ,index)))
|
|
|
|
((zerop val) ;index has been tried before, yet still no value
|
2014-09-07 16:25:36 +03:00
|
|
|
,(if dont-approximate
|
|
|
|
`(aset ,vec ,index
|
|
|
|
(vlf-tune-approximate-nearby ,vec ,index))
|
|
|
|
`(vlf-tune-approximate-nearby ,vec ,index)))
|
2014-09-25 01:23:08 +03:00
|
|
|
(t val)))
|
|
|
|
most-positive-fixnum))
|
2014-09-05 13:52:46 +03:00
|
|
|
|
2014-09-07 00:02:14 +03:00
|
|
|
(defmacro vlf-tune-get-vector (key)
|
|
|
|
"Get vlf-tune vector corresponding to KEY."
|
|
|
|
`(cond ((eq ,key :insert) vlf-tune-insert-bps)
|
|
|
|
((eq ,key :raw) vlf-tune-insert-raw-bps)
|
|
|
|
((eq ,key :encode) vlf-tune-encode-bps)
|
|
|
|
((eq ,key :write) vlf-tune-write-bps)
|
|
|
|
((eq ,key :hexl) vlf-tune-hexl-bps)
|
|
|
|
((eq ,key :dehexlify) vlf-tune-dehexlify-bps)))
|
|
|
|
|
|
|
|
(defun vlf-tune-assess (type coef index &optional approximate)
|
|
|
|
"Get measurement value according to TYPE, COEF and INDEX.
|
|
|
|
If APPROXIMATE is t, do approximation for missing values."
|
|
|
|
(* coef (or (if approximate
|
|
|
|
(vlf-tune-get-value (vlf-tune-get-vector type)
|
|
|
|
index)
|
|
|
|
(vlf-tune-get-value (vlf-tune-get-vector type)
|
|
|
|
index t))
|
2014-09-04 15:43:37 +03:00
|
|
|
0)))
|
2014-09-03 02:34:37 +03:00
|
|
|
|
2014-09-07 18:05:13 +03:00
|
|
|
(defun vlf-tune-score (types index &optional approximate time-max)
|
2014-09-07 00:02:14 +03:00
|
|
|
"Calculate cumulative speed over TYPES for INDEX.
|
2014-09-07 18:05:13 +03:00
|
|
|
If APPROXIMATE is t, do approximation for missing values.
|
|
|
|
If TIME-MAX is non nil, return cumulative time instead of speed.
|
|
|
|
If it is number, stop as soon as cumulative time gets equal or above."
|
2014-09-03 02:34:37 +03:00
|
|
|
(catch 'result
|
2014-09-05 02:49:55 +03:00
|
|
|
(let ((time 0)
|
2014-09-07 18:05:13 +03:00
|
|
|
(size (* (1+ index) vlf-tune-step))
|
|
|
|
(cut-time (numberp time-max)))
|
|
|
|
(dolist (el types (if time-max time
|
|
|
|
(/ size time)))
|
2014-09-04 15:24:55 +03:00
|
|
|
(let ((bps (if (consp el)
|
2014-09-07 00:02:14 +03:00
|
|
|
(vlf-tune-assess (car el) (cadr el) index
|
|
|
|
approximate)
|
2014-09-25 01:23:08 +03:00
|
|
|
(vlf-tune-assess el 1.0 index approximate))))
|
2014-09-04 15:24:55 +03:00
|
|
|
(if (zerop bps)
|
2014-09-03 02:34:37 +03:00
|
|
|
(throw 'result nil)
|
2014-09-07 18:05:13 +03:00
|
|
|
(setq time (+ time (/ size bps)))
|
|
|
|
(and cut-time (<= time-max time)
|
|
|
|
(throw 'result nil))))))))
|
2014-09-03 02:34:37 +03:00
|
|
|
|
|
|
|
(defun vlf-tune-conservative (types &optional index)
|
2014-09-05 02:49:55 +03:00
|
|
|
"Adjust `vlf-batch-size' to best nearby value over TYPES.
|
2014-09-03 02:34:37 +03:00
|
|
|
INDEX if given, specifies search independent of current batch size."
|
|
|
|
(if (eq vlf-tune-enabled t)
|
2014-09-05 19:11:48 +03:00
|
|
|
(let* ((half-max (/ (1+ vlf-file-size) 2))
|
2014-09-03 02:34:37 +03:00
|
|
|
(idx (or index (vlf-tune-closest-index vlf-batch-size)))
|
2014-09-05 02:49:55 +03:00
|
|
|
(curr (if (< half-max (* idx vlf-tune-step)) t
|
2014-09-03 02:34:37 +03:00
|
|
|
(vlf-tune-score types idx))))
|
2014-09-05 02:49:55 +03:00
|
|
|
(if curr
|
|
|
|
(let ((prev (if (zerop idx) t
|
|
|
|
(vlf-tune-score types (1- idx)))))
|
|
|
|
(if prev
|
|
|
|
(let ((next (if (or (eq curr t)
|
|
|
|
(< half-max (* (1+ idx)
|
|
|
|
vlf-tune-step)))
|
|
|
|
t
|
|
|
|
(vlf-tune-score types (1+ idx)))))
|
|
|
|
(cond ((null next)
|
|
|
|
(setq vlf-batch-size (* (+ 2 idx)
|
2014-09-04 15:24:55 +03:00
|
|
|
vlf-tune-step)))
|
2014-09-05 02:49:55 +03:00
|
|
|
((eq curr t)
|
|
|
|
(or (eq prev t)
|
|
|
|
(setq vlf-batch-size
|
|
|
|
(* idx vlf-tune-step))))
|
|
|
|
(t (let ((best-idx idx))
|
|
|
|
(and (numberp prev) (< curr prev)
|
|
|
|
(setq curr prev
|
|
|
|
best-idx (1- idx)))
|
|
|
|
(and (numberp next) (< curr next)
|
|
|
|
(setq best-idx (1+ idx)))
|
|
|
|
(setq vlf-batch-size
|
|
|
|
(* (1+ best-idx)
|
|
|
|
vlf-tune-step))))))
|
|
|
|
(setq vlf-batch-size (* idx vlf-tune-step))))
|
|
|
|
(setq vlf-batch-size (* (1+ idx) vlf-tune-step))))))
|
|
|
|
|
|
|
|
(defun vlf-tune-binary (types min max)
|
|
|
|
"Adjust `vlf-batch-size' to optimal value using binary search, \
|
|
|
|
optimizing over TYPES.
|
|
|
|
MIN and MAX specify interval of indexes to search."
|
2014-09-05 13:52:46 +03:00
|
|
|
(let ((sum (+ min max)))
|
|
|
|
(if (< (- max min) 3)
|
|
|
|
(vlf-tune-conservative types (/ sum 2))
|
|
|
|
(let* ((left-idx (round (+ sum (* 2 min)) 4))
|
|
|
|
(left (vlf-tune-score types left-idx)))
|
|
|
|
(if left
|
|
|
|
(let* ((right-idx (round (+ sum (* 2 max)) 4))
|
|
|
|
(right (vlf-tune-score types right-idx)))
|
|
|
|
(cond ((null right)
|
|
|
|
(setq vlf-batch-size (* (1+ right-idx)
|
|
|
|
vlf-tune-step)))
|
|
|
|
((< left right)
|
|
|
|
(vlf-tune-binary types (/ (1+ sum) 2) max))
|
|
|
|
(t (vlf-tune-binary types min (/ sum 2)))))
|
|
|
|
(setq vlf-batch-size (* (1+ left-idx) vlf-tune-step)))))))
|
2014-09-05 02:49:55 +03:00
|
|
|
|
|
|
|
(defun vlf-tune-linear (types max-idx)
|
2014-10-02 13:49:42 +03:00
|
|
|
"Adjust `vlf-batch-size' to optimal known value using linear search.
|
|
|
|
Optimize over TYPES up to MAX-IDX."
|
2014-09-05 02:49:55 +03:00
|
|
|
(let ((best-idx 0)
|
|
|
|
(best-bps 0)
|
2014-10-02 13:49:42 +03:00
|
|
|
(idx 0))
|
|
|
|
(while (< idx max-idx)
|
2014-10-07 01:33:21 +03:00
|
|
|
(let ((bps (vlf-tune-score types idx t)))
|
2014-10-02 13:49:42 +03:00
|
|
|
(and bps (< best-bps bps)
|
|
|
|
(setq best-idx idx
|
|
|
|
best-bps bps)))
|
2014-09-05 02:49:55 +03:00
|
|
|
(setq idx (1+ idx)))
|
2014-10-02 13:49:42 +03:00
|
|
|
(setq vlf-batch-size (* (1+ best-idx) vlf-tune-step))))
|
2014-09-05 02:49:55 +03:00
|
|
|
|
2014-09-26 14:00:01 +03:00
|
|
|
(defun vlf-tune-batch (types &optional linear file)
|
2014-09-05 02:49:55 +03:00
|
|
|
"Adjust `vlf-batch-size' to optimal value optimizing on TYPES.
|
|
|
|
TYPES is alist of elements that may be of form (type coef) or
|
|
|
|
non list values in which case coeficient is assumed 1.
|
|
|
|
Types can be :insert, :raw, :encode, :write, :hexl or :dehexlify.
|
2014-09-06 23:59:32 +03:00
|
|
|
If LINEAR is non nil, use brute-force. In case requested measurement
|
|
|
|
is missing, stop search and set `vlf-batch-size' to this value.
|
2014-09-26 14:00:01 +03:00
|
|
|
FILE if given is filename to be used, otherwise `buffer-file-name'.
|
2014-09-06 23:59:32 +03:00
|
|
|
Suitable for multiple batch operations."
|
2014-09-05 02:49:55 +03:00
|
|
|
(if (eq vlf-tune-enabled t)
|
2014-09-05 19:11:48 +03:00
|
|
|
(let ((max-idx (1- (/ (min vlf-tune-max
|
|
|
|
(/ (1+ vlf-file-size) 2))
|
2014-09-05 02:49:55 +03:00
|
|
|
vlf-tune-step))))
|
2014-10-07 14:18:34 +03:00
|
|
|
(if linear
|
|
|
|
(vlf-tune-linear types max-idx)
|
|
|
|
(let ((batch-size vlf-batch-size))
|
|
|
|
(cond ((file-remote-p (or file buffer-file-name))
|
|
|
|
(vlf-tune-conservative types))
|
|
|
|
((<= 1 max-idx)
|
|
|
|
(if (< max-idx 3)
|
|
|
|
(vlf-tune-conservative types (/ max-idx 2))
|
|
|
|
(vlf-tune-binary types 0 max-idx))))
|
|
|
|
(if (= batch-size vlf-batch-size) ;local maxima?
|
|
|
|
(vlf-tune-linear types max-idx)))))))
|
2014-09-03 02:34:37 +03:00
|
|
|
|
2014-09-07 22:10:56 +03:00
|
|
|
(defun vlf-tune-optimal-load (types &optional min-idx max-idx)
|
2014-09-07 18:05:13 +03:00
|
|
|
"Get best batch size according to existing measurements over TYPES.
|
2014-09-07 22:10:56 +03:00
|
|
|
Best considered where primitive operations total is closest to
|
|
|
|
`vlf-tune-load-time'. If MIN-IDX and MAX-IDX are given,
|
|
|
|
confine search to this region."
|
2014-09-26 15:01:27 +03:00
|
|
|
(if (eq vlf-tune-enabled t)
|
2014-09-08 02:21:09 +03:00
|
|
|
(progn
|
2014-09-25 01:23:08 +03:00
|
|
|
(setq min-idx (max 0 (or min-idx 0))
|
|
|
|
max-idx (min (or max-idx vlf-tune-max)
|
2014-09-08 12:36:35 +03:00
|
|
|
(1- (/ (min vlf-tune-max
|
|
|
|
(/ (1+ vlf-file-size) 2))
|
|
|
|
vlf-tune-step))))
|
2014-09-25 01:23:08 +03:00
|
|
|
(let* ((idx min-idx)
|
2014-09-08 02:21:09 +03:00
|
|
|
(best-idx idx)
|
|
|
|
(best-time-diff vlf-tune-load-time)
|
|
|
|
(all-less t)
|
|
|
|
(all-more t))
|
|
|
|
(while (and (not (zerop best-time-diff)) (< idx max-idx))
|
|
|
|
(let ((time-diff (vlf-tune-score types idx t
|
|
|
|
(+ vlf-tune-load-time
|
|
|
|
best-time-diff))))
|
2014-09-08 14:32:02 +03:00
|
|
|
(if time-diff
|
|
|
|
(progn
|
|
|
|
(setq time-diff (if (< vlf-tune-load-time time-diff)
|
|
|
|
(progn (setq all-less nil)
|
|
|
|
(- time-diff
|
|
|
|
vlf-tune-load-time))
|
|
|
|
(setq all-more nil)
|
|
|
|
(- vlf-tune-load-time time-diff)))
|
|
|
|
(if (< time-diff best-time-diff)
|
|
|
|
(setq best-idx idx
|
|
|
|
best-time-diff time-diff)))
|
|
|
|
(setq all-less nil)))
|
2014-09-08 02:21:09 +03:00
|
|
|
(setq idx (1+ idx)))
|
|
|
|
(* vlf-tune-step (1+ (cond ((or (zerop best-time-diff)
|
|
|
|
(eq all-less all-more))
|
|
|
|
best-idx)
|
|
|
|
(all-less max-idx)
|
|
|
|
(t min-idx))))))
|
|
|
|
vlf-batch-size))
|
2014-09-07 22:10:56 +03:00
|
|
|
|
|
|
|
(defun vlf-tune-load (types &optional region)
|
|
|
|
"Adjust `vlf-batch-size' slightly to better load time.
|
|
|
|
Optimize on TYPES on the nearby REGION. Use 2 if REGION is nil."
|
2014-09-08 02:21:09 +03:00
|
|
|
(when (eq vlf-tune-enabled t)
|
|
|
|
(or region (setq region 2))
|
|
|
|
(let ((idx (vlf-tune-closest-index vlf-batch-size)))
|
|
|
|
(setq vlf-batch-size (vlf-tune-optimal-load types (- idx region)
|
|
|
|
(+ idx 1 region))))))
|
2014-09-07 00:08:32 +03:00
|
|
|
|
2014-08-31 02:10:28 +03:00
|
|
|
(provide 'vlf-tune)
|
|
|
|
|
|
|
|
;;; vlf-tune.el ends here
|