Implement runtime optimisation via Hashing of transitions.
authorKim Nguyễn <kn@lri.fr>
Wed, 13 Mar 2013 17:41:06 +0000 (18:41 +0100)
committerKim Nguyễn <kn@lri.fr>
Wed, 13 Mar 2013 17:41:06 +0000 (18:41 +0100)
src/auto/ata.ml
src/auto/eval.ml
src/utils.mlpack
src/utils/cache.ml
src/utils/cache.mli
src/utils/hlist.ml [new file with mode: 0644]
src/utils/hlist.mli [new file with mode: 0644]
src/utils/hlist_sig.ml [new file with mode: 0644]
tests/xmark_small.xml.queries

index 4418996..9e5e57f 100644 (file)
@@ -14,7 +14,7 @@
 (***********************************************************************)
 
 (*
 (***********************************************************************)
 
 (*
-  Time-stamp: <Last modified on 2013-03-11 00:14:28 CET by Kim Nguyen>
+  Time-stamp: <Last modified on 2013-03-13 18:31:19 CET by Kim Nguyen>
 *)
 
 INCLUDE "utils.ml"
 *)
 
 INCLUDE "utils.ml"
@@ -153,14 +153,24 @@ let create () = { id = next ();
  }
 
 
  }
 
 
+module Transition = Hcons.Make (struct
+  type t = State.t * QNameSet.t * SFormula.t
+  let equal (a, b, c) (d, e, f) =
+    a == d && b == e && c == f
+  let hash (a, b, c) =
+    HASHINT4 (PRIME1, a, ((QNameSet.uid b) :> int), ((SFormula.uid c) :> int))
+end)
+
+module TransList : Hlist.S with type elt = Transition.t = Hlist.Make(Transition)
+
 let get_trans a states tag =
   StateSet.fold (fun q acc0 ->
     try
       let trs = Hashtbl.find a.transitions q in
       List.fold_left (fun acc1 (labs, phi) ->
 let get_trans a states tag =
   StateSet.fold (fun q acc0 ->
     try
       let trs = Hashtbl.find a.transitions q in
       List.fold_left (fun acc1 (labs, phi) ->
-        if QNameSet.mem tag labs then (q,phi)::acc1 else acc1) acc0 trs
+        if QNameSet.mem tag labs then TransList.cons (Transition.make (q, labs, phi)) acc1 else acc1) acc0 trs
     with Not_found -> acc0
     with Not_found -> acc0
-  ) states []
+  ) states TransList.nil
 
 (*
   [add_trans a q labels f] adds a transition [(q,labels) -> f] to the
 
 (*
   [add_trans a q labels f] adds a transition [(q,labels) -> f] to the
index bf9788f..5c346d0 100644 (file)
 (***********************************************************************)
 
 (*
 (***********************************************************************)
 
 (*
-  Time-stamp: <Last modified on 2013-03-13 14:56:29 CET by Kim Nguyen>
+  Time-stamp: <Last modified on 2013-03-13 18:27:13 CET by Kim Nguyen>
 *)
 
 INCLUDE "utils.ml"
 open Format
 open Utils
 
 *)
 
 INCLUDE "utils.ml"
 open Format
 open Utils
 
-module Make (T : Tree.Sig.S) = struct
+module Make (T : Tree.Sig.S) :
+  sig
+    val eval : Ata.t -> T.t -> T.node -> T.node list
+  end
+ = struct
 
   type cache = (int, StateSet.t) Hashtbl.t
 
   type cache = (int, StateSet.t) Hashtbl.t
-
   let get c t n =
     try Hashtbl.find c (T.preorder t n)
     with Not_found -> StateSet.empty
 
   let set c t n v = Hashtbl.replace c (T.preorder t n) v
   let get c t n =
     try Hashtbl.find c (T.preorder t n)
     with Not_found -> StateSet.empty
 
   let set c t n v = Hashtbl.replace c (T.preorder t n) v
-  let eval_form  phi tree node fcs nss ps ss =
+
+  module Info = struct
+    type t = { is_left : bool;
+               is_right : bool;
+               has_left : bool;
+               has_right : bool;
+               kind : Tree.Common.NodeKind.t;
+             }
+    let equal a b = a = b
+    let hash  a = Hashtbl.hash a
+  end
+
+  module NodeInfo = Hcons.Make(Info)
+
+  let eval_form phi node_info fcs nss ps ss =
+    let open NodeInfo in
+    let open Info in
     let rec loop phi =
     let rec loop phi =
-      match Ata.SFormula.expr phi with
+      begin match Ata.SFormula.expr phi with
         Formula.True -> true
       | Formula.False -> false
       | Formula.Atom a ->
           let p, b, q = Ata.Atom.node a in
           let pos =
             let open Ata in
         Formula.True -> true
       | Formula.False -> false
       | Formula.Atom a ->
           let p, b, q = Ata.Atom.node a in
           let pos =
             let open Ata in
-            match p with
-            | First_child -> StateSet.mem q fcs
-            | Next_sibling ->  StateSet.mem q nss
-            | Parent | Previous_sibling -> StateSet.mem q ps
-            | Stay -> StateSet.mem q ss
-            | Is_first_child ->
-                node == (T.first_child tree (T.parent tree node))
-            | Is_next_sibling ->
-                node == (T.next_sibling tree (T.parent tree node))
-            | Is k -> k == (T.kind tree node)
-            | Has_first_child ->
-                T.nil != T.first_child tree node
-            | Has_next_sibling ->
-                T.nil != T.next_sibling tree node
-          in
-          if Ata.is_move p && (not b) then
-            eprintf "Warning: Invalid negative atom %a" Ata.Atom.print a;
-          b == pos
+                match p with
+                | First_child -> StateSet.mem q fcs
+                | Next_sibling ->  StateSet.mem q nss
+                | Parent | Previous_sibling -> StateSet.mem q ps
+                | Stay -> StateSet.mem q ss
+                | Is_first_child -> node_info.node.is_left
+                | Is_next_sibling -> node_info.node.is_right
+                | Is k -> k == node_info.node.kind
+                | Has_first_child -> node_info.node.has_left
+                | Has_next_sibling -> node_info.node.has_right
+            in
+            if Ata.is_move p && (not b) then
+              eprintf "Warning: Invalid negative atom %a" Ata.Atom.print a;
+            b == pos
       | Formula.And(phi1, phi2) -> loop phi1 && loop phi2
       | Formula.Or (phi1, phi2) -> loop phi1 || loop phi2
       | Formula.And(phi1, phi2) -> loop phi1 && loop phi2
       | Formula.Or (phi1, phi2) -> loop phi1 || loop phi2
+      end
     in
     loop phi
 
     in
     loop phi
 
-  let eval_trans l tree node fcs nss ps ss acc =
-    List.fold_left (fun (acct, accs) ((q, phi) as trs) ->
-      if StateSet.mem q accs then (acct, accs) else
-      if eval_form phi tree node fcs nss ps ss then
-        (acct, StateSet.add q accs)
-      else
-        (trs::acct, accs)
-    ) ([], acc) l
+  let eval_trans cache ltrs node_info fcs nss ps ss =
+    let i = (ltrs.Ata.TransList.id :> int)
+    and j = (node_info.NodeInfo.id :> int)
+    and k = (fcs.StateSet.id :> int)
+    and l = (nss.StateSet.id :> int)
+    and m = (ps.StateSet.id :> int)
+    and n = (ss.StateSet.id :> int) in
+    let res = Cache.N6.find cache i j k l m n in
+    if res == Cache.N6.dummy cache then
+      let res =
+        Ata.TransList.fold (fun trs (acct, accs) ->
+          let q, _, phi = Ata.Transition.node trs in
+          if StateSet.mem q accs then (acct, accs) else
+            if eval_form phi node_info fcs nss ps accs then
+              (acct, StateSet.add q accs)
+            else
+              (Ata.TransList.cons trs acct, accs)
+        ) ltrs (Ata.TransList.nil, ss) 
+      in
+      Cache.N6.add cache i j k l m n res; res
+    else
+      res
 
   let top_down_run auto tree node cache _i =
     let redo = ref false in
 
   let top_down_run auto tree node cache _i =
     let redo = ref false in
+    let dummy2 = Ata.TransList.cons
+      (Ata.Transition.make (State.dummy,QNameSet.empty, Ata.SFormula.false_))
+      Ata.TransList.nil
+    in
+    let dummy6 = (dummy2, StateSet.empty) in
+    let trans_cache6 = Cache.N6.create 17 dummy6 in
+    let trans_cache2 = Cache.N2.create 17 dummy2 in
     let rec loop node =
       if node != T.nil then begin
         let parent = T.parent tree node in
         let fc = T.first_child tree node in
         let ns = T.next_sibling tree node in
     let rec loop node =
       if node != T.nil then begin
         let parent = T.parent tree node in
         let fc = T.first_child tree node in
         let ns = T.next_sibling tree node in
-        let states0 = get cache tree node in
         let tag = T.tag tree node in
         let tag = T.tag tree node in
-        let trans0 = Ata.get_trans auto auto.Ata.states tag in
+        let states0 = get cache tree node in
+        let trans0 =
+          let trs =
+            Cache.N2.find trans_cache2
+              (tag.QName.id :> int) (auto.Ata.states.StateSet.id :> int)
+          in
+          if trs == dummy2 then
+            let trs = Ata.get_trans auto auto.Ata.states tag in
+            (Cache.N2.add
+              trans_cache2
+              (tag.QName.id :> int)
+              (auto.Ata.states.StateSet.id :> int) trs; trs)
+          else trs
+        in
         let ps = get cache tree parent in
         let fcs = get cache tree fc in
         let nss = get cache tree ns in
         let ps = get cache tree parent in
         let fcs = get cache tree fc in
         let nss = get cache tree ns in
+        let node_info = NodeInfo.make
+          (Info.({ is_left = node == T.first_child tree parent;
+                   is_right = node == T.next_sibling tree parent;
+                   has_left = fc != T.nil;
+                   has_right = ns != T.nil;
+                   kind = T.kind tree node }))
+        in
         let trans1, states1 =
         let trans1, states1 =
-          eval_trans trans0 tree node fcs nss ps states0 states0
+          eval_trans trans_cache6 trans0 node_info fcs nss ps states0
         in
         if states1 != states0 then set cache tree node states1;
         let () = loop fc in
         let fcs1 = get cache tree fc in
         let trans2, states2 =
         in
         if states1 != states0 then set cache tree node states1;
         let () = loop fc in
         let fcs1 = get cache tree fc in
         let trans2, states2 =
-          eval_trans trans1 tree node fcs1 nss ps states1 states1
+          eval_trans trans_cache6 trans1 node_info fcs1 nss ps states1
         in
         if states2 != states1 then set cache tree node states2;
         let () = loop ns in
         let _, states3 =
         in
         if states2 != states1 then set cache tree node states2;
         let () = loop ns in
         let _, states3 =
-          eval_trans trans2 tree node fcs1 (get cache tree ns) ps states2 states2
+          eval_trans trans_cache6 trans2 node_info fcs1 (get cache tree ns) ps states2
         in
         if states3 != states2 then set cache tree node states3;
         if states0 != states3 && (not !redo) then redo := true
         in
         if states3 != states2 then set cache tree node states3;
         if states0 != states3 && (not !redo) then redo := true
index 711e03f..755f521 100644 (file)
@@ -4,6 +4,8 @@ utils/FiniteCofinite
 utils/FiniteCofinite_sig
 utils/Hcons
 utils/Hcons_sig
 utils/FiniteCofinite_sig
 utils/Hcons
 utils/Hcons_sig
+utils/Hlist
+utils/Hlist_sig
 utils/Misc
 utils/Pretty
 utils/Ptset
 utils/Misc
 utils/Pretty
 utils/Ptset
index a38fe0c..7ba67d5 100644 (file)
@@ -14,7 +14,7 @@
 (***********************************************************************)
 
 (*
 (***********************************************************************)
 
 (*
-  Time-stamp: <Last modified on 2013-03-13 16:02:03 CET by Kim Nguyen>
+  Time-stamp: <Last modified on 2013-03-13 18:17:13 CET by Kim Nguyen>
 *)
 
 let realloc l old_size new_size dummy =
 *)
 
 let realloc l old_size new_size dummy =
@@ -30,7 +30,7 @@ struct
                dummy : 'a;
                mutable offset : int;
              }
                dummy : 'a;
                mutable offset : int;
              }
-  let create n a = {
+  let create _ a = {
     line = Array.create 0 a;
     dummy = a;
     offset = ~-1;
     line = Array.create 0 a;
     dummy = a;
     offset = ~-1;
@@ -241,3 +241,30 @@ struct
       done
 
 end
       done
 
 end
+
+module N6 =
+struct
+  type 'a t = 'a N3.t N3.t
+
+  let create _n a =
+         let dummy1 = N3.create 512 a in
+         N3.create 512 dummy1
+
+  let add a i j k l m n v =
+    let line = N3.find a i j k in
+    if line == N3.dummy a then
+      let nline =  N3.create 0 (N3.dummy line) in
+      N3.add a i j k nline;
+      N3.add nline l m n v
+    else
+      N3.add line l m n v
+
+  let find a i j k l m n =
+    let v = N3.find a i j k in
+    if v == N3.dummy a then N3.dummy (N3.dummy a)
+    else N3.find v l m n
+
+
+  let dummy a = N3.dummy (N3.dummy a)
+  let iteri _f _a = assert false
+end
index dba9917..d6c0ebb 100644 (file)
@@ -14,7 +14,7 @@
 (***********************************************************************)
 
 (*
 (***********************************************************************)
 
 (*
-  Time-stamp: <Last modified on 2013-03-13 16:01:55 CET by Kim Nguyen>
+  Time-stamp: <Last modified on 2013-03-13 18:04:35 CET by Kim Nguyen>
 *)
 
 (** N-dimentional caches *)
 *)
 
 (** N-dimentional caches *)
@@ -69,3 +69,13 @@ module N5 :
     val dummy : 'a t -> 'a
     val iteri : (int -> int -> int -> int -> int -> 'a -> bool -> unit) -> 'a t -> unit
   end
     val dummy : 'a t -> 'a
     val iteri : (int -> int -> int -> int -> int -> 'a -> bool -> unit) -> 'a t -> unit
   end
+
+module N6 :
+  sig
+    type 'a t
+    val create : int -> 'a -> 'a t
+    val find : 'a t -> int -> int -> int -> int -> int -> int -> 'a
+    val add : 'a t -> int -> int -> int -> int -> int -> int -> 'a -> unit
+    val dummy : 'a t -> 'a
+    val iteri : (int -> int -> int -> int -> int -> int -> 'a -> bool -> unit) -> 'a t -> unit
+  end
diff --git a/src/utils/hlist.ml b/src/utils/hlist.ml
new file mode 100644 (file)
index 0000000..3ffb8fc
--- /dev/null
@@ -0,0 +1,82 @@
+INCLUDE "utils.ml"
+
+include Hlist_sig
+
+module type HConsBuilder =
+  functor (H : Common_sig.HashedType) -> Hcons.S with type data = H.t
+
+module Builder (HCB : HConsBuilder) (H : Hcons.Abstract) :
+  S with type elt = H.t =
+struct
+  type elt = H.t
+
+  module rec Node : Hcons.S with type data = Data.t = HCB(Data)
+                            and Data : Common_sig.HashedType with type t = (elt, Node.t) node =
+  struct
+    type t = (elt, Node.t) node
+    let equal x y =
+      match x,y with
+      | Nil, Nil -> true
+      | Cons(e1, l1), Cons(e2, l2) -> e1 == e2 && l1 == l2
+      | _ -> false
+
+    let hash = function
+    | Nil -> 0
+    | Cons(e, l) -> HASHINT3 (PRIME1, Uid.to_int (H.uid e), Uid.to_int (Node.uid l))
+  end
+
+  include Node
+
+  let nil = make Nil
+
+  let rec sorted_cons e l =
+    match l.Node.node with
+      |        Nil -> Node.make (Cons(e, l))
+      | Cons (x, ll) ->
+         if H.uid e < H.uid x
+         then Node.make (Cons(e, l))
+         else Node.make (Cons(x, sorted_cons e ll))
+
+  let cons e l =
+    Node.make(Cons(e, l))
+
+  let cons ?(sorted=true) e l =
+    if sorted then sorted_cons e l else cons e l
+
+  let hd = function { Node.node = Cons(e, _); _ } -> e | _ -> failwith "hd"
+  let tl = function { Node.node = Cons(_, l); _ } -> l | _ -> failwith "tl"
+
+  let fold f l acc =
+    let rec loop acc l = match l.Node.node with
+      | Nil -> acc
+      | Cons (a, aa) -> loop (f a acc) aa
+    in
+      loop acc l
+
+  let map f l  =
+    let rec loop l = match l.Node.node with
+      | Nil -> nil
+      | Cons(a, aa) -> cons (f a) (loop aa)
+    in
+      loop l
+
+  let iter f l =
+    let rec loop l = match l.Node.node with
+      | Nil -> ()
+      | Cons(a,aa) ->  (f a);(loop aa)
+    in
+      loop l
+
+  let rev l = fold cons l nil
+  let rev_map f l = fold (fun x acc -> cons (f x) acc) l nil
+  let length l = fold (fun _ c -> c+1) l 0
+  let rec mem e l =
+    match l.Node.node with
+      | Nil -> false
+      | Cons (x, ll) -> x == e || mem e ll
+
+end
+
+module Make = Builder(Hcons.Make)
+module Weak = Builder(Hcons.Weak)
+
diff --git a/src/utils/hlist.mli b/src/utils/hlist.mli
new file mode 100644 (file)
index 0000000..80204d2
--- /dev/null
@@ -0,0 +1,30 @@
+(***********************************************************************)
+(*                                                                     *)
+(*                               TAToo                                 *)
+(*                                                                     *)
+(*                     Kim Nguyen, LRI UMR8623                         *)
+(*                   Université Paris-Sud & CNRS                       *)
+(*                                                                     *)
+(*  Copyright 2010-2012 Université Paris-Sud and Centre National de la *)
+(*  Recherche Scientifique. All rights reserved.  This file is         *)
+(*  distributed under the terms of the GNU Lesser General Public       *)
+(*  License, with the special exception on linking described in file   *)
+(*  ../LICENSE.                                                        *)
+(*                                                                     *)
+(***********************************************************************)
+
+(*
+  Time-stamp: <Last modified on 2013-03-13 18:30:59 CET by Kim Nguyen>
+*)
+
+include module type of Hlist_sig
+
+module Make (H : Hcons.Abstract) : S with type elt = H.t
+(** Builds an implementation of hashconsed lists of hashconsed elements.
+    See {!Hcons.Make}.
+*)
+
+module Weak (H : Hcons.Abstract) : S with type elt = H.t
+(** Builds an implementation of hashconsed lists of hashconsed elements
+    with weak internal storage. See {!Hcons.Weak}.
+*)
diff --git a/src/utils/hlist_sig.ml b/src/utils/hlist_sig.ml
new file mode 100644 (file)
index 0000000..227768c
--- /dev/null
@@ -0,0 +1,35 @@
+(***********************************************************************)
+(*                                                                     *)
+(*                               TAToo                                 *)
+(*                                                                     *)
+(*                     Kim Nguyen, LRI UMR8623                         *)
+(*                   Université Paris-Sud & CNRS                       *)
+(*                                                                     *)
+(*  Copyright 2010-2012 Université Paris-Sud and Centre National de la *)
+(*  Recherche Scientifique. All rights reserved.  This file is         *)
+(*  distributed under the terms of the GNU Lesser General Public       *)
+(*  License, with the special exception on linking described in file   *)
+(*  ../LICENSE.                                                        *)
+(*                                                                     *)
+(***********************************************************************)
+
+(*
+  Time-stamp: <Last modified on 2013-03-13 18:21:28 CET by Kim Nguyen>
+*)
+type ('a,'b) node = Nil | Cons of ('a * 'b)
+
+module type S = sig
+  type elt
+  include Hcons.S
+  val nil : t
+  val cons : ?sorted:bool -> elt -> t -> t
+  val hd : t -> elt
+  val tl : t -> t
+  val fold : (elt -> 'a -> 'a) -> t -> 'a -> 'a
+  val map : (elt -> elt) -> t -> t
+  val iter : (elt -> 'a) -> t -> unit
+  val rev : t -> t
+  val rev_map : (elt -> elt) -> t -> t
+  val length : t -> int
+  val mem : elt -> t -> bool
+end
index a51fc3c..237bba7 100644 (file)
@@ -14,8 +14,8 @@ B5 /site/regions/*/item[following::item]/name
 B6 /site/regions/*/item[preceding::item]/name
 B7 //person[profile/@income]/name
 B8 /site/open_auctions/open_auction[bidder and not(bidder/preceding-sibling::bidder)]/interval
 B6 /site/regions/*/item[preceding::item]/name
 B7 //person[profile/@income]/name
 B8 /site/open_auctions/open_auction[bidder and not(bidder/preceding-sibling::bidder)]/interval
-#B9 /site/open_auctions/open_auction[(not(bidder/following::bidder) or not(bidder/preceding::bidder)) or (bidder/following::bidder and bidder/preceding::bidder)]/interval
-#B10 /site/open_auctions/open_auction[(not(bidder/following::bidder) or not(bidder/preceding::bidder)) and (bidder/following::bidder and bidder/preceding::bidder)]/interval
+B9 /site/open_auctions/open_auction[(not(bidder/following::bidder) or not(bidder/preceding::bidder)) or (bidder/following::bidder and bidder/preceding::bidder)]/interval
+B10 /site/open_auctions/open_auction[(not(bidder/following::bidder) or not(bidder/preceding::bidder)) and (bidder/following::bidder and bidder/preceding::bidder)]/interval
 B11 //open_auction/bidder/../bidder/../bidder/../interval
 B12 //item/@id/../@id/../@id/../@id/../name
 B13 //keyword/ancestor::parlist/descendant::keyword/ancestor::parlist/descendant::keyword/ancestor::parlist/descendant::keyword
 B11 //open_auction/bidder/../bidder/../bidder/../interval
 B12 //item/@id/../@id/../@id/../@id/../name
 B13 //keyword/ancestor::parlist/descendant::keyword/ancestor::parlist/descendant::keyword/ancestor::parlist/descendant::keyword