Skip to content

Commit d47ad14

Browse files
authored
updates the KB version number and adds a few more microoptimizations (#1412)
Updates the version number to v2 to fix the cache broken in #1411 and adds a couple more small optimizations that give some extra performance boost (about 2%, but still).
1 parent a4054d2 commit d47ad14

File tree

1 file changed

+52
-35
lines changed

1 file changed

+52
-35
lines changed

lib/knowledge/bap_knowledge.ml

Lines changed: 52 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -164,11 +164,6 @@ module Pid : Sid = Int63
164164
let user_package = "user"
165165
let keyword_package = "keyword"
166166

167-
type slot_status =
168-
| Sleep
169-
| Awoke
170-
| Ready
171-
172167
type fullname = {
173168
package : string;
174169
name : string;
@@ -2297,6 +2292,23 @@ module Knowledge = struct
22972292
| None -> Env.empty_class
22982293
| Some objs -> objs
22992294

2295+
let update_objects {Class.name} f =
2296+
get () >>= fun state ->
2297+
let objs = f @@ match Map.find state.classes name with
2298+
| None -> Env.empty_class
2299+
| Some objs -> objs in
2300+
put {state with classes = Map.set state.classes name objs}
2301+
2302+
let map_update_objects {Class.name} f =
2303+
get () >>= fun state ->
2304+
let objs = match Map.find state.classes name with
2305+
| None -> Env.empty_class
2306+
| Some objs -> objs in
2307+
f objs @@ fun objs res ->
2308+
put {state with classes = Map.set state.classes name objs} >>| fun () ->
2309+
res
2310+
2311+
23002312
module Object = struct
23012313
type +'a t = 'a obj
23022314
type 'a ord = Oid.comparator_witness
@@ -2575,12 +2587,15 @@ module Knowledge = struct
25752587

25762588
let uid {Slot.name} = name
25772589

2578-
let is_empty
2579-
: _ slot -> _ -> _ obj -> bool =
2580-
fun slot vals obj -> match Map.find vals obj with
2581-
| None -> true
2582-
| Some v ->
2583-
Domain.is_empty slot.dom (Record.get slot.key slot.dom v)
2590+
2591+
type slot_status =
2592+
| Sleep
2593+
| Awoke
2594+
| Ready of Dict.record
2595+
2596+
let is_empty {Slot.dom; key} v =
2597+
Domain.is_empty dom (Record.get key dom v)
2598+
[@@inline]
25842599

25852600
let status
25862601
: ('a,_) slot -> 'a obj -> slot_status knowledge =
@@ -2589,22 +2604,24 @@ module Knowledge = struct
25892604
match Map.find comp obj with
25902605
| None -> Sleep
25912606
| Some slots -> match Map.find slots (uid slot) with
2592-
| None -> if is_empty slot vals obj then Sleep else Ready
25932607
| Some Work _ -> Awoke
2594-
| Some Done -> Ready
2608+
| other -> match other,Map.find vals obj with
2609+
| Some Work _,_ -> assert false
2610+
| None,None -> Sleep
2611+
| Some Done,None -> Ready Record.empty
2612+
| Some Done,Some v -> Ready v
2613+
| None,Some v -> if is_empty slot v then Sleep else Ready v
25952614

25962615
let update_slot
25972616
: ('a,_) slot -> 'a obj -> _ -> unit knowledge =
25982617
fun slot obj f ->
2599-
objects slot.cls >>= fun ({comp} as objs) ->
2618+
update_objects slot.cls @@ fun ({comp} as objs) ->
26002619
let comp = Map.update comp obj ~f:(fun slots ->
26012620
let slots = match slots with
26022621
| None -> Map.empty (module Name)
26032622
| Some slots -> slots in
26042623
Map.update slots (uid slot) ~f) in
2605-
get () >>= fun s ->
2606-
let classes = Map.set s.classes slot.cls.name {objs with comp} in
2607-
put {s with classes}
2624+
{objs with comp}
26082625

26092626
let enter_slot : ('a,_) slot -> 'a obj -> unit knowledge = fun s x ->
26102627
update_slot s x @@ function
@@ -2636,21 +2653,24 @@ module Knowledge = struct
26362653
update_work s x @@ fun {waiting; current} ->
26372654
Work {waiting = Set.union current waiting; current}
26382655

2639-
let collect_waiting
2640-
: ('a,'p) slot -> 'a obj -> _ Knowledge.t = fun s x ->
2641-
objects s.cls >>| fun {comp} ->
2642-
Map.find_exn (Map.find_exn comp x) (uid s) |> function
2643-
| Env.Done -> assert false
2644-
| Env.Work {waiting} ->
2645-
Set.fold waiting ~init:[] ~f:(fun ps p ->
2646-
Hashtbl.find_exn s.Slot.promises p :: ps)
2647-
2648-
let dequeue_waiting s x = update_work s x @@ fun _ ->
2649-
Work {
2656+
let no_work = Env.Work {
26502657
waiting = Set.empty (module Pid);
26512658
current = Set.empty (module Pid)
26522659
}
26532660

2661+
let dequeue_waiting
2662+
: ('a,'p) slot -> 'a obj -> _ Knowledge.t = fun s x ->
2663+
map_update_objects s.cls @@ fun ({comp} as objs) k ->
2664+
let works = Map.find_exn comp x in
2665+
Map.find_exn works (uid s) |> function
2666+
| Env.Done -> assert false
2667+
| Env.Work {waiting} ->
2668+
let waiting = Set.fold waiting ~init:[] ~f:(fun ps p ->
2669+
Hashtbl.find_exn s.Slot.promises p :: ps) in
2670+
let works = Map.set works (uid s) no_work in
2671+
let objs = {objs with comp = Map.set comp x works} in
2672+
k objs waiting
2673+
26542674
let initial_promises {Slot.promises} = Hashtbl.data promises
26552675

26562676
let current : type a p. (a,p) slot -> a obj -> p Knowledge.t =
@@ -2668,8 +2688,7 @@ module Knowledge = struct
26682688
enter_promise slot obj pid >>= fun () ->
26692689
run obj >>= fun () ->
26702690
leave_promise slot obj pid) >>= fun () ->
2671-
collect_waiting slot obj >>= fun waiting ->
2672-
dequeue_waiting slot obj >>= fun () ->
2691+
dequeue_waiting slot obj >>= fun waiting ->
26732692
match waiting with
26742693
| [] -> Knowledge.return ()
26752694
| promises ->
@@ -2678,15 +2697,13 @@ module Knowledge = struct
26782697
| EQ | LT -> Knowledge.return ()
26792698
| GT | NC -> collect_inner slot obj promises
26802699

2681-
2682-
26832700
let collect : type a p. (a,p) slot -> a obj -> p Knowledge.t =
26842701
fun slot id ->
26852702
if Object.is_null id
26862703
then !!(Domain.empty slot.dom)
26872704
else status slot id >>= function
2688-
| Ready ->
2689-
current slot id
2705+
| Ready v ->
2706+
Knowledge.return @@ Record.get slot.key slot.dom v
26902707
| Awoke ->
26912708
enqueue_promises slot id >>= fun () ->
26922709
current slot id
@@ -3067,7 +3084,7 @@ module Knowledge = struct
30673084
then None
30683085
else Some {key=oid; sym; data; comp}) in
30693086
cid,(last,data)) in {
3070-
version = V1;
3087+
version = V2;
30713088
payload;
30723089
}
30733090

0 commit comments

Comments
 (0)