[Ocaml-pxp-users] Non-tail-recursive function find_all_elements causes a crash
Richard Jones
rich at annexia.org
Sat Aug 20 10:32:52 PDT 2005
Just a note here.
I'm using PXP 1.1.95, and I've been tracking a nasty little bug in my
code down much of the afternoon. It turned out in the end to be due
to a stack overflow in Pxp_document.find_all_elements (actually in
Pxp_document.find_all:search_flat). My data set contains O(1 million)
records which mostly match the predicate, so quickly causing the
process to die.
My next question will be why, instead of throwing a Stack_overflow
exception, the process goes into an infinite loop throwing SIGSEGV,
but I suspect that's a more fundamental compiler bug and nothing to do
with PXP.
Rich.
let find_all ?(deeply=false) f base =
let rec search_flat children =
match children with
[] -> []
| n :: children' ->
if f n then n :: search_flat children' else search_flat children'
in
let rec search_deep children =
match children with
[] -> []
| n :: children' ->
let rest =
search_deep (n # sub_nodes) @ search_deep children' in
if f n then
n :: rest
else
rest
in
(if deeply then search_deep else search_flat)
(base # sub_nodes)
;;
let find_all_elements ?deeply eltype base =
find_all
?deeply:deeply
(fun n ->
match n # node_type with
T_element name -> name = eltype
| _ -> false)
base
;;
--
Richard Jones, CTO Merjis Ltd.
Merjis - web marketing and technology - http://merjis.com
Team Notepad - intranets and extranets for business - http://team-notepad.com
More information about the Ocaml-pxp-users
mailing list