Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit4352708

Browse files
committed
get rid of global locks in favor of domain local caches
1 parent4d74fcc commit4352708

File tree

7 files changed

+109
-95
lines changed

7 files changed

+109
-95
lines changed

‎analysis/bin/main.ml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ let main () =
119119
| [_;"cache-delete"; rootPath] -> (
120120
Cfg.readProjectConfigCache:=false;
121121
let uri=Uri.fromPath rootPathin
122-
matchPackages.findRoot~uri(Hashtbl.create0)with
122+
matchPackages.findRoot~uriwith
123123
|Some (`BsrootPath) -> (
124124
matchBuildSystem.getLibBs rootPathwith
125125
|None -> print_endline"\"ERR\""

‎analysis/src/AnalysisCache.ml‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
(* Helpers for domain-local caches*)
2+
3+
letmake_hashtbl (size: int) :('k, 'v) Hashtbl.t Domain.DLS.key=
4+
Domain.DLS.new_key (fun() ->Hashtbl.create size)
5+
6+
letget_hashtbl (key: ('k, 'v) Hashtbl.t Domain.DLS.key) :('k, 'v) Hashtbl.t=
7+
Domain.DLS.get key

‎analysis/src/Cmt.ml‎

Lines changed: 54 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
openSharedTypes
22

3+
moduleFullCache=struct
4+
letkey :(string, full) Hashtbl.t Domain.DLS.key=
5+
AnalysisCache.make_hashtbl64
6+
7+
letget() :(string, full) Hashtbl.t=AnalysisCache.get_hashtbl key
8+
end
9+
310
letfullForCmt~moduleName~package~uricmt=
411
matchShared.tryReadCmt cmtwith
512
|None ->None
@@ -8,45 +15,64 @@ let fullForCmt ~moduleName ~package ~uri cmt =
815
let extra=ProcessExtra.getExtra~file~infosin
916
Some {file; extra; package}
1017

11-
letfullFromUri~uri=
18+
letfullFromUriWithPackage~package~uri=
1219
let path=Uri.toPath uriin
13-
matchPackages.getPackage~uriwith
14-
|None ->None
15-
|Somepackage -> (
16-
let moduleName=
17-
BuildSystem.namespacedName package.namespace (FindFiles.getName path)
18-
in
19-
let incremental=
20-
if!Cfg.inIncrementalTypecheckingModethen
21-
let incrementalCmtPath=
22-
package.rootPath^"/lib/bs/___incremental"^"/"^ moduleName
23-
^
24-
matchFiles.classifySourceFile pathwith
25-
|Resi ->".cmti"
26-
|_ ->".cmt"
27-
in
28-
fullForCmt~moduleName~package~uri incrementalCmtPath
29-
elseNone
20+
let moduleName=
21+
BuildSystem.namespacedName package.namespace (FindFiles.getName path)
22+
in
23+
letcached_fullcmt_path=
24+
let cache=FullCache.get()in
25+
matchHashtbl.find_opt cache cmt_pathwith
26+
|Somev ->Some v
27+
|None -> (
28+
match fullForCmt~moduleName~package~uri cmt_pathwith
29+
|Somevasres ->
30+
Hashtbl.replace cache cmt_path v;
31+
res
32+
|None ->None)
33+
in
34+
if!Cfg.inIncrementalTypecheckingModethen
35+
let incrementalCmtPath=
36+
package.rootPath^"/lib/bs/___incremental"^"/"^ moduleName
37+
^
38+
matchFiles.classifySourceFile pathwith
39+
|Resi ->".cmti"
40+
|_ ->".cmt"
3041
in
31-
match incrementalwith
32-
|SomecmtInfo ->
33-
ifDebug.verbose()thenPrintf.printf"[cmt] Found incremental cmt\n";
34-
Some cmtInfo
42+
match cached_full incrementalCmtPathwith
43+
|Some_asx -> x
3544
|None -> (
45+
(* Fallback to non-incremental*)
3646
matchHashtbl.find_opt package.pathsForModule moduleNamewith
3747
|Somepaths ->
3848
let cmt= getCmtPath~uri pathsin
39-
fullForCmt~moduleName~package~uri cmt
49+
cached_full cmt
4050
|None ->
4151
prerr_endline ("can't find module"^ moduleName);
42-
None))
52+
None)
53+
else
54+
matchHashtbl.find_opt package.pathsForModule moduleNamewith
55+
|Somepaths ->
56+
let cmt= getCmtPath~uri pathsin
57+
cached_full cmt
58+
|None ->
59+
prerr_endline ("can't find module"^ moduleName);
60+
None
61+
62+
letfullFromUri~uri=
63+
matchPackages.getPackage~uriwith
64+
|None ->None
65+
|Somepackage -> fullFromUriWithPackage~package~uri
4366

4467
letfullsFromModule~package~moduleName=
45-
ifHashtbl.mem package.pathsForModule moduleNamethen
46-
let paths=Hashtbl.find package.pathsForModule moduleNamein
68+
matchHashtbl.find_opt package.pathsForModule moduleNamewith
69+
|None ->[]
70+
|Somepaths ->
4771
let uris= getUris pathsin
48-
uris|>List.filter_map (funuri -> fullFromUri~uri)
49-
else[]
72+
uris
73+
|>List.filter_map (funuri ->
74+
let cmt= getCmtPath~uri pathsin
75+
fullForCmt~moduleName~package~uri cmt)
5076

5177
letloadFullCmtFromPath~path=
5278
let uri=Uri.fromPath pathin

‎analysis/src/Packages.ml‎

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
openSharedTypes
22

3+
(* Domain-local caches for packages and URI->root mapping.*)
4+
moduleLocalCache=struct
5+
letpackages_key :(string, package) Hashtbl.t Domain.DLS.key=
6+
AnalysisCache.make_hashtbl1
7+
8+
letroots_key :(Uri.t, string) Hashtbl.t Domain.DLS.key=
9+
AnalysisCache.make_hashtbl30
10+
11+
letpackages()=AnalysisCache.get_hashtbl packages_key
12+
letroots()=AnalysisCache.get_hashtbl roots_key
13+
end
14+
315
(* Creates the `pathsForModule` hashtbl, which maps a `moduleName` to it's `paths` (the ml/re, mli/rei, cmt, and cmti files)*)
416
letmakePathsForModule~projectFilesAndPaths~dependenciesFilesAndPaths=
517
let pathsForModule=Hashtbl.create30in
@@ -200,14 +212,11 @@ let newBsPackage ~rootPath =
200212
Log.log ("Unable to read"^ bsconfigJson);
201213
None)
202214

203-
letfindRoot~uripackagesByRoot=
215+
letfindRoot~uri=
204216
let path=Uri.toPath uriin
205217
letreclooppath=
206218
if path="/"thenNone
207-
elseif
208-
SharedTypes.StateSync.with_lock (fun() ->
209-
Hashtbl.mem packagesByRoot path)
210-
thenSome (`Root path)
219+
elseifHashtbl.mem (LocalCache.packages()) paththenSome (`Root path)
211220
elseif
212221
Files.exists (Filename.concat path"rescript.json")
213222
||Files.exists (Filename.concat path"bsconfig.json")
@@ -219,29 +228,29 @@ let findRoot ~uri packagesByRoot =
219228
loop (ifSys.is_directory paththen pathelseFilename.dirname path)
220229

221230
letgetPackage~uri=
222-
letopenSharedTypesin
223-
match
224-
SharedTypes.StateSync.with_lock (fun() ->
225-
ifHashtbl.mem state.rootForUri urithen
226-
let root=Hashtbl.find state.rootForUri uriin
227-
Some (Hashtbl.find state.packagesByRoot root)
228-
elseNone)
229-
with
230-
|Somepkg ->Some pkg
231+
let roots=LocalCache.roots()in
232+
let packages=LocalCache.packages()in
233+
matchHashtbl.find_opt roots uriwith
234+
|Someroot ->Hashtbl.find_opt packages root
231235
|None -> (
232-
match findRoot~uristate.packagesByRootwith
236+
match findRoot~uriwith
233237
|None ->
234238
Log.log"No root directory found";
235239
None
236-
|Some (`RootrootPath) ->
237-
SharedTypes.StateSync.with_lock (fun() ->
238-
Hashtbl.replace state.rootForUri uri rootPath;
239-
Some (Hashtbl.find state.packagesByRoot rootPath))
240+
|Some (`RootrootPath) -> (
241+
Hashtbl.replace roots uri rootPath;
242+
matchHashtbl.find_opt packages rootPathwith
243+
|Somepkg ->Some pkg
244+
|None -> (
245+
match newBsPackage~rootPathwith
246+
|Somepkg ->
247+
Hashtbl.replace packages rootPath pkg;
248+
Some pkg
249+
|None ->None))
240250
|Some (`BsrootPath) -> (
241251
match newBsPackage~rootPathwith
242252
|None ->None
243253
|Somepackage ->
244-
SharedTypes.StateSync.with_lock (fun() ->
245-
Hashtbl.replace state.rootForUri uri package.rootPath;
246-
Hashtbl.replace state.packagesByRoot package.rootPath package;
247-
Some package)))
254+
Hashtbl.replace roots uri package.rootPath;
255+
Hashtbl.replace packages package.rootPath package;
256+
Some package))

‎analysis/src/ProcessCmt.ml‎

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -773,27 +773,16 @@ let fileForCmtInfos ~moduleName ~uri
773773
|_ ->File.create moduleName uri
774774

775775
letfileForCmt~moduleName~cmt~uri=
776-
(* Double-checked locking: fast path under lock; if missing, compute without
777-
holding the lock, then insert under lock if still absent.*)
778-
match
779-
SharedTypes.StateSync.with_lock (fun() ->
780-
Hashtbl.find_opt state.cmtCache cmt)
781-
with
776+
let local=SharedTypes.CmtCache.get()in
777+
matchHashtbl.find_opt local cmtwith
782778
|Somefile ->Some file
783779
|None -> (
784780
matchShared.tryReadCmt cmtwith
785781
|None ->None
786782
|Someinfos ->
787783
let file= fileForCmtInfos~moduleName~uri infosin
788-
let cached=
789-
SharedTypes.StateSync.with_lock (fun() ->
790-
matchHashtbl.find_opt state.cmtCache cmtwith
791-
|Somef ->Some f
792-
|None ->
793-
Hashtbl.replace state.cmtCache cmt file;
794-
Some file)
795-
in
796-
cached)
784+
Hashtbl.replace local cmt file;
785+
Some file)
797786

798787
letfileForModulemoduleName~package=
799788
matchHashtbl.find_opt package.pathsForModule moduleNamewith

‎analysis/src/References.ml‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,9 @@ let alternateDeclared ~(file : File.t) ~package (declared : _ Declared.t) tip =
219219
maybeLog
220220
("alternateDeclared for"^ file.moduleName^" has both resi and res");
221221
let alternateUri=ifUri.isInterface file.urithen reselse resiin
222-
matchCmt.fullFromUri~uri:(Uri.fromPath alternateUri)with
222+
match
223+
Cmt.fullFromUriWithPackage~package~uri:(Uri.fromPath alternateUri)
224+
with
223225
|None ->None
224226
|Some{file; extra} -> (
225227
let env=QueryEnv.fromFile filein
@@ -568,7 +570,7 @@ let allReferencesForLocItem ~domain_mgr ~full:({file; package} as full) locItem
568570
matchProcessCmt.fileForModule~package namewith
569571
|None ->[]
570572
|Somefile -> (
571-
matchCmt.fullFromUri~uri:file.uriwith
573+
matchCmt.fullFromUriWithPackage~package~uri:file.uriwith
572574
|None ->[]
573575
|Somefull -> (
574576
match
@@ -612,7 +614,7 @@ let allReferencesForLocItem ~domain_mgr ~full:({file; package} as full) locItem
612614
match exportedForTip~env~path~package~tipwith
613615
|None ->[]
614616
|Some (env,_name,stamp) -> (
615-
matchCmt.fullFromUri~uri:env.file.uriwith
617+
matchCmt.fullFromUriWithPackage~package~uri:env.file.uriwith
616618
|None ->[]
617619
|Somefull ->
618620
maybeLog

‎analysis/src/SharedTypes.ml‎

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -537,34 +537,15 @@ let initExtra () =
537537
locItems=[];
538538
}
539539

540-
typestate= {
541-
packagesByRoot: (string,package)Hashtbl.t;
542-
rootForUri: (Uri.t,string)Hashtbl.t;
543-
cmtCache: (filePath,File.t)Hashtbl.t;
544-
}
540+
moduleCmtCache=struct
541+
letkey :(filePath, File.t) Hashtbl.t Domain.DLS.key=
542+
AnalysisCache.make_hashtbl30
545543

546-
(* There's only one state, so it can as well be global*)
547-
let state=
548-
{
549-
packagesByRoot=Hashtbl.create1;
550-
rootForUri=Hashtbl.create30;
551-
cmtCache=Hashtbl.create30;
552-
}
553-
554-
moduleStateSync=struct
555-
letmutex :Mutex.t=Mutex.create()
556-
557-
letwith_lockf=
558-
Mutex.lock mutex;
559-
match f()with
560-
|v ->
561-
Mutex.unlock mutex;
562-
v
563-
|exceptionexn ->
564-
Mutex.unlock mutex;
565-
raiseexn
544+
letget() :(filePath, File.t) Hashtbl.t=AnalysisCache.get_hashtbl key
566545
end
567546

547+
moduleStringMap=Map.Make (String)
548+
568549
letlocKindToString=function
569550
|LocalReference (_,tip) ->"(LocalReference"^Tip.toString tip^")"
570551
|GlobalReference_ ->"GlobalReference"

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp