APPROVE COMMIT
NOTE: This patch has been committed
# HG changeset patch
# User Aidan Kehoe <kehoea(a)parhasard.net>
# Date 1534105666 -3600
# Sun Aug 12 21:27:46 2018 +0100
# Node ID 94cedc3ecd89c2c50ea4e3b1d5774459049f90af
# Parent 22544df6c9205a9d08fb9405b22d227ac98b3866
Improve the Lisp implementation of weak boxes; document it some more.
lisp/ChangeLog addition:
2018-08-12 Aidan Kehoe <kehoea(a)parhasard.net>
* misc.el:
* misc.el (weak-box-p):
* misc.el (make-weak-box):
* misc.el (weak-box-ref):
Improve the implementation here; make #'weak-box-ref free in terms
of garbage, make weak boxes that reference distinct objects print
distinctly, give information as to the type of CONTENTS stored.
Document some more on the performance and debugging implications
of the Lisp implementation.
diff -r 22544df6c920 -r 94cedc3ecd89 lisp/ChangeLog
--- a/lisp/ChangeLog Sun Aug 12 18:30:18 2018 +0100
+++ b/lisp/ChangeLog Sun Aug 12 21:27:46 2018 +0100
@@ -1,3 +1,15 @@
+2018-08-12 Aidan Kehoe <kehoea(a)parhasard.net>
+
+ * misc.el:
+ * misc.el (weak-box-p):
+ * misc.el (make-weak-box):
+ * misc.el (weak-box-ref):
+ Improve the implementation here; make #'weak-box-ref free in terms
+ of garbage, make weak boxes that reference distinct objects print
+ distinctly, give information as to the type of CONTENTS stored.
+ Document some more on the performance and debugging implications
+ of the Lisp implementation.
+
2018-08-12 Aidan Kehoe <kehoea(a)parhasard.net>
* misc.el:
diff -r 22544df6c920 -r 94cedc3ecd89 lisp/misc.el
--- a/lisp/misc.el Sun Aug 12 18:30:18 2018 +0100
+++ b/lisp/misc.el Sun Aug 12 21:27:46 2018 +0100
@@ -65,13 +65,19 @@
;;; Weak boxes, formerly in data.c, but never used enough to merit a C
-;;; implementation.
+;;; implementation. There is minimal performance and GC impact to this Lisp
+;;; implementation (GC performance actually improves, as in the vast majority
+;;; of the time, when there are no weak boxes, prune_weak_boxes() isn't
+;;; called.) In debugging terms, however, there is no error on attempting to
+;;; print readably, as the C implementation gave, and the print method isn't
+;;; as pretty as it was with the C version.
-(autoload 'all-weak-boxes-list "misc")
+;; Quiet the compiler about calls to this:
+(autoload 'weak-box-ref-1 "misc")
(defun weak-box-p (object)
"Return non-nil if OBJECT is a weak box."
- (and (vectorp object) (eql (length object) 1)
+ (and (vectorp object) (eql (length object) 3)
(eq 'cl-weak-box (aref object 0))))
(defun make-weak-box (contents)
@@ -80,23 +86,28 @@
`weak-box-ref'. However, the weak box does not contribute to the
reachability of CONTENTS. When CONTENTS is garbage-collected,
`weak-box-ref' will return NIL."
- (caar (set-weak-list-list
- (load-time-value
- (progn
- (defvar #1=#:all-weak-boxes (make-weak-list 'assoc))
- (defalias 'all-weak-boxes-list
- ;; If the weak box code is actually used, this #'copy-list
- ;; might be an issue in terms of GC. It isn't, currently, and
- ;; so the protection against other callers modifying the list
- ;; out from under the feet of our code is preferred.
- #'(lambda () (copy-list (weak-list-list #1#))))
- #1#))
- (acons (vector 'cl-weak-box) contents (all-weak-boxes-list)))))
+ (symbol-macrolet ((all-weak-boxes #:all-weak-boxes))
+ (defvar all-weak-boxes)
+ (caar (set-weak-list-list
+ (load-time-value
+ (progn
+ (setq all-weak-boxes (make-weak-list 'assoc))
+ (defalias 'weak-box-ref-1
+ #'(lambda (weak-box)
+ (cdr (assq weak-box (weak-list-list all-weak-boxes)))))
+ all-weak-boxes))
+ ;; The #'eq-hash call isn't necessary, but it does mean that weak
+ ;; boxes that refer to distinct objects print distinctly. The
+ ;; #'type-of call is intended to give us a bit more context as to
+ ;; what's going on, without the need to fire up GDB to convert the
+ ;; eq-hash to an address.
+ (acons (vector 'cl-weak-box (type-of contents) (eq-hash contents))
+ contents (weak-list-list all-weak-boxes))))))
(defun weak-box-ref (weak-box)
"Return the contents of weak box WEAK-BOX.
If the contents have been GCed, return NIL."
(check-argument-type 'weak-box-p weak-box)
- (cdr (assq weak-box (all-weak-boxes-list))))
+ (weak-box-ref-1 weak-box))
;;; misc.el ends here
--
‘As I sat looking up at the Guinness ad, I could never figure out /
How your man stayed up on the surfboard after forty pints of stout’
(C. Moore)