Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 12 additions & 15 deletions impl/datascript.ml
Original file line number Diff line number Diff line change
Expand Up @@ -3380,11 +3380,11 @@ module Query = struct
|> List.sort_uniq compare
| `InColl (attr, values) ->
(* attr membership against a bound collection *)
let set = Hashtbl.create (List.length values) in
List.iter (fun value -> Hashtbl.replace set (Marshal.to_string value []) value) values;
let set = Query_api_impl.Query_value_table.create (List.length values) in
List.iter (fun value -> Query_api_impl.Query_value_table.replace set value ()) values;
primary_attr_datoms db Aevt attr
|> List.filter_map (fun (datom : datom) ->
if Hashtbl.mem set (Marshal.to_string datom.v []) then Some datom.e else None)
if Query_api_impl.Query_value_table.mem set datom.v then Some datom.e else None)
|> List.sort_uniq compare
| `EntitySet ids -> List.sort_uniq compare ids
| `Or _ | `ValueIn _ -> assert false (* handled below *)
Expand Down Expand Up @@ -3601,17 +3601,16 @@ module Query = struct
| Ref id -> Result_entity id
| v -> Result_value v
in
let seen = Hashtbl.create (List.length entity_ids) in
let seen = Query_api_impl.Query_result_table.create (List.length entity_ids) in
entity_ids
|> List.filter_map (fun entity_id ->
let out =
match entity_attr_index_datoms db entity_id attr with
| (d : datom) :: _ -> out_of d.v
| [] -> out_of default
in
let key = Marshal.to_string out [] in
if Hashtbl.mem seen key then None
else (Hashtbl.replace seen key (); Some out)))
if Query_api_impl.Query_result_table.mem seen out then None
else (Query_api_impl.Query_result_table.replace seen out (); Some out)))
| Some (entity_var, `Attr attr) ->
simple_attr_entity_ids db ?resolve ?coll entity_var query
|> Option.map (fun entity_ids ->
Expand All @@ -3626,7 +3625,7 @@ module Query = struct
| Some bound -> fun (d : datom) -> compare_value d.v bound = 0
| None -> fun _ -> true
in
let seen = Hashtbl.create (List.length entity_ids) in
let seen = Query_api_impl.Query_result_table.create (List.length entity_ids) in
primary_attr_datoms db Aevt attr
|> List.filter_map (fun (d : datom) ->
if d.e >= 0 && d.e < Bytes.length wanted
Expand All @@ -3636,9 +3635,8 @@ module Query = struct
| Ref id -> Result_entity id
| _ -> Result_value d.v
in
let key = Marshal.to_string out [] in
if Hashtbl.mem seen key then None
else (Hashtbl.replace seen key (); Some out)
if Query_api_impl.Query_result_table.mem seen out then None
else (Query_api_impl.Query_result_table.replace seen out (); Some out)
else None))

let simple_attr_entity_collection db ?resolve ?coll query =
Expand Down Expand Up @@ -4179,12 +4177,11 @@ module Query = struct
let rows =
if List.mem find_var find_var_names then rows
else
let seen = Hashtbl.create (List.length rows) in
let seen = Query_api_impl.List_row_table.create (List.length rows) in
List.filter
(fun row ->
let key = Marshal.to_string row [] in
if Hashtbl.mem seen key then false
else (Hashtbl.replace seen key (); true))
if Query_api_impl.List_row_table.mem seen row then false
else (Query_api_impl.List_row_table.replace seen row (); true))
rows
in
Query_relation rows))
Expand Down
16 changes: 16 additions & 0 deletions impl/query_api.ml
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,22 @@ end) = struct
let hash row = List.fold_left (fun acc v -> acc * 31 + query_result_hash v) 0 row
end)

module Query_value_table = Hashtbl.Make (struct
type t = value

let equal left right = compare_query_value left right = 0

let hash = query_value_hash
end)

module Query_result_table = Hashtbl.Make (struct
type t = query_result

let equal left right = compare_query_result left right = 0

let hash = query_result_hash
end)

(* O(n) hash dedup preserving first-occurrence order. [q] results are
sets, so row order is unspecified — sorting survivors would only add
O(n log n) cost for no observable difference. *)
Expand Down
Loading