gnu-emacs-sources
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

primitive-undo in Lisp


From: Aaron S. Hawley
Subject: primitive-undo in Lisp
Date: Fri, 23 Nov 2012 12:32:23 -0500

I've translated `primitive-undo' from C to Lisp.  This function is the
engine behind the `undo' command in Emacs -- C-/, C-x u and so on.
I've purposely cargo-culted the original C version, including carrying
over the comments and the parts that I believe are vestigial.  I've
been using and maintaining this code since 2010 and haven't had any
issues.  I've added a default condition to the while-loop that calls
`undefined' -- ringing the bell in Emacs -- for my own purposes, but
it should be removed.  I'm happy to contribute this back to Emacs in
the hope of excising 200 lines of C code from undo.c and adding 150
lines of Lisp to simple.el.

Install the Lisp version by putting the following snippet in your
.emacs, and putting the library primundo.el (attached) in your
load-path.

(unless (fboundp 'primitive-undo-C)
  (fset 'primitive-undo-C (symbol-function 'primitive-undo))
  (fmakunbound 'primitive-undo))
;; Roll-back the above with:
;; (fset 'primitive-undo (symbol-function 'primitive-undo-C))

(autoload 'primitive-undo "primundo"
 "Undo N records from the front of the list LIST.

\(fn n list)")

I wrote 5 tests that cover most of the execution paths and behavior.
These tests call `primitive-undo' 23 times.  I've benchmarked the Lisp
and C versions using benchmark.el end `elp-instrument-function' by
running these tests 100 times.  The elapsed time of these tests on my
computer for both versions of `primitive-undo' is approximately 200
seconds.

(benchmark 100 '(undo-test-all)) ;; C `primitive-undo'
=> Elapsed time: 199.088000s (69.875000s in 14605 GCs)
(benchmark 100 '(undo-test-all)) ;; Lisp `primitive-undo'
=> Elapsed time: 197.356000s (71.113000s in 14600 GCs)

Out of those 3 minutes, less than 4 seconds is attributable to
`primitive-undo'.  This averages to 1/600th (0.0016) of a second per
call of `primitive-undo' for both versions.

Function Name        Call Count  Elapsed Time  Average Time
=============        ==========  ============  ============
primitive-undo C     2300        3.8009999999  0.0016526086
primitive-undo Lisp  2300        3.6029999999  0.0015665217

Send me comments off-list if you come across any issues with using it.

Happy undoing,
aaron

Attachment: primundo.el
Description: Binary data


reply via email to

[Prev in Thread] Current Thread [Next in Thread]