-
- let time _ref f arg =
- let t1 = Unix.gettimeofday () in
- let r = f arg in
- let t2 = Unix.gettimeofday () in
- let t = (1000. *.(t2 -. t1)) in
-
- let (time,count) = try
- Hashtbl.find _timings _ref
- with
- | Not_found -> 0.,0
- in
- let time = time+. t
- and count = count + 1
- in
- Hashtbl.replace _timings _ref (time,count);r
-
- include XML.Binary
-
-
- let first_child_ doc node =
- time ("XMLTree.FirstChild()") (XML.Tree.first_child doc) node
- let next_sibling_ doc node =
- time ("XMLTree.NextSibling()") (XML.Tree.next_sibling doc) node
-
- let is_empty_ text node =
- time ("TextCollection.IsEmpty()") (XML.Text.is_empty text) node
-
- let prev_text_ doc node =
- time ("XMLTree.PrevText()") (XML.Tree.prev_text doc) node
-
- let my_text_ doc node =
- time ("XMLTree.MyText()") (XML.Tree.my_text doc) node
-
- let next_text_ doc node =
- time ("XMLTree.NextText()") (XML.Tree.next_text doc) node
-
- let is_leaf_ doc node =
- time ("XMLTree.IsLeaf()") (XML.Tree.is_leaf doc ) node
-
- let node_xml_id_ doc node =
- time ("XMLTree.NodeXMLId()") (XML.Tree.node_xml_id doc ) node
-
- let text_xml_id_ doc node =
- time ("XMLTree.TextXMLId()") (XML.Tree.text_xml_id doc ) node
-
-
- let first_child n =
- let node' =
- match n.node with
- | Node (NC t) when is_leaf_ n.doc t ->
- let txt = my_text_ n.doc t in
- if is_empty_ n.doc txt
- then Nil
- else Node(SC (txt,XML.Tree.nil))
- | Node (NC t) ->
- let fs = first_child_ n.doc t in
- let txt = prev_text_ n.doc fs in
- if is_empty_ n.doc txt
- then norm fs
- else Node (SC (txt, fs))
- | Node(SC (i,_)) -> String i
- | Nil | String _ -> failwith "first_child"
- in
- { n with node = node'}
-
-
- let next_sibling n =
- let node' =
- match n.node with
- | Node (SC (_,ns)) -> norm ns
- | Node(NC t) ->
- let ns = next_sibling_ n.doc t in
- let txt =
- if XML.Tree.is_nil ns then
- next_text_ n.doc t
- else prev_text_ n.doc ns
- in
- if is_empty_ n.doc txt
- then norm ns
- else Node (SC (txt, ns))
- | Nil | String _ -> failwith "next_sibling"
- in
- { n with node = node'}
-
- let id =
- function { doc=d; node=Node(NC n)} -> node_xml_id_ d n
- | { doc=d; node=Node(SC (i,_) )} -> text_xml_id_ d i
- | _ -> failwith "id"
-
- (* Wrapper around critical function *)
- let string t = time ("TextCollection.GetText()") (string) t
- let left = first_child
- let right = next_sibling
- let tag t = time ("XMLTree.GetTag()") (tag) t
-
- let print_stats ppf =
- let total_time,total_calls =
- Hashtbl.fold (fun _ (t,c) (tacc,cacc) ->
- tacc+. t, cacc + c) _timings (0.,0)
-
- in
- Format.fprintf ppf
- "Timing : Function Name, number of calls,%% of total calls, mean time, total time, %% of total time\n%!";
- Hashtbl.iter (fun name (time,count) ->
- Format.fprintf ppf "%-27s% 8d\t% 4.2f%%\t% 4.6f ms\t% 4.6f ms\t%04.2f%%\n%!"
- name
- count
- (100. *. (float_of_int count)/.(float_of_int total_calls))
- (time /. (float_of_int count))
- time
- (100. *. time /. total_time)) _timings;
- Format.fprintf ppf "-------------------------------------------------------------------\n";
- Format.fprintf ppf "%-27s% 8d\t% 4.0f%%\t########## ms\t% 4.6f ms\t% 4.0f%%\n%!"
- "Total" total_calls 100. total_time 100.
-
-
- let print_xml_fast outc t =
- let rec loop ?(print_right=true) t = match t.node with
- | Nil -> ()
- | String (s) -> output_string outc (string t)
- | Node _ when Tag.equal (tag t) Tag.pcdata -> loop (left t); loop (right t)
-
- | Node (_) ->
- let tg = Tag.to_string (tag t) in
- let l = left t
- and r = right t
+ let print_xml_fast2 =
+ let _ = init_stack () in
+ let h = Hashtbl.create MED_H_SIZE in
+ let tag_str t = try Hashtbl.find h t with
+ Not_found -> let s = Tag.to_string t in
+ Hashtbl.add h t s;s
+ in
+ let h_att = Hashtbl.create MED_H_SIZE in
+ let att_str t = try Hashtbl.find h_att t with
+ Not_found -> let s = Tag.to_string t in
+ let attname = String.sub s 3 ((String.length s) -3) in
+ Hashtbl.add h_att t attname;attname
+ in fun outc tree t ->
+ let tree = tree.doc in
+ let fin = tree_closing tree t in
+ let rec loop_tag t tag =
+ if t <= fin then
+ if tree_is_open tree t then
+ (* opening tag *)
+ if tag == Tag.pcdata then
+ begin
+ output_string outc (text_get_cached_text tree (tree_my_text_unsafe tree t));
+ loop (next2 t) (* skip closing $ *)
+ end
+ else
+ let tagstr = tag_str tag in
+ let _ = output_char outc '<';
+ output_string outc tagstr in
+ let t' = next t in
+ if tree_is_open tree t' then
+ let _ = push tagstr in
+ let tag' = tree_tag_id tree t' in
+ if tag' == Tag.attribute then let t'' = loop_attr (next t') 0 in
+ output_string outc ">"; loop t'' else (output_string outc ">";loop_tag t' tag')
+ else (* closing with no content *)
+ let _ = output_string outc "/>" in
+ loop (next t')
+ else
+ begin
+ (* closing tag *)
+ output_string outc "</";
+ output_string outc (pop());
+ output_char outc '>';
+ loop (next t);
+ end
+ and loop t = loop_tag t (tree_tag_id tree t)
+ and loop_attr t n =
+ if tree_is_open tree t then
+ let attname = att_str (tree_tag_id tree t) in
+ output_char outc ' ';
+ output_string outc attname;
+ output_string outc "=\"";
+ let t = next t in (* open $@ *)
+ output_string outc (text_get_cached_text tree (tree_my_text_unsafe tree t));
+ output_char outc '"';
+ loop_attr (next3 t) (n+1)
+ else
+ next t (* close @ *)
+ in loop t
+
+ let print_xml_fast =
+ let h = Hashtbl.create MED_H_SIZE in
+ let tag_str t = try Hashtbl.find h t with
+ Not_found -> let s = Tag.to_string t in
+ Hashtbl.add h t s;s
+ in
+ let h_att = Hashtbl.create MED_H_SIZE in
+ let att_str t = try Hashtbl.find h_att t with
+ Not_found -> let s = Tag.to_string t in
+ let attname = String.sub s 3 ((String.length s) -3) in
+ Hashtbl.add h_att t attname;attname
+ in fun outc tree t ->
+ let rec loop ?(print_right=true) t =
+ if t != nil
+ then
+ let tagid = tree_tag_id tree.doc t in
+ if tagid==Tag.pcdata
+ then
+ begin
+ let tid = tree_my_text_unsafe tree.doc t in
+ output_string outc (text_get_cached_text tree.doc tid);
+ if print_right
+ then loop (next_sibling tree t);
+ end
+ else
+ let tagstr = tag_str tagid in
+ let l = first_child tree t
+ and r = next_sibling tree t