Skip to content

Commit

Permalink
fix: boxing
Browse files Browse the repository at this point in the history
  • Loading branch information
g000001 committed Apr 5, 2020
1 parent 68d63a2 commit c56099a
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 12 deletions.
1 change: 0 additions & 1 deletion package.lisp
Expand Up @@ -17,7 +17,6 @@
(:use
"https://github.com/g000001/srfi-45"
cl
trivial-garbage
fiveam
mbe)
(:shadow loop))
Expand Down
4 changes: 3 additions & 1 deletion srfi-45.asd
Expand Up @@ -11,7 +11,9 @@ https://srfi.schemers.org/srfi-45"
:author "Andre van Tonder"
:maintainer "CHIBA Masaomi"
:serial t
:depends-on (:trivial-garbage
:depends-on (
:com.informatimago.clext.closer-weak
:trivial-timeout
:mbe
:fiveam)
:components ((:file "package")
Expand Down
31 changes: 26 additions & 5 deletions srfi-45.lisp
Expand Up @@ -5,13 +5,19 @@
;=========================================================================
; Boxes

(declaim (inline box unbox set-box!))
(declaim (notinline box unbox set-box!))

(defun box (x) (make-weak-pointer (list x)))
(defun unbox (box) (car (weak-pointer-value box)))
(defun set-box! (box item)
;(defun box (x) (make-weak-pointer (list x)))
(defun box (x) (list x))

;(defun unbox (box) (car (weak-pointer-value box)))
(defun unbox (box) (car box))
#|(defun set-box! (box item)
(rplaca (weak-pointer-value box)
item))
item))|#
(defun set-box! (box item)
(rplaca box item))


;=========================================================================
; Primitives for lazy evaluation:
Expand Down Expand Up @@ -42,4 +48,19 @@
; the first line of the let* caused it to be forced. For an example
; where this happens, see reentrancy test 3 below.

#|(defun force (promise)
(prog (content promise*)
L (setq content (unbox promise))
(return
(case (car content)
((eager) (cdr content))
((lazy)
(setq promise* (funcall (cdr content)))
(setq content (unbox promise))
(when (not (eql (car content) 'eager)) ; *
(setf (car content) (car (unbox promise*)))
(setf (cdr content) (cdr (unbox promise*)))
(set-box! promise* content))
(go L))))))|#

;;; eof
16 changes: 11 additions & 5 deletions test.lisp
Expand Up @@ -9,11 +9,9 @@
(get-output-stream-string ,out))))

(defmacro until-stack-exhausted-or-timeout (&body body)
#+sbcl `(handler-case (sb-ext:with-timeout 1 ,@body)
(sb-kernel::control-stack-exhausted ()
'stack-exhausted)
(sb-ext:timeout () 'timeout))
#-sbcl t)
`(handler-case (trivial-timeout:with-timeout (1) ,@body)
(trivial-timeout:timeout-error () 'timeout)
(error () 'stack-exhausted)))

;=========================================================================
; TESTS AND BENCHMARKS:
Expand Down Expand Up @@ -239,3 +237,11 @@
(force (times3 100000000))))))

;;; eof


;(force (times3 1000))

#|(dotimes (i 1000)
(print (force (stream-ref (from 0) i))))|#

;(cl:loop :for i :from 0 :repeat 1000 :collect )

0 comments on commit c56099a

Please sign in to comment.