[armedbear-devel] Patch that fixes ticket #187 (Stack Overflow for Worst-case Vector Sort)
jorge.tavares at ieee.org
Sat Feb 4 10:57:01 UTC 2012
I'm sending a patch that solves the issue reported in ticket #187. The quicksort function picks the pivot by selecting a midpoint and also sorts the smaller partition first. These are enough to avoid the stack overflow problem as reported. I've performed some tests and it looks it is correct.
Below I include a sample session, including the test case in the ticket:
idesk:abcl jast$ ./abcl
Armed Bear Common Lisp 1.1.0-dev-svn-13848M
Java 1.6.0_29 Apple Inc.
Java HotSpot(TM) 64-Bit Server VM
Low-level initialization completed in 0.307 seconds.
Startup completed in 0.945 seconds.
Loading /Users/jast/.abclrc completed in 4.528 seconds.
Type ":help" for a list of available commands.
CL-USER(1): (ql:quickload "alexandria")
To load "alexandria":
Load 1 ASDF system:
; Loading "alexandria"
CL-USER(2): (defparameter *sorted* (make-array 1000000 :initial-contents (alexandria:iota 1000000)))
CL-USER(3): (defparameter *numbers* (alexandria:shuffle (copy-seq *sorted*)))
CL-USER(4): (equalp *sorted* *numbers*)
CL-USER(5): (time (progn (setf *numbers* (sort *numbers* #'<)) nil))
2.157 seconds real time
3 cons cells
CL-USER(6): (equalp *sorted* *numbers*)
CL-USER(7): (let ((a (map-into (make-array 10000)
(let ((x 0))
(lambda () (incf x))))))
(sort a #'<)
CL-USER(8): (time (progn (setf *numbers* (sort *numbers* #'<)) nil))
1.074 seconds real time
3 cons cells
With these changes, sort seems to be a little faster (for vectors) although I was not worried with optimizations. In ABCL 1.0.1, in my machine, sorting 1000000 random integers takes around 10s on average while now it takes 2s. However, I must point out I didn't do any serious and proper benchmarking, just some runs.
I will be happy to answer any questions if necessary.
-------------- next part --------------
A non-text attachment was scrubbed...
Size: 3004 bytes
Desc: not available
More information about the armedbear-devel