.
[SXSI/xpathcomp.git] / ata.ml
diff --git a/ata.ml b/ata.ml
index 77d5008..f2b9e72 100644 (file)
--- a/ata.ml
+++ b/ata.ml
@@ -58,8 +58,8 @@ struct
       match f.pos with
        | False -> 0
        | True -> 1
-       | Or (f1,f2) -> HASHINT3(PRIME2,f1.Node.id, f2.Node.id)
-       | And (f1,f2) -> HASHINT3(PRIME3,f1.Node.id,f2.Node.id)
+       | Or (f1,f2) -> HASHINT3(PRIME2,Uid.to_int f1.Node.id, Uid.to_int f2.Node.id)
+       | And (f1,f2) -> HASHINT3(PRIME3,Uid.to_int f1.Node.id, Uid.to_int f2.Node.id)
        | Atom(d,p,s) -> HASHINT4(PRIME4,hash_const_variant d,vb p,s)       
     end
 
@@ -197,7 +197,9 @@ module Transition = struct
   type node = State.t*TagSet.t*bool*Formula.t*bool
   include Hcons.Make(struct
                       type t = node
-                      let hash (s,ts,m,f,b) = HASHINT5(s,TagSet.uid ts,Formula.uid f,vb m,vb b)
+                      let hash (s,ts,m,f,b) = HASHINT5(s,Uid.to_int (TagSet.uid ts),
+                                                       Uid.to_int (Formula.uid f),
+                                                       vb m,vb b)
                       let equal (s,ts,b,f,m) (s',ts',b',f',m') = 
                         s == s' && ts == ts' && b==b' && m==m' && f == f'
                     end)
@@ -284,7 +286,9 @@ module FormTable = Hashtbl.Make(struct
                                  let equal (f1,s1,t1) (f2,s2,t2) =
                                    f1 == f2 && s1 == s2 && t1 == t2
                                  let hash (f,s,t) = 
-                                   HASHINT3(Formula.uid f ,StateSet.uid s,StateSet.uid t)
+                                   HASHINT3(Uid.to_int (Formula.uid f),
+                                            Uid.to_int (StateSet.uid s),
+                                            Uid.to_int (StateSet.uid t))
                                end)
 module F = Formula
 
@@ -332,7 +336,10 @@ module FTable = Hashtbl.Make(struct
                               type t = Tag.t*Formlist.t*StateSet.t*StateSet.t
                               let equal (tg1,f1,s1,t1) (tg2,f2,s2,t2) =
                                 tg1 == tg2 && f1 == f2 &&  s1 == s2 && t1 == t2;;
-                              let hash (tg,f,s,t) =  HASHINT4(tg,Formlist.uid f ,StateSet.uid s,StateSet.uid t);;
+                              let hash (tg,f,s,t) =  
+                                HASHINT4(tg, Uid.to_int (Formlist.uid f),
+                                         Uid.to_int (StateSet.uid s),
+                                         Uid.to_int (StateSet.uid t))
                             end)
 
 
@@ -528,13 +535,13 @@ let tags_of_state a q =
     module GResult(Doc : sig val doc : Tree.t end) = struct
       type bits
       type elt = [` Tree] Tree.node
-      external create_empty : int -> bits = "caml_result_set_create"
-      external set : bits -> int -> unit = "caml_result_set_set"
-      external next : bits -> int -> int = "caml_result_set_next"
-      external count : bits -> int  = "caml_result_set_count"
-      external clear : bits -> elt -> elt -> unit = "caml_result_set_clear"
+      external create_empty : int -> bits = "caml_result_set_create" "noalloc"
+      external set : bits -> int -> unit = "caml_result_set_set" "noalloc"
+      external next : bits -> int -> int = "caml_result_set_next" "noalloc"
+      external count : bits -> int  = "caml_result_set_count" "noalloc"
+      external clear : bits -> elt -> elt -> unit = "caml_result_set_clear" "noalloc"
         
-      external set_tag_bits : bits -> Tag.t -> Tree.t -> elt -> elt = "caml_set_tag_bits"
+      external set_tag_bits : bits -> Tag.t -> Tree.t -> elt -> elt = "caml_set_tag_bits" "noalloc"
       type t = 
         { segments : elt list;
           bits : bits;
@@ -817,11 +824,40 @@ END
          
       module SListTable = Hashtbl.Make(struct type t = SList.t
                                              let equal = (==)
-                                             let hash t = t.SList.Node.id 
+                                             let hash t = Uid.to_int t.SList.Node.id
                                       end)
 
 
-      module TransCacheOld = 
+      module TransCache =
+      struct
+       type cell = { key : int;
+                     obj : Obj.t }
+       type 'a t = cell array
+       let dummy = { key = 0; obj = Obj.repr () }
+       let create n = Array.create 25000 dummy
+       let hash a b = HASHINT2(Obj.magic a, Uid.to_int b.SList.Node.id)
+
+       let find_slot t key =
+         let rec loop i =
+           if (t.(i)  != dummy) && (t.(i).key != key)
+           then loop ((i+1 mod 25000))
+           else i
+         in loop (key mod 25000)
+       ;;
+
+       let find t k1 k2 = 
+         let i = find_slot t (hash k1 k2) in
+         if t.(i) == dummy then raise Not_found
+         else Obj.magic (t.(i).obj)
+           
+       let add t k1 k2 v = 
+         let key = hash k1 k2 in
+         let i = find_slot t key in
+         t.(i)<- { key = key; obj = (Obj.repr v) }
+
+      end
+
+      module TransCache2 = 
       struct
        type 'a t = Obj.t array SListTable.t
        let create n = SListTable.create n
@@ -863,34 +899,24 @@ END
        let create n = Array.create n dummy_cell
        let dummy = fun _ _-> assert false
        let find h tag slist =
-         let tab = get h slist.SList.Node.id in
+         let tab = get h (Uid.to_int slist.SList.Node.id) in
          if tab == dummy_cell then raise Not_found
          else
          let res = get tab tag in
          if res == dummy then raise Not_found else res
 
        let add (h : t) tag slist (data : fun_tree) =
-         let tab = get h slist.SList.Node.id in
+         let tab = get h (Uid.to_int slist.SList.Node.id) in
          let tab = if tab == dummy_cell then
-           let x = Array.create 10000 dummy in
-           (set h slist.SList.Node.id  x;x)
+           let x = Array.create 100000 dummy in
+           (set h (Uid.to_int slist.SList.Node.id) x;x)
          else tab
          in
          set tab tag data        
       end
        
-      module TransCache2 = struct
-       include Hashtbl.Make (struct
-                       type t = Tag.t*SList.t
-                       let equal (a,b) (c,d) = a==c && b==d
-                       let hash (a,b) = HASHINT2((Obj.magic a), b.SList.Node.id)
-                     end)
-
-       let add h t s d = add h (t,s) d
-       let find h t s = find h (t,s)
-      end
        
-      let td_trans = TransCache.create 10000 (* should be number of tags *number of states^2
+      let td_trans = TransCache.create 100000 (* should be number of tags *number of states^2
                                                in the document *)
 
       let empty_size n =
@@ -900,7 +926,7 @@ END
             
       module FllTable = Hashtbl.Make (struct type t = Formlistlist.t
                                             let equal = (==)
-                                            let hash t = t.Formlistlist.Node.id
+                                            let hash t = Uid.to_int t.Formlistlist.Node.id
                                      end)
        
       module Fold2Res = struct
@@ -913,59 +939,68 @@ END
          let v = Obj.repr ((),2,()) in
          Obj.magic v
 
-       let create n = Array.create n dummy
 
+       let create n = Array.create n dummy
        let find h tag fl s1 s2 = 
          let af = get h tag in
          if af == dummy then raise Not_found
          else 
-         let as1 = get af fl.Formlistlist.Node.id in
+         let as1 = get af (Uid.to_int fl.Formlistlist.Node.id) in
          if as1 == dummy then raise Not_found
          else 
-         let as2 = get as1 s1.SList.Node.id in
-         if as2 == dummy then raise Not_found 
-         else let v = get as2 s2.SList.Node.id in
-         if field1 v == 2 then raise Not_found 
-         else v
+         let as2 = get as1 (Uid.to_int s1.SList.Node.id) in
+         if as2 == dummy then raise Not_found
+         else 
+         let v = get as2 (Uid.to_int s2.SList.Node.id) in
+         if field1 v == 2 then raise Not_found
+         else 
+         v
+
        
        let add h tag fl s1 s2 data = 
          let af =
            let x = get h tag in
            if x == dummy then 
            begin
-             let y = Array.make 10000 dummy in
+             let y = Array.make 100000 dummy in
              set h tag y;y
            end
            else x
          in
          let as1 = 
-           let x = get af fl.Formlistlist.Node.id in 
+           let x = get af (Uid.to_int fl.Formlistlist.Node.id) in 
            if x == dummy then
            begin
-             let y = Array.make 10000 dummy in
-             set af fl.Formlistlist.Node.id y;y
+             let y = Array.make 100000 dummy in
+             set af (Uid.to_int fl.Formlistlist.Node.id) y;y
            end
            else x
          in
          let as2 = 
-           let x = get as1 s1.SList.Node.id in 
+           let x = get as1 (Uid.to_int s1.SList.Node.id) in 
            if x == dummy then 
            begin
-             let y = Array.make 10000 dummy_val in
-             set as1 s1.SList.Node.id y;y
+             let y = Array.make 100000 dummy_val in
+             set as1 (Uid.to_int s1.SList.Node.id) y;y
            end
            else x
          in
-         set as2 s2.SList.Node.id data    
+         set as2 (Uid.to_int s2.SList.Node.id) data    
       end
+
+
+
+
        
       module Fold2Res2 = struct
        include Hashtbl.Make(struct 
                               type t = Tag.t*Formlistlist.t*SList.t*SList.t
                               let equal (a,b,c,d) (x,y,z,t) =
                                 a == x && b == y && c == z && d == t
-                              let hash (a,b,c,d) = HASHINT4 (a,b.Formlistlist.Node.id,
-                                                             c.SList.Node.id,d.SList.Node.id)
+                              let hash (a,b,c,d) = HASHINT4 (a,
+                                                             Uid.to_int b.Formlistlist.Node.id,
+                                                             Uid.to_int c.SList.Node.id,
+                                                             Uid.to_int d.SList.Node.id)
                             end)
        let add h t f s1 s2 d =
          add h (t,f,s1,s2) d
@@ -973,6 +1008,38 @@ END
          find h (t,f,s1,s2)
       end
 
+      module Fold2ResOld =
+      struct
+       type cell = { key : int;
+                     obj : Obj.t }
+       type 'a t = cell array
+       let dummy = { key = 0; obj = Obj.repr () }
+       let create n = Array.create 25000 dummy
+       let hash a b c d = HASHINT4(Obj.magic a, 
+                                   Uid.to_int b.Formlistlist.Node.id, 
+                                   Uid.to_int c.SList.Node.id,
+                                   Uid.to_int d.SList.Node.id)
+
+       let find_slot t key =
+         let rec loop i =
+           if (t.(i)  != dummy) && (t.(i).key != key)
+           then loop ((i+1 mod 25000))
+           else i
+         in loop (key mod 25000)
+       ;;
+
+       let find t k1 k2 k3 k4 = 
+         let i = find_slot t (hash k1 k2 k3 k4) in
+         if t.(i) == dummy then raise Not_found
+         else Obj.magic (t.(i).obj)
+           
+       let add t k1 k2 k3 k4 v = 
+         let key = hash k1 k2 k3 k4 in
+         let i = find_slot t key in
+         t.(i)<- { key = key; obj = (Obj.repr v) }
+
+      end
+
       let h_fold2 = Fold2Res.create 10000
       
       let top_down ?(noright=false) a tree t slist ctx slot_size =     
@@ -1038,7 +1105,7 @@ END
                                     (ts,t)  ->
                                       if (TagSet.mem tag ts)
                                       then 
-                                        let _,_,_,f,_ = Transition.node t in
+                                        let _,_,_,f,_ = t.Transition.node in
                                         let (child,desc,below),(sibl,foll,after) = Formula.st f in
                                           (Formlist.cons t fl_acc,
                                            StateSet.union ll_acc below,
@@ -1075,7 +1142,7 @@ END
                          (fun t _ -> eval_fold2_slist fl_list t (Tree.tag tree t) empty_res empty_res)
                      |  _,`NIL -> (
                            match f_kind with
-                             |`TAG(tag') ->                            
+                             (*|`TAG(tag') ->                          
                                let default = fun t _ -> eval_fold2_slist fl_list t (Tree.tag tree t) empty_res
                                   (loop_tag tag' (first t) llist t )
                                in
@@ -1088,7 +1155,7 @@ END
                                then 
                                RS.mk_quick_tag_loop default llist 1 tree tag' 
                                else default
-                               else default
+                               else default *)
                             | _ ->
                                 (fun t _ -> eval_fold2_slist fl_list t (Tree.tag tree t) empty_res
                                   (loop (first t) llist t ))
@@ -1096,13 +1163,13 @@ END
                       | `NIL,_ -> (
                           match n_kind with
                             |`TAG(tag') ->
-                              (*if SList.equal rlist slist && tag == tag' then
+                              if SList.equal rlist slist && tag == tag' then
                               let rec loop t ctx = 
                                 if t == Tree.nil then empty_res else 
                                 let res2 = loop (next t ctx) ctx in                               
                                 eval_fold2_slist fl_list t tag res2 empty_res            
                               in loop
-                              else *)
+                              else 
                                (fun t ctx -> eval_fold2_slist fl_list t (Tree.tag tree t)
                                  (loop_tag tag' (next t ctx) rlist ctx ) empty_res)
                                                                                             
@@ -1189,7 +1256,7 @@ END
            if Ptss.mem s c.sets then
              { c with results = IMap.add s (RS.concat r (IMap.find s c.results)) c.results}
            else
-             { hash = HASHINT2(c.hash,Ptset.Int.uid s);
+             { hash = HASHINT2(c.hash,Uid.to_int (Ptset.Int.uid s));
                sets = Ptss.add s c.sets;
                results = IMap.add s r c.results
              }
@@ -1223,7 +1290,7 @@ END
            in
            let h,s =
              Ptss.fold 
-               (fun s (ah,ass) -> (HASHINT2(ah,Ptset.Int.uid s),
+               (fun s (ah,ass) -> (HASHINT2(ah, Uid.to_int (Ptset.Int.uid s)),
                                    Ptss.add s ass))
                (Ptss.union c1.sets c2.sets) (0,Ptss.empty)
            in
@@ -1270,7 +1337,7 @@ END
        let h_trans = Hashtbl.create 4096
 
        let get_up_trans slist ptag a tree =      
-         let key = (HASHINT2(SList.uid slist,ptag)) in
+         let key = (HASHINT2(Uid.to_int slist.SList.Node.id ,ptag)) in
            try
          Hashtbl.find h_trans key              
          with