Paste number 12880: flet and with-recursive-lock performance

Index of paste annotations: 1 | 2 | 3

Paste number 12880: flet and with-recursive-lock performance
Pasted by: mega
When:9 years, 1 month ago
Share:Tweet this! | http://paste.lisp.org/+9XS
Channel:#lisp
Paste contents:
Raw Source | XML | Display As
(defmacro with-recursive-lock1 ((mutex) &body body)
  (sb-impl::with-unique-names (body-fun mutex1)
    `(flet ((,body-fun ()
              ,@body))
       (declare (dynamic-extent ,body-fun))
       (let ((,mutex1 ,mutex))
         ;; Optimize for the case when the lock is already held by
         ;; this thread, avoid UNWIND-PROTECT.
         (if (eq (sb-thread:mutex-value ,mutex1) sb-thread:*current-thread*)
             (,body-fun)
             (unwind-protect
                  (progn
                    (sb-thread:get-mutex ,mutex1)
                    (,body-fun))
               (sb-thread:release-mutex ,mutex1)))))))

(defmacro with-recursive-lock2 ((mutex) &body body)
  (sb-impl::with-unique-names (mutex1)
    `(let ((,mutex1 ,mutex))
       ;; Optimize for the case when the lock is already held by
       ;; this thread, avoid UNWIND-PROTECT.
       (if (eq (sb-thread:mutex-value ,mutex1) sb-thread:*current-thread*)
           (progn ,@body)
           (unwind-protect
                (progn
                  (sb-thread:get-mutex ,mutex1)
                  ,@body)
             (sb-thread:release-mutex ,mutex1))))))

(defparameter *mutex* (sb-thread:make-mutex))

(defun test1 ()
  (with-recursive-lock1 (*mutex*)
    (loop repeat 40000000 do
          (with-recursive-lock1 (*mutex*)))))

(defun test2 ()
  (with-recursive-lock2 (*mutex*)
    (loop repeat 40000000 do
          (with-recursive-lock2 (*mutex*)))))

(time (test1))
(time (test2))

;;;; (time (test1)) ...
Evaluation took:
  1.007 seconds of real time
  1.002848 seconds of user run time
  0.0 seconds of system run time
  0 page faults and
  0 bytes consed.
;;;; (time (test2)) ...
Evaluation took:
  0.533 seconds of real time
  0.533919 seconds of user run time
  0.0 seconds of system run time
  0 page faults and
  0 bytes consed.

Annotations for this paste:

Annotation number 1: another test
Pasted by: nikodemus
When:9 years, 1 month ago
Share:Tweet this! | http://paste.lisp.org/+9XS/1
Paste contents:
Raw Source | Display As
(defmacro with-flet (&body body)
  `(flet ((exec () ,@body))
     (if (test)
         (exec)
         (unwind-protect
              (progn
                (setup)
                (exec))
           (teardown)))))

(defmacro with-inline (&body body)
  `(if (test)
       (progn ,@body)
       (unwind-protect
            (progn
              (setup)
              ,@body)
         (teardown))))

(defun test ()
  (zerop (random 3)))

(defun setup ())
(defun teardown ())

(defun quux ())
(defun foo ())
(defun bar ())

(defun test-flet (n)
  (loop repeat n
        do (with-flet (quux) (foo) (bar))))

(defun test-inline (n)
  (loop repeat n
        do (with-inline (quux) (foo) (bar))))

CL-USER> (time (test-inline (expt 10 7)))                                       
Evaluation took:                                                                
  0.908 seconds of real time                                                    
  0.91 seconds of user run time                                                 
  0.0 seconds of system run time                                                
  0 page faults and                                                             
  0 bytes consed.                                                               
NIL
CL-USER> (time (test-flet (expt 10 7)))                                         
Evaluation took:                                                                
  0.893 seconds of real time                                                    
  0.89 seconds of user run time                                                 
  0.0 seconds of system run time                                                
  0 page faults and                                                             
  0 bytes consed.                                                               
NIL

Annotation number 2: x86 results for 'another test'
Pasted by: mega
When:9 years, 1 month ago
Share:Tweet this! | http://paste.lisp.org/+9XS/2
Paste contents:
Raw Source | Display As
CL-USER> (time (test-inline (expt 10 7)))
Evaluation took:
  0.413 seconds of real time
  0.402938 seconds of user run time
  0.0 seconds of system run time
  0 page faults and
  0 bytes consed.
CL-USER> (time (test-flet (expt 10 7)))
Evaluation took:
  0.412 seconds of real time
  0.401939 seconds of user run time
  0.0 seconds of system run time
  0 page faults and
  0 bytes consed.

Annotation number 3: inlining foo makes it slower?
Pasted by: mega
When:9 years, 1 month ago
Share:Tweet this! | http://paste.lisp.org/+9XS/3
Paste contents:
Raw Source | Display As
;; this is the distillation of the above

(defmacro with-recursive-lock1 (&body body)
  `(flet ((body-fun ()
            ,@body))
     (if *print-level*
         (body-fun)
         (body-fun))))

(defparameter *x* 0)

(defun foo ()
  (setq *x* 42))

(defun test1 ()
  (loop repeat 40000000 do
        (with-recursive-lock1
          (setq *x* 42))))

(defun test2 ()
  (loop repeat 40000000 do
        (with-recursive-lock1
           (foo))))

;; (defun foo ()
;;   (setq *x* 42))

(dolist (test '(test1 test2))
  (gc)
  (format t "Testing: ~A~%" test)
  (time (funcall test)))

;; With the second definition of FOO active, it is not inlined and
;; TEST2 is faster than TEST1!

;; Testing: TEST1
;; Evaluation took:
;;   0.91 seconds of real time
;;   0.907862 seconds of user run time
;;   0.0 seconds of system run time
;;   0 page faults and
;;   0 bytes consed.
;; Testing: TEST2
;; Evaluation took:
;;   0.717 seconds of real time
;;   0.715891 seconds of user run time
;;   0.0 seconds of system run time
;;   0 page faults and
;;   0 bytes consed.

;; With the first FOO:

;; Testing: TEST1
;; Evaluation took:
;;   0.915 seconds of real time
;;   0.911862 seconds of user run time
;;   0.002 seconds of system run time
;;   0 page faults and
;;   0 bytes consed.
;; Testing: TEST2
;; Evaluation took:
;;   1.25 seconds of real time
;;   1.24781 seconds of user run time
;;   0.0 seconds of system run time
;;   0 page faults and
;;   0 bytes consed.

Colorize as:
Show Line Numbers

Lisppaste pastes can be made by anyone at any time. Imagine a fearsomely comprehensive disclaimer of liability. Now fear, comprehensively.