Skip to content

Commit

Permalink
Replace recursive sum calculation
Browse files Browse the repository at this point in the history
Elisp has a limited stack depth and with enough modules loaded
benchmark-init/show-durations-tree would fail due to running out of
stack.

Fixes #3
  • Loading branch information
dholm committed Sep 5, 2015
1 parent 646875c commit 8e4c32f
Showing 1 changed file with 5 additions and 14 deletions.
19 changes: 5 additions & 14 deletions benchmark-init.el
Expand Up @@ -90,18 +90,8 @@ Slots:
"Calculate the number of milliseconds that have elapsed between B and A."
(* 1000.0 (float-time (time-subtract b a))))

(defun benchmark-init/node-foldr (f base node)
"Apply binary function F to BASE and NODE."
(cond ((null node) base)
((benchmark-init/node-p node)
(let ((children (benchmark-init/node-children node)))
(funcall f node (benchmark-init/node-foldr f base children))))
(t (let ((x (car node))
(xs (cdr node)))
(funcall f x (benchmark-init/node-foldr f base xs))))))

(defun benchmark-init/flatten (node)
"Flatten NODE into an association list."
"Flatten NODE into a property list."
(let ((node-alist `((:name . ,(benchmark-init/node-name node))
(:type . ,(benchmark-init/node-type node))
(:duration . ,(benchmark-init/node-duration node))
Expand All @@ -128,9 +118,10 @@ Slots:

(defun benchmark-init/sum-node-durations (nodes)
"Return the sum of NODES durations."
(benchmark-init/node-foldr (lambda (x base)
(+ (benchmark-init/node-duration x) base))
0 nodes))
(let ((accum 0)
(f (lambda (x base) (+ (benchmark-init/node-duration x) base))))
(dolist (node nodes accum)
(setq accum (+ (benchmark-init/node-duration node) accum)))))

;; Benchmark helpers

Expand Down

0 comments on commit 8e4c32f

Please sign in to comment.