Skip to content

Commit 50cd2c6

Browse files
sinancepelfacebook-github-bot
authored andcommitted
introduce the concept of a singleton key for shared memory
Summary: We had the pattern of only using one canonical key when we wanted to store sets in shared memory. We were previously relying on ad-hoc int or string keys, but since we have 3 uses now, I think it makes sense to abstract this pattern. Reviewed By: dkgi Differential Revision: D14726545 fbshipit-source-id: d3f19be61adb8ddd1c26641b87480f89be783136
1 parent 221dcf8 commit 50cd2c6

File tree

11 files changed

+70
-51
lines changed

11 files changed

+70
-51
lines changed

ast/sharedMemory.ml

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -177,20 +177,20 @@ module HandleKeys = struct
177177
let description = "All handles"
178178
end
179179

180-
module HandleKeys = SharedMemory.WithCache (IntKey) (HandleKeysValue)
180+
module HandleKeys = SharedMemory.WithCache (SharedMemory.SingletonKey) (HandleKeysValue)
181181

182182
let get () =
183-
HandleKeys.get 0
183+
HandleKeys.get SharedMemory.SingletonKey.key
184184
|> Option.value ~default:File.Handle.Set.Tree.empty
185185

186186
let clear () =
187-
HandleKeys.remove_batch (HandleKeys.KeySet.singleton 0)
187+
HandleKeys.remove_batch (HandleKeys.KeySet.singleton SharedMemory.SingletonKey.key)
188188

189189
let add ~handles:new_keys =
190190
let handles = get () in
191191
clear ();
192192
let handles = File.Handle.Set.Tree.union handles new_keys in
193-
HandleKeys.add 0 handles
193+
HandleKeys.add SharedMemory.SingletonKey.key handles
194194

195195
let remove ~handles:old_keys =
196196
let handles = get () in
@@ -200,7 +200,7 @@ module HandleKeys = struct
200200
|> File.Handle.Set.Tree.of_list
201201
|> File.Handle.Set.Tree.diff handles
202202
in
203-
HandleKeys.add 0 handles
203+
HandleKeys.add SharedMemory.SingletonKey.key handles
204204

205205
let normalize () =
206206
let handles = get () in
@@ -209,16 +209,10 @@ module HandleKeys = struct
209209
|> File.Handle.Set.Tree.to_list
210210
|> List.sort ~compare:File.Handle.compare
211211
|> File.Handle.Set.Tree.of_list
212-
|> HandleKeys.add 0
213-
214-
let hash_of_key =
215-
HandleKeys.hash_of_key
216-
217-
let serialize_key =
218-
HandleKeys.serialize_key
212+
|> HandleKeys.add SharedMemory.SingletonKey.key
219213

220214
let compute_hashes_to_keys () =
221-
HandleKeys.compute_hashes_to_keys ~keys:[0]
215+
HandleKeys.compute_hashes_to_keys ~keys:[SharedMemory.SingletonKey.key]
222216
end
223217

224218

ast/sharedMemory.mli

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ end
5757

5858
module HandleKeys: sig
5959
module HandleKeysValue: Value.Type with type t = File.Handle.Set.Tree.t
60-
module HandleKeys: module type of Memory.NoCache (IntKey) (HandleKeysValue)
60+
module HandleKeys: module type of Memory.NoCache (Memory.SingletonKey) (HandleKeysValue)
6161

6262
val get: unit -> File.Handle.Set.Tree.t
6363

@@ -70,11 +70,6 @@ module HandleKeys: sig
7070

7171
val normalize: unit -> unit
7272

73-
(* Exposed for testing. *)
74-
val hash_of_key: int -> string
75-
val serialize_key: int -> string
76-
77-
7873
val compute_hashes_to_keys: unit -> string String.Map.t
7974
end
8075

ast/test/sharedMemoryTest.ml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,10 @@ let test_compute_hashes_to_keys _ =
6666
~keys:[File.Handle.create "first.py"; File.Handle.create "second/__init__.py"]
6767
);
6868
assert_mapping_equal
69-
[HandleKeys.hash_of_key 0, HandleKeys.serialize_key 0]
69+
[
70+
HandleKeys.HandleKeys.hash_of_key Memory.SingletonKey.key,
71+
HandleKeys.HandleKeys.serialize_key Memory.SingletonKey.key;
72+
]
7073
(HandleKeys.compute_hashes_to_keys ());
7174
assert_mapping_equal
7275
[

command/test/serverTest.ml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1087,8 +1087,8 @@ let test_compute_hashes_to_keys context =
10871087
@
10881088
[
10891089
to_binding
1090-
(OrderKeys.hash_of_key "Order")
1091-
(OrderKeys.serialize_key "Order");
1090+
(OrderKeys.hash_of_key SharedMemory.SingletonKey.key)
1091+
(OrderKeys.serialize_key SharedMemory.SingletonKey.key);
10921092
to_binding
10931093
(OrderIndices.hash_of_key (Type.Primitive "sixteen"))
10941094
(OrderIndices.serialize_key (Type.Primitive "sixteen"));
@@ -1144,7 +1144,9 @@ let test_compute_hashes_to_keys context =
11441144
to_binding
11451145
(Coverage.SharedMemory.hash_of_key (File.Handle.create "sample.py"))
11461146
(Coverage.SharedMemory.serialize_key (File.Handle.create "sample.py"));
1147-
to_binding (Protocols.hash_of_key 0) (Protocols.serialize_key 0);
1147+
to_binding
1148+
(Protocols.hash_of_key SharedMemory.SingletonKey.key)
1149+
(Protocols.serialize_key SharedMemory.SingletonKey.key);
11481150
]
11491151
|> List.sort ~compare
11501152
in

server/request.ml

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -457,10 +457,10 @@ let process_type_query_request ~state:({ State.environment; _ } as state) ~confi
457457
let map =
458458
Map.set
459459
String.Map.empty
460-
~key:(OrderKeys.hash_of_key "Order")
461-
~data:(OrderKeys.serialize_key "Order")
460+
~key:(OrderKeys.hash_of_key SharedMemory.SingletonKey.key)
461+
~data:(OrderKeys.serialize_key SharedMemory.SingletonKey.key)
462462
in
463-
match OrderKeys.get "Order" with
463+
match OrderKeys.get SharedMemory.SingletonKey.key with
464464
| Some indices ->
465465
let annotations = List.filter_map indices ~f:OrderAnnotations.get in
466466
extend_map
@@ -544,7 +544,10 @@ let process_type_query_request ~state:({ State.environment; _ } as state) ~confi
544544
extend_map map ~new_map:(Coverage.SharedMemory.compute_hashes_to_keys ~keys:handles)
545545
in
546546
(* Protocols. *)
547-
let map = extend_map map ~new_map:(Protocols.compute_hashes_to_keys ~keys:[0]) in
547+
let map =
548+
extend_map
549+
map
550+
~new_map:(Protocols.compute_hashes_to_keys ~keys:[SharedMemory.SingletonKey.key]) in
548551
map
549552
|> Map.to_alist
550553
|> List.sort ~compare:(fun (left, _) (right, _) -> String.compare left right)
@@ -710,7 +713,7 @@ let process_type_query_request ~state:({ State.environment; _ } as state) ~confi
710713
Some {
711714
TypeQuery.serialized_key;
712715
kind = OrderKeyValue.description;
713-
actual_key = key;
716+
actual_key = Int.to_string key;
714717
actual_value =
715718
value
716719
>>| List.to_string ~f:Int.to_string;

service/environment.ml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -190,12 +190,12 @@ module SharedHandler: Analysis.Environment.Handler = struct
190190
ClassDefinitions.get
191191

192192
let register_protocol protocol =
193-
let protocols = Protocols.get 0 |> Option.value ~default:[] in
194-
Protocols.add 0 (protocol :: protocols)
193+
let protocols = Protocols.get SharedMemory.SingletonKey.key |> Option.value ~default:[] in
194+
Protocols.add SharedMemory.SingletonKey.key (protocol :: protocols)
195195

196196

197197
let protocols () =
198-
Protocols.get 0
198+
Protocols.get SharedMemory.SingletonKey.key
199199
|> Option.value ~default:[]
200200

201201
let register_module ~qualifier ~local_mode ~handle ~stub ~statements =
@@ -543,7 +543,7 @@ let populate_shared_memory
543543

544544
add_table OrderIndices.write_through indices;
545545
add_table OrderAnnotations.write_through annotations;
546-
OrderKeys.write_through "Order" (Hashtbl.keys annotations);
546+
OrderKeys.write_through SharedMemory.SingletonKey.key (Hashtbl.keys annotations);
547547
in
548548
add_type_order order;
549549

@@ -557,7 +557,7 @@ let populate_shared_memory
557557
add_table GlobalKeys.write_through (Hashtbl.map ~f:Hash_set.to_list global_keys);
558558
add_table DependentKeys.write_through (Hashtbl.map ~f:Hash_set.to_list dependent_keys);
559559

560-
Protocols.write_through 0 (Hash_set.to_list protocols);
560+
Protocols.write_through SharedMemory.SingletonKey.key (Hash_set.to_list protocols);
561561
add_table
562562
(fun qualifier ast_module -> Ast.SharedMemory.Modules.add ~qualifier ~ast_module)
563563
modules;
@@ -584,13 +584,13 @@ let normalize_shared_memory () =
584584
(* Since we don't provide an API to the raw order keys in the type order handler,
585585
handle it inline here. *)
586586
begin
587-
match OrderKeys.get "Order" with
587+
match OrderKeys.get SharedMemory.SingletonKey.key with
588588
| None ->
589589
()
590590
| Some keys ->
591-
OrderKeys.remove_batch (OrderKeys.KeySet.singleton "Order");
591+
OrderKeys.remove_batch (OrderKeys.KeySet.singleton SharedMemory.SingletonKey.key);
592592
List.sort ~compare:Int.compare keys
593-
|> OrderKeys.add "Order";
593+
|> OrderKeys.add SharedMemory.SingletonKey.key;
594594
end;
595595
Ast.SharedMemory.HandleKeys.normalize ();
596596
let handles = Ast.SharedMemory.HandleKeys.get () in

service/environmentSharedMemory.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ module Globals = Memory.WithCache (Ast.SharedMemory.ReferenceKey) (GlobalValue)
148148

149149
module Dependents = Memory.WithCache (Ast.SharedMemory.ReferenceKey) (DependentValue)
150150

151-
module Protocols = Memory.WithCache (Ast.SharedMemory.IntKey) (ProtocolValue)
151+
module Protocols = Memory.WithCache (Memory.SingletonKey) (ProtocolValue)
152152

153153
(** Keys *)
154154
module FunctionKeys = Memory.WithCache (Ast.SharedMemory.HandleKey) (FunctionKeyValue)
@@ -170,7 +170,7 @@ module OrderEdges = Memory.WithCache (Ast.SharedMemory.IntKey) (EdgeValue)
170170

171171
module OrderBackedges = Memory.WithCache (Ast.SharedMemory.IntKey) (BackedgeValue)
172172

173-
module OrderKeys = Memory.WithCache (StringKey) (OrderKeyValue)
173+
module OrderKeys = Memory.WithCache (Memory.SingletonKey) (OrderKeyValue)
174174

175175
module StoredConfiguration = Memory.NoCache (StringKey) (ConfigurationValue)
176176

service/memory.ml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,3 +231,14 @@ let save_shared_memory ~path =
231231

232232
let load_shared_memory ~path =
233233
SharedMem.load_table path
234+
235+
236+
module SingletonKey = struct
237+
type t = int
238+
let to_string = Core.Int.to_string
239+
let compare = Core.Int.compare
240+
241+
type out = int
242+
let from_string = Core.Int.of_string
243+
let key = 0
244+
end

service/memory.mli

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ module NoCache (Key: KeyType) (Value: Value.Type): sig
4343

4444
include SharedMemory.NoCache with
4545
type t = Value.t
46-
and type key = Key.t
47-
and module KeySet = Set.Make (Key)
48-
and module KeyMap = MyMap.Make (Key)
46+
and type key = Key.t
47+
and module KeySet = Set.Make (Key)
48+
and module KeyMap = MyMap.Make (Key)
4949
end
5050

5151

@@ -58,9 +58,9 @@ module WithCache (Key: KeyType) (Value: Value.Type): sig
5858

5959
include SharedMemory.WithCache with
6060
type t = Value.t
61-
and type key = Key.t
62-
and module KeySet = Set.Make (Key)
63-
and module KeyMap = MyMap.Make (Key)
61+
and type key = Key.t
62+
and module KeySet = Set.Make (Key)
63+
and module KeyMap = MyMap.Make (Key)
6464
end
6565

6666
val get_heap_handle: Configuration.Analysis.t -> SharedMemory.handle
@@ -73,3 +73,14 @@ val save_shared_memory: path: string -> unit
7373
val load_shared_memory: path: string -> unit
7474

7575
val unsafe_little_endian_representation: key: Digest.t -> Int64.t
76+
77+
module SingletonKey: sig
78+
type t
79+
val to_string: t -> string
80+
val compare: t -> t -> int
81+
82+
type out = int
83+
val from_string: string -> int
84+
85+
val key: t
86+
end

service/test/environmentTest.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ let test_normalize_order_keys _ =
186186
in
187187
Service.Environment.normalize_shared_memory ();
188188
assert_equal
189-
(Service.EnvironmentSharedMemory.OrderKeys.get "Order")
189+
(Service.EnvironmentSharedMemory.OrderKeys.get SharedMemory.SingletonKey.key)
190190
(Some indices)
191191

192192

service/typeOrder.ml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,19 +79,19 @@ module Handler = struct
7979
set key data
8080

8181
let length _ =
82-
(OrderKeys.get "Order"
82+
(OrderKeys.get SharedMemory.SingletonKey.key
8383
>>| List.length)
8484
|> Option.value ~default:0
8585

8686
let add_key key =
87-
match OrderKeys.get "Order" with
88-
| None -> OrderKeys.add "Order" [key]
87+
match OrderKeys.get SharedMemory.SingletonKey.key with
88+
| None -> OrderKeys.add SharedMemory.SingletonKey.key [key]
8989
| Some keys ->
90-
OrderKeys.remove_batch (OrderKeys.KeySet.singleton "Order");
91-
OrderKeys.add "Order" (key :: keys)
90+
OrderKeys.remove_batch (OrderKeys.KeySet.singleton SharedMemory.SingletonKey.key);
91+
OrderKeys.add SharedMemory.SingletonKey.key (key :: keys)
9292

9393
let keys () =
94-
Option.value ~default:[] (OrderKeys.get "Order")
94+
Option.value ~default:[] (OrderKeys.get SharedMemory.SingletonKey.key)
9595

9696
let show () =
9797
let keys =

0 commit comments

Comments
 (0)