1
Fork 0
mirror of git://git.sv.gnu.org/emacs.git synced 2026-04-21 05:21:37 -07:00

Minor optimization

* lisp/emacs-lisp/igc.el (igc--format-bytes-human-readable): Avoid some
consing.
This commit is contained in:
Helmut Eller 2025-10-04 12:50:26 +02:00
parent 1b3a8a0099
commit c5f80fa076

View file

@ -153,22 +153,26 @@ IGC statistics:
(defun igc--format-bytes-human-readable (n)
(let* ((negative (< n 0))
(n (abs n))
(units '((10 . "K")
(20 . "M")
(30 . "G")
(40 . "T")
(50 . "P")
(60 . "E")))
(p (cl-assoc-if (lambda (key) (<= (ash 1 key) n))
(reverse units)))
(unit-bits (if p (car p) 0))
(unit-name (if p (cdr p) ""))
(d (ash 1 unit-bits))
(r (mod n d)))
(format "%s%s%s" (if negative "-" "")
(cond ((= r 0) (number-to-string (/ n d)))
(t (format "%.1f" (/ (float n) d))))
unit-name)))
(units (eval-when-compile
(cl-map 'vector
(lambda (p) (cons (ash 1 (car p)) (cdr p)))
'((10 . "K")
(20 . "M")
(30 . "G")
(40 . "T")
(50 . "P")
(60 . "E")))))
(pos (cl-position-if (lambda (p) (< n (car p))) units))
(p (cl-case pos
((nil 0) '(1 . ""))
(t (aref units (1- pos)))))
(base (car p))
(name (cdr p))
(rem (mod n base)))
(concat (if negative "-" "")
(cond ((= rem 0) (number-to-string (/ n base)))
(t (format "%.1f" (/ (float n) base))))
name)))
(defun igc--format-bytes (n)
(cl-ecase igc--number-format