-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsideline-load-cost.el
169 lines (137 loc) · 5.71 KB
/
sideline-load-cost.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
;;; sideline-load-cost.el --- Display load/require module size with sideline -*- lexical-binding: t; -*-
;; Copyright (C) 2024-2025 Shen, Jen-Chieh
;; Author: Shen, Jen-Chieh <jcs090218@gmail.com>
;; Maintainer: Shen, Jen-Chieh <jcs090218@gmail.com>
;; URL: https://github.com/emacs-sideline/sideline-load-cost
;; Version: 0.1.0
;; Package-Requires: ((emacs "28.1") (sideline "0.1.0"))
;; Keywords: help
;; This file is not part of GNU Emacs.
;; This program 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 of the License, or
;; (at your option) any later version.
;; This program 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 this program. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;;
;; Display load/require module size with sideline.
;;
;; 1) Add sideline-load-cost to sideline backends list,
;;
;; (setq sideline-backends-right '(sideline-load-cost))
;;
;; 2) Then enable sideline-mode in the target buffer,
;;
;; M-x sideline-mode
;;
;;; Code:
(require 'benchmark)
(require 'find-func)
(require 'sideline)
(defgroup sideline-load-cost nil
"Display load/require module size with sideline."
:prefix "sideline-load-cost-"
:group 'tool
:link '(url-link :tag "Repository" "https://github.com/emacs-sideline/sideline-load-cost"))
(defface sideline-load-cost
'((t :foreground "#D14D3B"))
"Face for load cost in sideline."
:group 'sideline-load-cost)
;;
;;; Externals
(defvar features)
;;
;;; Util
(defun sideline-load-cost--comment-p ()
"Return non-nil if it's inside comment or string."
(nth 4 (syntax-ppss)))
(defun sideline-load-cost--file-size (filename)
"Return the FILENAME size."
(when-let (((file-readable-p filename))
(attributes (file-attributes filename)))
(file-attribute-size attributes)))
(defun sideline-load-cost--find-size (filename &optional ext)
"Return FILENAME's (extension . size).
Optional argument EXT is use to drop-in replace the current extension."
(when-let* ((filename (if ext (file-name-sans-extension filename)
filename))
(filename (concat filename ext))
(size (sideline-load-cost--file-size filename)))
(cons (or ext (file-name-extension filename))
size)))
;;
;;; Standard
(defvar sideline-load-cost--standard-time
(benchmark-run 1 (let ((features)) (require 'sideline)))
"Standard time use to measure average load time.")
(defvar sideline-load-cost--standard-file-size
(let ((lib (find-library-name "sideline")))
(sideline-load-cost--file-size lib))
"Standard time use to measure average load time.")
(defun sideline-load-cost--measure-load-time (size)
"Measure load time cost by file's SIZE."
(* (/ (float size) sideline-load-cost--standard-file-size)
(car sideline-load-cost--standard-time)))
;;
;;; Prefix
(defconst sideline-load-cost--load-names '("require" "load" "load-file")
"List of load operations.")
(defun sideline-load-cost--statement-p (line)
"Return non-nil if LINE has the load operations."
(cl-some (lambda (op)
(and (string-prefix-p (concat "(" op " ") line)
op))
sideline-load-cost--load-names))
;;
;;; Core
(defun sideline-load-cost--find-cost ()
"Return the cost text."
(when-let (((and (memq major-mode '(emacs-lisp-mode lisp-interaction-mode))
(not (sideline-load-cost--comment-p))))
(line (string-trim (thing-at-point 'line)))
(op (sideline-load-cost--statement-p line))
(bol (line-end-position)))
(save-excursion
(beginning-of-line)
(when (search-forward op bol t)
(forward-word 1)
(when-let* (((<= (point) bol))
(thing (or (thing-at-point 'string)
(thing-at-point 'symbol)))
(thing (if (stringp thing)
(sideline--s-replace "\"" "" thing)
thing))
(filename
(cond
((and (stringp thing) (file-exists-p thing))
(expand-file-name thing))
(t (ignore-errors (find-library-name (format "%s" thing)))))))
(let* ((size-data (or (sideline-load-cost--find-size filename ".el.gz")
(sideline-load-cost--find-size filename ".elc")
(sideline-load-cost--find-size filename ".el")
(sideline-load-cost--find-size filename)))
(size (cdr size-data))
(ext (car size-data))
(text-size (and size
(format "%s: %s" ext (file-size-human-readable size))))
(time (sideline-load-cost--measure-load-time size))
(text-time (format "(%.2fs)" time)))
(concat text-size " " text-time)))))))
;;;###autoload
(defun sideline-load-cost (command)
"Backend for sideline.
Argument COMMAND is required in sideline backend."
(cl-case command
(`candidates (cons :async #'sideline-load-cost--show))))
(defun sideline-load-cost--show (callback &rest _)
"Execute CALLBACK to display with sideline."
(when-let* ((text (sideline-load-cost--find-cost)))
(add-face-text-property 0 (length text) 'sideline-load-cost nil text)
(funcall callback (list text))))
(provide 'sideline-load-cost)
;;; sideline-load-cost.el ends here