Skip to content

Commit 7278619

Browse files
committed
Task: Add stream.el benchmarks
1 parent 19687e7 commit 7278619

File tree

2 files changed

+309
-6
lines changed

2 files changed

+309
-6
lines changed

README.org

Lines changed: 144 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3575,10 +3575,153 @@ Yes, please! Send pull requests and file issues on the [[https://github.com/alp
35753575

35763576
These resources should be added to the appropriate sections above. Since it takes some work to catalog and organize them, they are dumped here for future reference. Pull requests for these are welcome!
35773577

3578+
** TODO stream.el benchmarks
3579+
3580+
[2020-11-03 Tue 15:27] It's very surprising that =stream= seems faster than a plain =cl-loop=. I wonder if I'm doing something wrong...
3581+
3582+
Also see:
3583+
3584+
+ [[https://www.reddit.com/r/emacs/comments/jmvzhs/has_anything_of_substance_ever_been_done_in_elisp/][Has anything of substance ever been done in elisp using lazy data structures? : emacs]]
3585+
3586+
#+BEGIN_SRC elisp
3587+
(bench-multi-lexical :times 100 :ensure-equal t
3588+
:forms (("cl-loop"
3589+
(let* ((buffer (find-file-noselect "~/org/main.org"))
3590+
(regexp (rx bow "Emacs" eow)))
3591+
(with-current-buffer buffer
3592+
(goto-char (point-min))
3593+
(length (cl-loop while (re-search-forward regexp nil t)
3594+
collect (match-string-no-properties 0))))))
3595+
3596+
("stream-regexp/seq-do/push"
3597+
(let* ((buffer (find-file-noselect "~/org/main.org"))
3598+
(regexp (rx bow "Emacs" eow))
3599+
(stream (stream-regexp buffer regexp))
3600+
result)
3601+
(with-current-buffer buffer
3602+
(goto-char (point-min))
3603+
(seq-do (lambda (_match)
3604+
(push (match-string-no-properties 0) result))
3605+
stream)
3606+
(length result))))
3607+
3608+
("stream-regexp/cl-loop"
3609+
(let* ((buffer (find-file-noselect "~/org/main.org"))
3610+
(regexp (rx bow "Emacs" eow))
3611+
(stream (stream-regexp buffer regexp)))
3612+
(with-current-buffer buffer
3613+
(goto-char (point-min))
3614+
(length (cl-loop while (stream-pop stream)
3615+
collect (match-string-no-properties 0))))))))
3616+
#+END_SRC
3617+
3618+
#+RESULTS:
3619+
| Form | x faster than next | Total runtime | # of GCs | Total GC runtime |
3620+
|---------------------------+--------------------+---------------+----------+------------------|
3621+
| stream-regexp/cl-loop | 1.03 | 1.305895 | 1 | 0.193277 |
3622+
| stream-regexp/seq-do/push | 1.42 | 1.350986 | 1 | 0.199524 |
3623+
| cl-loop | slowest | 1.925070 | 0 | 0 |
3624+
3625+
[2020-11-03 Tue 16:57] Other stream-related tests (these methods are bespoke):
3626+
3627+
*** Buffer lines
3628+
3629+
The benchmark macros need to be extended to allow definitions that aren't part of the benchmarked code.
3630+
3631+
#+BEGIN_SRC elisp
3632+
(cl-defmethod stream-lines ((buffer buffer) &optional pos no-properties)
3633+
"Return a stream of the lines of the buffer BUFFER.
3634+
BUFFER may be a buffer or a string (buffer name).
3635+
The sequence starts at POS if non-nil, `point-min' otherwise."
3636+
;; Copied from the buffer method.
3637+
(let ((fn (if no-properties
3638+
#'buffer-substring-no-properties
3639+
#'buffer-substring)))
3640+
(with-current-buffer buffer
3641+
(unless pos (setq pos (point-min)))
3642+
(if (>= pos (point-max))
3643+
(stream-empty))
3644+
(stream-cons
3645+
(with-current-buffer buffer
3646+
(save-excursion
3647+
(save-restriction
3648+
(widen)
3649+
(goto-char pos)
3650+
(prog1 (funcall fn (point-at-bol) (point-at-eol))
3651+
(setf pos (progn
3652+
(forward-line 1)
3653+
(point)))))))
3654+
(stream-lines buffer pos no-properties)))))
3655+
3656+
(bench-multi-lexical :times 100 :ensure-equal t
3657+
:forms (("stream-lines/seq-take/seq-into"
3658+
(let* ((buffer (find-file-noselect "~/org/main.org"))
3659+
(stream (stream-lines buffer nil 'no-properties)))
3660+
(seq-into (seq-take stream 10) 'list)))
3661+
3662+
("cl-loop"
3663+
(let* ((buffer (find-file-noselect "~/org/main.org"))
3664+
(no-properties t))
3665+
(with-current-buffer buffer
3666+
(save-excursion
3667+
(save-restriction
3668+
(widen)
3669+
(goto-char (point-min))
3670+
(cl-loop for fn = (if no-properties
3671+
#'buffer-substring-no-properties
3672+
#'buffer-substring)
3673+
collect (funcall fn (point-at-bol) (point-at-eol))
3674+
do (forward-line 1)
3675+
until (eobp)))))))))
3676+
#+END_SRC
3677+
3678+
*** String lines
3679+
3680+
The benchmark macros need to be extended to allow definitions that aren't part of the benchmarked code. I'm not sure how I even got those results because it's not working now...
3681+
3682+
#+BEGIN_SRC elisp
3683+
(cl-defmethod stream-lines ((string string) &optional pos no-properties)
3684+
"Return a stream of the lines of the string STRING.
3685+
The sequence starts at POS if non-nil, 0 otherwise."
3686+
;; Copied from the buffer method.
3687+
(unless pos (setq pos 0))
3688+
(let ((fn (if no-properties
3689+
#'buffer-substring-no-properties
3690+
#'buffer-substring))
3691+
(eol (when (string-match "\n" string pos)
3692+
(match-beginning 0))))
3693+
(stream-cons
3694+
(seq-subseq string pos eol)
3695+
(if (not eol)
3696+
(stream-empty)
3697+
(stream-lines string (1+ eol) no-properties)))))
3698+
3699+
(bench-multi-lexical :times 100 :ensure-equal t
3700+
:forms (("stream-lines/seq-into"
3701+
(let* ((string "abcd
3702+
efgh
3703+
hijk
3704+
zz"))
3705+
(seq-into (stream-lines string nil 'no-properties) 'list)))
3706+
3707+
("s-lines"
3708+
(let* ((string "abcd
3709+
efgh
3710+
hijk
3711+
zz"))
3712+
(s-lines string)))))
3713+
#+END_SRC
3714+
3715+
#+RESULTS:
3716+
| Form | x faster than next | Total runtime | # of GCs | Total GC runtime |
3717+
|-----------------+--------------------+---------------+----------+------------------|
3718+
| s-lines | 10.47 | 0.000513 | 0 | 0 |
3719+
| stream/seq-into | slowest | 0.005370 | 0 | 0 |
3720+
3721+
35783722
** TODO Mention Emacs 27.1 SVG screenshots
35793723

35803724
e.g. https://www.reddit.com/r/emacs/comments/idz35e/emacs_27_can_take_svg_screenshots_of_itself/
3581-
35823725
** TODO Add section about files
35833726

35843727
e.g. saving files to disk.

0 commit comments

Comments
 (0)