Skip to content

Commit 5c65118

Browse files
committed
Refactor cache management in parser.ml by removing unnecessary mutex locks
- Eliminated mutex locks for cache operations, simplifying the code and improving performance. - Streamlined cache access methods by directly manipulating hash tables without locking, while maintaining thread safety through other means. - Enhanced error handling and clarity in cache operations, ensuring consistent behavior across the module.
1 parent 5c6fbf6 commit 5c65118

File tree

1 file changed

+13
-52
lines changed

1 file changed

+13
-52
lines changed

lib/parser.ml

Lines changed: 13 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,6 @@ module Cache = struct
2525
(* AST dependency check result cache *)
2626
let ast_dependency_cache = Hashtbl.create 200
2727

28-
(* Mutex locks for thread safety *)
29-
let cache_table_mutex = Eio.Mutex.create ()
30-
let ast_cache_mutex = Eio.Mutex.create ()
31-
let ast_dependency_cache_mutex = Eio.Mutex.create ()
32-
3328
(* Initialize the cache - simplified to just return false as we don't load from disk anymore *)
3429
let initialize ?(verbose = false) ?(skip_cache = false) () =
3530
if verbose then
@@ -45,65 +40,48 @@ module Cache = struct
4540
(* Add or update an entry in the cache *)
4641
let add ?(verbose = false) ?(skip_cache = false) path module_info =
4742
(* Skip adding to cache if skip_cache is set *)
48-
if not skip_cache then (
43+
if not skip_cache then
4944
try
50-
Eio.Mutex.lock cache_table_mutex;
5145
let entry = { module_info } in
52-
Hashtbl.replace cache_table path entry;
53-
Eio.Mutex.unlock cache_table_mutex
46+
Hashtbl.replace cache_table path entry
5447
with exn ->
55-
Eio.Mutex.unlock cache_table_mutex;
5648
if verbose then
57-
Printf.eprintf "Error adding to cache: %s\n" (Printexc.to_string exn))
49+
Printf.eprintf "Error adding to cache: %s\n" (Printexc.to_string exn)
5850

5951
(* Cache AST file data with mutex protection *)
6052
let cache_ast_data ?(verbose = false) ?(skip_cache = false) ast_path data =
61-
if not skip_cache then (
62-
try
63-
Eio.Mutex.lock ast_cache_mutex;
64-
Hashtbl.replace ast_cache ast_path data;
65-
Eio.Mutex.unlock ast_cache_mutex
53+
if not skip_cache then
54+
try Hashtbl.replace ast_cache ast_path data
6655
with exn ->
67-
Eio.Mutex.unlock ast_cache_mutex;
6856
if verbose then
69-
Printf.eprintf "Error caching AST data: %s\n" (Printexc.to_string exn))
57+
Printf.eprintf "Error caching AST data: %s\n" (Printexc.to_string exn)
7058

7159
(* Get cached AST file data with mutex protection *)
7260
let get_ast_data ?(verbose = false) ast_path =
73-
try
74-
Eio.Mutex.lock ast_cache_mutex;
75-
let result = Hashtbl.find_opt ast_cache ast_path in
76-
Eio.Mutex.unlock ast_cache_mutex;
77-
result
61+
try let result = Hashtbl.find_opt ast_cache ast_path in
62+
63+
result
7864
with exn ->
79-
Eio.Mutex.unlock ast_cache_mutex;
8065
if verbose then
8166
Printf.eprintf "Error getting AST data: %s\n" (Printexc.to_string exn);
8267
None
8368

8469
(* Cache AST dependency check result with mutex protection *)
8570
let cache_ast_dependency_result ?(verbose = false) ?(skip_cache = false) key
8671
result =
87-
if not skip_cache then (
88-
try
89-
Eio.Mutex.lock ast_dependency_cache_mutex;
90-
Hashtbl.replace ast_dependency_cache key result;
91-
Eio.Mutex.unlock ast_dependency_cache_mutex
72+
if not skip_cache then
73+
try Hashtbl.replace ast_dependency_cache key result
9274
with exn ->
93-
Eio.Mutex.unlock ast_dependency_cache_mutex;
9475
if verbose then
9576
Printf.eprintf "Error caching AST dependency: %s\n"
96-
(Printexc.to_string exn))
77+
(Printexc.to_string exn)
9778

9879
(* Get cached AST dependency check result with mutex protection *)
9980
let get_ast_dependency_result ?(verbose = false) key =
10081
try
101-
Eio.Mutex.lock ast_dependency_cache_mutex;
10282
let result = Hashtbl.find_opt ast_dependency_cache key in
103-
Eio.Mutex.unlock ast_dependency_cache_mutex;
10483
result
10584
with exn ->
106-
Eio.Mutex.unlock ast_dependency_cache_mutex;
10785
if verbose then
10886
Printf.eprintf "Error getting AST dependency: %s\n"
10987
(Printexc.to_string exn);
@@ -119,7 +97,6 @@ module Cache = struct
11997
None)
12098
else
12199
try
122-
Eio.Mutex.lock cache_table_mutex;
123100
let result =
124101
match Hashtbl.find_opt cache_table path with
125102
| None ->
@@ -154,35 +131,19 @@ module Cache = struct
154131
Printf.printf "Cache invalid for %s (digest mismatch)\n" path;
155132
None)
156133
in
157-
Eio.Mutex.unlock cache_table_mutex;
158134
result
159135
with exn ->
160-
Eio.Mutex.unlock cache_table_mutex;
161136
if verbose then
162137
Printf.eprintf "Error finding in cache: %s\n" (Printexc.to_string exn);
163138
None
164139

165140
(* Clear the cache with mutex protection *)
166141
let clear ?(verbose = false) () =
167142
try
168-
(* Lock all mutexes to safely clear all caches *)
169-
Eio.Mutex.lock cache_table_mutex;
170-
Eio.Mutex.lock ast_cache_mutex;
171-
Eio.Mutex.lock ast_dependency_cache_mutex;
172-
173143
Hashtbl.clear cache_table;
174144
Hashtbl.clear ast_cache;
175-
Hashtbl.clear ast_dependency_cache;
176-
177-
(* Unlock in reverse order *)
178-
Eio.Mutex.unlock ast_dependency_cache_mutex;
179-
Eio.Mutex.unlock ast_cache_mutex;
180-
Eio.Mutex.unlock cache_table_mutex
145+
Hashtbl.clear ast_dependency_cache
181146
with exn ->
182-
(* Make sure to unlock in case of exceptions *)
183-
(try Eio.Mutex.unlock ast_dependency_cache_mutex with _ -> ());
184-
(try Eio.Mutex.unlock ast_cache_mutex with _ -> ());
185-
(try Eio.Mutex.unlock cache_table_mutex with _ -> ());
186147
if verbose then
187148
Printf.eprintf "Error clearing cache: %s\n" (Printexc.to_string exn)
188149
end

0 commit comments

Comments
 (0)