Optimize the bottom-up run using a Camlp4 macro instead of an
[SXSI/xpathcomp.git] / src / runtime.ml
index b4de9cf..bc771eb 100644 (file)
@@ -1,5 +1,5 @@
 INCLUDE "debug.ml"
-INCLUDE "trace.ml"
+INCLUDE "log.ml"
 INCLUDE "utils.ml"
 
 open Format
@@ -43,8 +43,7 @@ module Make (U : ResJIT.S) : S with type result_set = U.NS.t =
 
 
     let eval_trans auto s1 s2 trans =
-      TRACE("top-down-run", 2, __ "Evaluating transition list:\n%!");
-      TRACE("top-down-run", 2, __ "%a\n%!" Translist.print trans);
+      LOG(__ "top-down-run" 3 "Evaluating transition list:@\n%a" Translist.print trans);
       Translist.fold
        (fun t ((a_st, a_op, a_todo) as acc)->
           let q, _, m, f = Transition.node t in
@@ -75,7 +74,7 @@ module Make (U : ResJIT.S) : S with type result_set = U.NS.t =
        let show_stats a =
          let count = ref 0 in
          Cache.Lvl3.iteri (fun _ _ _ _ b -> if not b then incr count) a;
-         eprintf "%!L3JIT: %i used entries\n%!" !count
+         Logger.print err_formatter "@?L3JIT: %i used entries@\n@?" !count
        let create () =
          let v = Cache.Lvl3.create 1024 dummy in
          if !Options.verbose then at_exit (fun () -> show_stats v);
@@ -104,7 +103,7 @@ module Make (U : ResJIT.S) : S with type result_set = U.NS.t =
          in
          let ns1 = StateSet.inter s1 orig_s1
          and ns2 = StateSet.inter s2 orig_s2 in
-         let res, ops, todo = eval_trans auto orig_s1 orig_s2 trl in
+         let res, ops, todo = eval_trans auto ns1 ns2 trl in
          let code, not_marking = ResJIT.compile ops in
          let todo_code, todo_notmarking =
            List.fold_left (fun (l, b) (p, q, o) -> let c, b' = ResJIT.compile o in
@@ -130,7 +129,6 @@ module Make (U : ResJIT.S) : S with type result_set = U.NS.t =
                      else sl1
                    else sl2
                  in
-                 eprintf "Here 1\n%!";
                  U.exec sl sl1 sl2 node code;
                  res, sl
              end
@@ -142,7 +140,6 @@ module Make (U : ResJIT.S) : S with type result_set = U.NS.t =
                    else sl1
                  else sl2
                in
-                 eprintf "Here 2\n%!";
                U.exec sl sl1 sl2 node code;
                res, sl
              end
@@ -170,19 +167,19 @@ module Make (U : ResJIT.S) : S with type result_set = U.NS.t =
 
        let cache_apply cache auto tlist s1 s2 =
          let f = gen_code auto tlist s1 s2 in
-         TRACE("grammar", 2, __ "Inserting: %i, %a, %a\n%!"
+         LOG(__ "grammar" 2 "Inserting: %i, %a, %a\n%!"
            (Uid.to_int tlist.Translist.Node.id) StateSet.print s1 StateSet.print s2);
          add cache tlist s1 s2 f; f
       end
 
 DEFINE LOOP (t, states, ctx) = (
   let _t = t in
-  TRACE("top-down-run", 3,
-       __ "Entering node %i with loop (tag %s, context %i) with states %a\n%!"
-         (Node.to_int _t)
-         (Tag.to_string (Tree.tag tree _t))
-         (Node.to_int (ctx))
-         (StateSet.print) (states));
+  LOG(__ "top-down-run" 3
+      "Entering node %i with loop (tag %s, context %i) with states %a"
+       (Node.to_int _t)
+       (Tag.to_string (Tree.tag tree _t))
+       (Node.to_int (ctx))
+       (StateSet.print) (states));
   if _t == Tree.nil then nil_res
   else
     let tag = Tree.tag tree _t in
@@ -192,8 +189,8 @@ DEFINE LOOP (t, states, ctx) = (
 
 DEFINE LOOP_TAG (t, states, tag, ctx) = (
   let _t = (t) in (* to avoid duplicating expression t *)
-  TRACE("top-down-run", 3,
-       __ "Entering node %i with loop_tag (tag %s, context %i) with states %a\n%!"
+  LOG(__ "top-down-run" 3
+       "Entering node %i with loop_tag (tag %s, context %i) with states %a"
          (Node.to_int _t)
          (Tag.to_string (tag))
          (Node.to_int (ctx))
@@ -203,6 +200,7 @@ DEFINE LOOP_TAG (t, states, tag, ctx) = (
     l2jit_dispatch
       _t (tag) (states) (ctx) (L2JIT.find cache2 (tag) (states)))
 
+
     let top_down_run auto tree root states ctx =
       let res_len = StateSet.max_elt auto.states + 1 in
       let empty_slot = Array.create res_len U.NS.empty in
@@ -228,7 +226,9 @@ DEFINE LOOP_TAG (t, states, tag, ctx) = (
        match opcode with
          | L2JIT.RETURN -> nil_res
          | L2JIT.CACHE ->
-           eprintf "New configuration\n%!";
+           LOG(__ "top-down-run" 3
+               "Top-down cache miss for configuration %s %a"
+                 (Tag.to_string tag) StateSet.print states);
            let opcode = L2JIT.compile cache2 auto tree tag states in
            l2jit_dispatch t tag states ctx opcode
 
@@ -254,7 +254,8 @@ DEFINE LOOP_TAG (t, states, tag, ctx) = (
                l3jit_dispatch tr_list res1 res2 t slot1 slot2
 
     and l2jit_dispatch_instr t ctx instr =
-      match instr with
+       match instr with
+       | L2JIT.NOP () -> nil_res
        | L2JIT.FIRST_CHILD s -> LOOP ((Tree.first_child tree t), s, ctx)
        | L2JIT.NEXT_SIBLING s -> LOOP ((Tree.next_sibling tree t), s, ctx)
 
@@ -308,19 +309,6 @@ DEFINE LOOP_TAG (t, states, tag, ctx) = (
 
     (*** Bottom-up evaluation function **)
 
-    let ns_print fmt t =
-      Format.fprintf fmt "{ ";
-      U.NS.iter begin fun node ->
-       Format.fprintf fmt "%a " Node.print node;
-      end t;
-      Format.fprintf fmt "}"
-
-    let slot_print fmt t =
-      Array.iteri begin fun state ns ->
-       Format.eprintf "%a -> %a\n" State.print state ns_print ns;
-      end t
-
-
     let eval_trans auto tree parent res1 res2 = assert false
 
     let rec uniq = function
@@ -328,6 +316,15 @@ DEFINE LOOP_TAG (t, states, tag, ctx) = (
       | e1 :: ((e2 :: ll) as l) -> if e1 == e2 then uniq l
        else e1 :: e2 :: (uniq ll);;
 
+DEFINE BOTTOM_UP_NEXT(node, rest, stop) =
+      (let ___fs = Tree.first_child tree node in
+       let ___res1 =
+        if ___fs == Tree.nil then nil_res
+        else full_top_down_run auto states tree ___fs
+       in
+       move_up node ___res1 true rest stop)
+
+
     let bottom_up_run auto tree (query, pat) =
       let array = time ~msg:"Timing text query" (Tree.full_text_query query tree) pat in
       let leaves = Array.to_list array in
@@ -335,43 +332,26 @@ DEFINE LOOP_TAG (t, states, tag, ctx) = (
       let res_len = (StateSet.max_elt states) + 1 in
       let empty_slot = Array.create res_len U.NS.empty in
       let nil_res = auto.bottom_states, empty_slot in
-      let cache = Cache.Lvl3.create 1024 L3JIT.dummy in
-      let rec loop_leaves l acc =
-       match l with
-           [] -> acc
-         | node :: ll ->
-           let res, lll = bottom_up_next node ll Tree.nil in
-           if (lll <> []) then
-             begin
-               eprintf "Leftover nodes: %i\n" (List.length lll);
-             end;
-           res
-
-      and bottom_up_next node rest stop =
-       let fs = Tree.first_child tree node in
-       let res1 =
-         if fs == Tree.nil then nil_res
-         else full_top_down_run auto states tree fs
-       in
-       move_up node res1 true rest stop
-
-      and move_up node res is_left rest stop =
+      let cache = Cache.Lvl3.create 0 L3JIT.dummy in
+      let rec move_up node res is_left rest stop =
        if node == stop then res, rest
        else
-         let prev_sibling = Tree.prev_sibling tree node in
-         let is_left' = prev_sibling == Tree.nil in
-         let real_parent = Tree.parent tree node in
-         let parent =
-           if is_left' then real_parent else max (Tree.first_child tree real_parent) stop
+         (*let prev_sibling = Tree.prev_sibling tree node in *)
+         let is_left' = Tree.is_first_child tree node (*prev_sibling == Tree.nil*) in
+         (*TODO: unsound in case of following-sibling moves
+           should replace the else by previous_sibling and walk up the sequence of
+           right child moves *)
+         let parent = if is_left' then Tree.parent tree node else
+             let p = Tree.first_child tree (Tree.parent tree node) in
+             if p < stop then stop else p
          in
-         (* let parent = if is_left' then Tree.parent tree node else prev_sibling in *)
          let (s1, sl1), (s2, sl2), rest' =
            if is_left then match rest with
                [] -> res, nil_res, rest
              | next :: rest' ->
                if Tree.is_right_descendant tree node next
                then
-                 let res2, rest' = bottom_up_next next rest' node in
+                 let res2, rest' = (*bottom_up_next*) BOTTOM_UP_NEXT(next, rest', node) in
                  res, res2, rest'
                else res, nil_res, rest
            else
@@ -381,13 +361,13 @@ DEFINE LOOP_TAG (t, states, tag, ctx) = (
          let id1 = Uid.to_int s1.StateSet.Node.id in
          let id2 = Uid.to_int s2.StateSet.Node.id in
          let code =
-           let code = Cache.Lvl3.find cache tag id1 id2 in
-           if code == L3JIT.dummy then
+           let code = Cache.Lvl3.find cache id2 id1 tag in
+           if code == L3JIT.dummy  then
              let trl =
                StateSet.fold
                  (fun q acc ->
                    List.fold_left (fun acc' (labels, tr) ->
-                     if labels == TagSet.any || TagSet.mem tag labels
+                     if TagSet.mem tag labels
                      then Translist.cons tr acc' else acc')
                      acc
                      (Hashtbl.find auto.trans q)
@@ -396,14 +376,24 @@ DEFINE LOOP_TAG (t, states, tag, ctx) = (
                  Translist.nil
              in
              let code = L3JIT.gen_code auto trl s1 s2 in
-             Cache.Lvl3.add cache tag id1 id2 code; code
+             Cache.Lvl3.add cache id2 id1 tag code; code
            else code
          in
          let res' = code empty_slot sl1 sl2 tree node in
          move_up parent res' is_left' rest' stop
       in
-      let _, slot = loop_leaves leaves (nil_res) in
-      slot.(StateSet.min_elt auto.topdown_marking_states)
+      let loop_leaves l =
+       match l with
+         [] -> nil_res
+       | node :: ll ->
+         let res, lll = BOTTOM_UP_NEXT( (*bottom_up_next*) node, ll, Tree.nil) in
+         if lll <> [] then
+           Logger.print err_formatter "WARNING: Leftover nodes: %i\n" (List.length lll);
+         res
+      in
+    let _, slot = loop_leaves leaves in
+    slot.(StateSet.min_elt auto.topdown_marking_states)
+
 
 let get_trans g auto tag states =
   StateSet.fold (fun q tr_acc ->
@@ -497,14 +487,11 @@ let dispatch_param1 conf id2 y0 y1 =
       let res_len = (StateSet.max_elt auto.states) + 1 in
       let empty_slot = Array.create res_len U.NS.empty in
       let nil_res = mk_nil auto.bottom_states empty_slot in
-      let empty_res = mk_empty (StateSet.empty, empty_slot) in
       let cache3 = L3JIT.create () in
       let dummy2 = (StateSet.empty, StateSet.empty, Translist.nil) in
       let cache2 = Cache.Lvl2.create 512 dummy2 in
       let rule_counter = ref 0 in
       let preorder_counter = ref 0 in
-      let dcache = DCache.create 1023 in
-      let ucache = UCache.create 1023 in
       let term_array = [| StateSet.empty; StateSet.empty |] in
       let get_trans tag states =
        let c = Cache.Lvl2.find cache2 tag (Uid.to_int states.StateSet.Node.id) in
@@ -518,7 +505,7 @@ let dispatch_param1 conf id2 y0 y1 =
       in
       let lambda = ref 0 in
       let rec start_loop idx states =
-       TRACE("grammar", 2, __ "Node %i\n%!" (Node.to_int idx));
+       LOG(__ "grammar" 2 "Node %i\n%!" (Node.to_int idx));
        if states == dummy_set then nil_res else
        if idx < Node.null then nil_res
        else begin
@@ -724,7 +711,7 @@ let dispatch_param1 conf id2 y0 y1 =
 
 
     let set a i v =
-      TRACE("twopass", 2, __ "Setting node %i to state %a\n%!"
+      LOG(__ "twopass" 2 "Setting node %i to state %a\n%!"
        i StateSet.print v);
       a.(i) <- v
 
@@ -741,7 +728,7 @@ let dispatch_param1 conf id2 y0 y1 =
          auto.bottom_states
        else
          let tag = Tree.tag tree t in
-         TRACE("twopass", 2, __ "Traversing node %i (tag %s) in states %a\n%!" (Node.to_int t) (Tag.to_string tag)
+         LOG(__ "twopass" 2 "Traversing node %i (tag %s) in states %a\n%!" (Node.to_int t) (Tag.to_string tag)
          StateSet.print states
          );
          let trans, lstates, rstates =
@@ -752,8 +739,8 @@ let dispatch_param1 conf id2 y0 y1 =
              c
            else c
          in
-         TRACE("twopass", 2, __ "\nTransitions are:\n%!");
-         TRACE("twopass", 2, __ "\nTransitions are:\n%a\n%!" 
+         LOG(__ "twopass" 2 "\nTransitions are:\n%!");
+         LOG(__ "twopass" 2"\nTransitions are:\n%a\n%!" 
            Translist.print trans
          );
          let s1 = loop (Tree.first_child tree t) lstates ctx
@@ -815,12 +802,12 @@ let dispatch_param1 conf id2 y0 y1 =
                 (Uid.to_int trans.Translist.Node.id) c;c
            else c
          in
-         TRACE("twopass", 2, __ "Evaluating node %i (tag %s).\n%!States=%a\n%!"
+         LOG(__ "twopass" 2 "Evaluating node %i (tag %s).\n%!States=%a\n%!"
            (Node.to_int t)
            (Tag.to_string tag)
            StateSet.print states
          );
-         TRACE("twopass", 2, __ "Translist=%a\nLeft=%a\nRight=%a\nMark=%s\n\n%!"
+         LOG(__ "twopass" 2 "Translist=%a\nLeft=%a\nRight=%a\nMark=%s\n\n%!"
            Translist.print trans
            StateSet.print s1
            StateSet.print s2
@@ -837,7 +824,7 @@ let dispatch_param1 conf id2 y0 y1 =
 
     let twopass_top_down_run auto tree root =
       let len = Node.to_int (Tree.closing tree root) + 1 in
-      TRACE("twopass", 2, __ "Creating array of size: %i\n%!" len);
+      LOG(__ "twopass" 2 "Creating array of size: %i\n%!" len);
       let states_array = Array.make len StateSet.empty in
       let _, cache =
        twopass_top_down states_array auto tree root auto.init Tree.nil