Added benchmarking funtions,
[SXSI/xpathcomp.git] / main.ml
diff --git a/main.ml b/main.ml
index 7cc631b..342bb2f 100644 (file)
--- a/main.ml
+++ b/main.ml
 (*  Copyright NICTA 2008                                                      *)
 (*  Distributed under the terms of the LGPL (see LICENCE)                     *)
 (******************************************************************************)
-INCLUDE "debug.ml"
 
 open Ata
-
-
-let l = ref [] ;;
-let time f x =
-  let t1 = Unix.gettimeofday () in
-  let r = f x in
-  let t2 = Unix.gettimeofday () in 
-  let t = (1000. *.(t2 -. t1)) in
-    l:= t::!l;
-    Printf.eprintf "  %fms\n%!" t ;
-    r
-;;
-let total_time () =  List.fold_left (+.) 0. !l;;
-
-let poa = TagSet.add Tag.pcdata (TagSet.singleton Tag.attribute)
-
-let rec fill_hashtag t =
-  if Tree.Binary.is_node t then 
-    begin
-      let tag = Tree.Binary.tag t in
-      let a = 
-       if TagSet.mem tag poa
-       then 0
-       else
-         fill_hashtag (Tree.Binary.first_child t) 
-      in
-      let b = fill_hashtag (Tree.Binary.next_sibling t)
-      in a+b+1
-    end
-  else 0
-  
-
-let test_slashslash tree k =
-  let test =
-    match k with "*" -> TagSet.remove (Tag.tag "") TagSet.star
-      | s -> TagSet.singleton (Tag.tag k)
+INCLUDE "utils.ml"
+let () = init_timer();;
+
+let enabled_gc = Gc.get()
+let disabled_gc = { Gc.get() with
+                     Gc.max_overhead = 1000000; 
+                     Gc.space_overhead = 100 }
+let hash x = 131*x/(x-1+1)
+
+let test_loop tree tag =
+  let t' = Tree.tagged_descendant tree tag  Tree.root in
+  let f = Hashtbl.create 4096
   in
-  let attorstring = TagSet.cup TagSet.pcdata TagSet.attribute in
-  let rec aux t acc =
-    if Tree.Binary.is_node t
-    then      
-      let tag = Tree.Binary.tag t in
-         let l = Tree.Binary.first_child t 
-         and r = Tree.Binary.next_sibling t
-         in
-         let acc = 
-           if TagSet.mem tag test
-           then
-             TS.append t acc
-           else
-             acc
-         in
-         let rl = if TagSet.mem tag attorstring then acc else aux l acc 
-         in aux r rl
-    else
-      acc
+  let jump t _ =  Tree.tagged_following_below tree tag t Tree.root in
+  let g t ctx = 
+    if t == Tree.nil then 0
+    else 1+ ((Hashtbl.find f (hash 101)) (jump t ctx) ctx)
   in
-  let _ = Printf.eprintf "Testing optimal //%s ... " k in
-  let r = time (aux tree ) TS.empty in
-  Printf.eprintf "Result set is %i nodes\n%!" (TS.length r)
-
-
-let test_jump tree k =
-  let ttag = Tag.tag k in
-
-  let rec loop acc tree = 
-    if Tree.Binary.is_node tree 
-    then
-      let acc = TS.cons tree acc in
-       loop acc (Tree.Binary.tagged_foll tree ttag)
-    else
-      acc
-    
+  Hashtbl.add f (hash 101) g;
+  (Hashtbl.find f (hash 101)) t' Tree.root
+
+let test_full tree = 
+  let root = Tree.root in
+  let fin = Tree.closing tree root in
+  let rec loop t = if t <= fin then
+  let tag = Tree.tag tree t in
+(*  let _ = Tag.to_string tag in *)
+  if tag == Tag.pcdata then (ignore (Tree.get_text tree t)); 
+  let t = (Obj.magic ((Obj.magic t) + 1)) in
+  loop t
   in
-  let _ = Printf.eprintf "Testing jumping for tag %s ... " k in
-  let r = time (loop TS.empty ) (Tree.Binary.tagged_next tree ttag) in
-    Printf.eprintf "Result set is %i nodes\n%!" (TS.length r)
+  loop root
 
 
-
-let test_traversal tree k =
-  let ttag = Tag.tag k in
-  let iid t = if Tree.Binary.is_node t then Tree.Binary.id t else -1 in
-  let rec aux t =
-    if Tree.Binary.is_node t
-    then      
-      let tag = Tree.Binary.tag t in
-      let l = Tree.Binary.first_child t 
-      and r = Tree.Binary.next_sibling t
-      in
-      let _ = Printf.eprintf "Tree with id %i and tag=%s, tagged_desc %s is %i tagged_foll is %i\n%!"
-       (Tree.Binary.id t) (Tag.to_string tag) (k) 
-       (iid (Tree.Binary.tagged_desc t ttag))
-       (iid (Tree.Binary.tagged_foll t ttag))
-      in
-       aux l;
-       aux r;
-
-    else 
-      ()
+let test_loop2 tree tag =
+  let t' = Tree.tagged_descendant tree tag  Tree.root in
+  let f = Hashtbl.create 4096
   in
-    aux tree
-
-
-  
-let test_count_subtree tree k =
-  let ttag = Tag.tag k in
-  let _ = Printf.eprintf "Counting subtrees with tag %s ... %!" k in
-  let r = time(Tree.Binary.subtree_tags tree) ttag in
-    Printf.eprintf "%i nodes \n%!" r
-
-
-let test_contains tree s =
-  let _ = Printf.eprintf "Fetching DocIds containing %s ... %!" s in
-    time (fun s -> let r = Tree.Binary.contains tree s in 
-           Tree.Binary.DocIdSet.iter 
-             (fun t -> output_string stderr 
-                (Tree.Binary.get_string tree t);
-                output_char stderr '\n') r ) s 
-    
-
-let test_count_contains tree s =
-  let _ = Printf.eprintf "Counting DocIds containing %s ... %!" s in
-  let r = time (Tree.Binary.count_contains tree) s in
-    Printf.eprintf "%i documents ids\n%!" (r)
-
-let test_contains_old tree s =
-  let _ = Printf.eprintf "Fetching (old) DocIds containing %s ... %!" s in
-  let r = time (Tree.Binary.contains_old tree) s in
-    Printf.eprintf "%i documents ids\n%!" (Tree.Binary.DocIdSet.cardinal r)
-
-let test_contains_iter tree s =
-  let _ = Printf.eprintf "Fetching (old) DocIds containing %s ... %!" s in
-  let r = time (Tree.Binary.contains_iter tree) s in
-    Printf.eprintf "%i documents ids\n%!" (Tree.Binary.DocIdSet.cardinal r)
-
-module Stack =
-struct
-  type t = { mutable table: Tree.Binary.t array;
-            mutable top : int }
-
-  let empty = { table = Array.make 0 (Obj.magic 0);
-               top = 0 }
-  let cons e s =
-    let ls = Array.length s.table in
-    if ls > s.top
-    then
-      begin 
-       s.table.(s.top) <- e;
-       s.top <- s.top + 1;
-       s
-      end
-    else
-
-      let a = Array.make (ls * 2 + 1) (Tree.Binary.root e)
-      in
-       Array.blit s.table 0 a 0 ls;
-       s.table <- a;
-       s.table.(s.top) <- e;
-       s.top <- s.top + 1;
-       s
-         
-end
-
-let test_fast tree = 
-  let rec aux t acc = 
-    if Tree.Binary.is_node t 
-    then 
-      aux (Tree.Binary.right t)( aux (Tree.Binary.left t) (Stack.cons t acc))    
-    else acc
-  in let _ = Printf.eprintf "Fast traversal ...%!" in
-    time (aux tree) Stack.empty
-
-let test_cps tree =
-  let rec aux t acc cont =
-    if Tree.Binary.is_node t
-    then aux (Tree.Binary.left t) (Stack.cons t acc) ((Tree.Binary.right t)::cont)
-    else match cont with
-      | [] -> acc
-      | p::r -> aux p acc r
+  let jump t _ =  Tree.tagged_following_below tree tag t Tree.root in
+  let rec g t ctx = 
+    if t == Tree.nil then 0
+    else 1+ (match (Hashtbl.find f (hash 101)) with
+               `Foo ->g (jump t ctx) ctx
+           ) 
   in
-  let _ = Printf.eprintf "CPS traversal ...%!" in
-    time (aux tree Stack.empty) []
-
+  Hashtbl.add f (hash 101) `Foo;
+  g t' Tree.root
 
-let main v query output =
-    let _ = Tag.init (Tree.Binary.tag_pool v) in
+let main v query_string output =
+    let _ = Tag.init (Tree.tag_pool v) in
       Printf.eprintf "Parsing query : ";    
       let query = try
        time
-         XPath.Parser.parse_string query
+         XPath.Parser.parse_string query_string
       with
          Ulexer.Loc.Exc_located ((x,y),e) -> Printf.eprintf "character %i-%i %s\n" x y (Printexc.to_string e);exit 1
       in
-       XPath.Ast.print Format.err_formatter query;
-       Format.fprintf Format.err_formatter "\n%!";
-(*     Printf.eprintf "Dummy iteration : ";
-       time (fill_hashtag) v;
-       Printf.eprintf "Dummy iteration (tag access cached) : ";
-       time (fill_hashtag) v;
-*)
-       Printf.eprintf "Compiling query : ";
-       let auto,_ = time XPath.Compile.compile  query in
-         Printf.eprintf "Execution time %s : " (if !Options.count_only then "(counting only)" else "");
-         begin
-           if !Options.count_only then
-             let result = time (BottomUpNew.run_count auto) v
+      let _ = Printf.eprintf "Number of nodes %i\n%!" (Tree.size v) in
+      let _ = Printf.eprintf "Timing first_child/next_sibling %!" in
+      let _ = time (Tree.benchmark_fsns)  v in
+      let _ = Printf.eprintf "Timing jump to a %!" in
+      let _ = time (Tree.benchmark_jump v) (Tag.tag "a")  in
+      
+(*      let _ = Printf.eprintf "Timing //keyword :" in
+      let r = time (test_loop v) (Tag.tag "keyword") in
+      let _ = Printf.eprintf "Count is %i\n%!" r in 
+      let _ = Printf.eprintf "Timing //keyword 2:" in
+      let r = time (test_loop2 v) (Tag.tag "keyword") in
+      let _ = Printf.eprintf "Count is %i\n%!" r in  
+      let _ = Printf.eprintf "Timing //node() :" in
+      let _ = time (test_full)  v in      *)
+      XPath.Ast.print Format.err_formatter query;
+      Format.fprintf Format.err_formatter "\n%!";
+      Printf.eprintf "Compiling query : ";
+      let auto,ltags,contains = time (XPath.Compile.compile ~querystring:query_string) query in 
+      let _ = Ata.dump Format.err_formatter auto in
+      let _ = Printf.eprintf "%!" in
+      let jump_to = 
+       match contains with
+          None -> (max_int,`NOTHING)
+         | Some ((op,s)) -> 
+             let r = Tree.count v s 
              in
-               Printf.eprintf "Number of nodes in the result set : %i\n" result
-           else
-           let result = time (BottomUpNew.run auto) v in         
-             Printf.eprintf "Number of nodes in the result set : %i\n" (TS.length result);
-             begin
-               match output with
-                 | None -> ()
-                 | Some f ->                 
-                     Printf.eprintf "Serializing results : ";
-                     time( fun () ->
-                             let oc = open_out f in
-                               output_string oc "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";                                
-                               TS.iter (fun t -> output_string oc "----------\n";
-                                          Tree.Binary.print_xml_fast oc t;
-                                          output_char oc '\n') result) ();
-             end;
+             Printf.eprintf "%i documents in the TextCollection\n" (Tree.text_size v);
+             Printf.eprintf "Global count is %i, using " r;
+             if r < !Options.tc_threshold then begin             
+               Printf.eprintf "TextCollection contains\nCalling global contains : ";
+               time (Tree.init_textfun op v) s;
+             end
+             else begin
+               Printf.eprintf "Naive contains\nCalling global contains : ";
+               time (Tree.init_naive_contains v) s
+             end;(r,`CONTAINS(s))
+      in
+      let test_list = jump_to in
+      (*
+       let test_list = 
+       if (!Options.backward) then begin
+       Printf.eprintf "Finding min occurences : ";
+       time 
+       ( List.fold_left (fun ((min_occ,kind)as acc)  (tag,_) ->
+                             let numtags = Tree.subtree_tags v tag Tree.root in
+                               if  ((numtags < min_occ) && numtags >= 2)
+                               then (numtags,`TAG(tag))
+                               else acc) jump_to) ltags
+         end
+         else (max_int,`NOTHING)
+       in*)
+       let _ = if (snd test_list) != `NOTHING then
+         let occ,s1,s2 = match test_list with
+           | (x,`TAG (tag)) -> (x, "tag", (Tag.to_string tag))
+           | (x,`CONTAINS(s)) -> (x, "contains()", s)
+           | _ -> assert false
+         in
+           Printf.eprintf "Will jump to %s %s occuring %i time\n%!" s1 s2 occ
+       in
+         Printf.eprintf "Execution time %s : "
+           (if !Options.count_only then "(counting only)" else if !Options.backward then "(bottomup)" else "");
+         begin
+           let _ = Gc.full_major();Gc.compact() in
+           let _ = Printf.eprintf "%!" in
+(*         let _ = Gc.set (disabled_gc) in *)
+             if !Options.backward && ((snd test_list) != `NOTHING )then 
+               if !Options.count_only then
+               let r = time_mem (bottom_up_count auto v )(snd test_list)  in
+               let _ = Printf.eprintf "Number of nodes in the result set : %i\n%!" r 
+               in ()
+               else begin
+               let r = time_mem (bottom_up auto v )(snd test_list)  in
+               let _ = Printf.eprintf "Number of nodes in the result set : %i\n%!" (IdSet.length r)
+               in
+                 match output with
+
+                   | None -> ()
+                   | Some f ->               
+                       Printf.eprintf "Serializing results : ";
+                       time( fun () ->
+                               (*let oc = open_out f in *)
+                               let oc = Unix.openfile f [ Unix.O_WRONLY;Unix.O_TRUNC;Unix.O_CREAT] 0o644 in
+                               (*output_string oc "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";*)
+                               IdSet.iter (fun t -> 
+                                             Tree.print_xml_fast3 v t oc;
+                                             (*output_char oc '\n'; *)                         
+                                          ) r) ();
+               end
+                    
+             else
+               let _ = 
+                 if !Options.backward then Printf.eprintf "WARNING: couldn't find a jumping point, running top-down\n" 
+               in
+               if !Options.count_only then
+                 let r = time_mem ( top_down_count auto ) v in 
+                 let _ = Printf.eprintf "Number of nodes in the result set : %i\n%!" r
+                 in ()
+               else      
+               let module GR = Ata.Test(struct let doc = v end) in
+                 let result = time_mem (GR.top_down auto) v in
+                 let _ = Printf.eprintf "Counting results " in
+                 let rcount = time (GR.Results.length) result in
+                   Printf.eprintf "Number of nodes in the result set : %i\n" rcount;
+                   Printf.eprintf "\n%!";
+                   begin
+                     match output with
+                       | None -> ()
+                       | Some f ->                   
+                           Printf.eprintf "Serializing results : ";
+                           time( fun () ->
+                                   (*let oc = open_out f in *)
+                                   let oc = Unix.openfile f [ Unix.O_WRONLY;Unix.O_TRUNC;Unix.O_CREAT] 0o644 in
+                                     (*output_string oc "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";*)
+                                   let t1 = ref (Unix.gettimeofday()) in
+                                   let count = ref 1 in
+                                   let old_count = ref 1 in
+                                    GR.Results.iter (fun t -> 
+                                                       incr count;
+                                                       begin
+                                                         if (!count mod 15) == 0
+                                                         then
+                                                         let t2 =  Unix.gettimeofday() in
+                                                         let _ = Printf.eprintf "Printing %i elements in %f ms\n"
+                                                           (!count - !old_count) (1000. *.(t2 -. !t1))
+                                                         in
+                                                         ( old_count := !count; t1 := Unix.gettimeofday())
+                                                       end;
+                                                       Tree.print_xml_fast3 v t oc;
+                                                       (*output_char oc '\n'; *)                               
+                                                    ) result) ();
+                   end;
          end;
-         Printf.eprintf "Total running time : %fms\n%!" (total_time())
+         let _ = Gc.set enabled_gc in
+           Printf.eprintf "Total running time : %fms\n%!" (total_time())
 ;;
-               
+
 Options.parse_cmdline();;
 
-let v = 
+let v =
   if (Filename.check_suffix !Options.input_file ".srx")
   then 
     begin
       Printf.eprintf "Loading from file : ";
-      time (Tree.Binary.load  ~sample:!Options.sample_factor )
-       (Filename.chop_suffix !Options.input_file ".srx");
-    end
+      time (Tree.load  ~sample:!Options.sample_factor ~load_text:(not !Options.count_only))
+       !Options.input_file;
+       end
   else 
     let v = 
-      time (fun () -> let v = Tree.Binary.parse_xml_uri !Options.input_file;
+      time (fun () -> let v = Tree.parse_xml_uri !Options.input_file;
            in Printf.eprintf "Parsing document : %!";v
           ) () 
     in
       if !Options.save_file <> ""
       then begin
        Printf.eprintf "Writing file to disk : ";
-       time (Tree.Binary.save v) !Options.save_file;
+       time (Tree.save v) !Options.save_file;
       end;
       v
 in
   main v !Options.query !Options.output_file;;
 
-IFDEF DEBUG
-THEN
-Printf.eprintf "\n=================================================\nDEBUGGING\n%!";
-
-Tree.DEBUGTREE.print_stats Format.err_formatter;;
-Gc.full_major()
-ENDIF