Skip to content

Commit d42a45b

Browse files
committed
list optims
1 parent 64ff54e commit d42a45b

File tree

1 file changed

+21
-26
lines changed

1 file changed

+21
-26
lines changed

src/tracedAtomic.ml

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -151,12 +151,6 @@ let spawn f =
151151
CCVector.push processes
152152
{ next_op = Start; next_repr = None; resume_func = fiber_f; finished = false }
153153

154-
let rec last_element l =
155-
match l with
156-
| h :: [] -> h
157-
| [] -> assert(false)
158-
| _ :: tl -> last_element tl
159-
160154
type proc_rec = { proc_id: int; op: atomic_op; obj_ptr : int option }
161155
type state_cell = { procs: proc_rec list; run_proc: int; run_op: atomic_op; run_ptr: int option; enabled : IntSet.t; mutable backtrack : IntSet.t }
162156

@@ -201,7 +195,7 @@ let do_run init_schedule =
201195
end
202196
in
203197
tracing := true;
204-
run_trace init_schedule ();
198+
run_trace (List.rev init_schedule) ();
205199
tracing := false;
206200
if !num_runs mod 1000 == 0 then
207201
Printf.printf "run: %d\n" !num_runs;
@@ -211,17 +205,16 @@ let do_run init_schedule =
211205
|> Seq.filter (fun (_,proc) -> not proc.finished)
212206
|> Seq.map (fun (id,_) -> id)
213207
|> IntSet.of_seq in
214-
match last_element init_schedule with
215-
| (run_proc, run_op, run_ptr) ->
216-
{ procs; enabled = current_enabled; run_proc; run_op; run_ptr; backtrack = IntSet.empty }
208+
let (run_proc, run_op, run_ptr) = List.hd init_schedule in
209+
{ procs; enabled = current_enabled; run_proc; run_op; run_ptr; backtrack = IntSet.empty }
217210

218-
let rec explore func state clock last_access =
219-
let s = last_element state in
211+
let rec explore func time state current_schedule clock last_access =
212+
let s = List.hd state in
220213
List.iter (fun proc ->
221214
let j = proc.proc_id in
222215
let i = Option.bind proc.obj_ptr (fun ptr -> IntMap.find_opt ptr last_access) |> Option.value ~default:0 in
223216
if i != 0 then begin
224-
let pre_s = List.nth state (i-1) in
217+
let pre_s = List.nth state (time - i + 1) in
225218
if IntSet.mem j pre_s.enabled then
226219
pre_s.backtrack <- IntSet.add j pre_s.backtrack
227220
else
@@ -237,8 +230,8 @@ let rec explore func state clock last_access =
237230
let j = IntSet.min_elt (IntSet.diff s.backtrack !dones) in
238231
dones := IntSet.add j !dones;
239232
let j_proc = List.nth s.procs j in
240-
let new_step = [(j, j_proc.op, j_proc.obj_ptr)] in
241-
let full_schedule = List.map (fun s -> (s.run_proc, s.run_op, s.run_ptr)) state @ new_step in
233+
let new_step = (j, j_proc.op, j_proc.obj_ptr) in
234+
let full_schedule = new_step :: current_schedule in
242235
let schedule =
243236
if !is_backtracking
244237
then begin
@@ -248,14 +241,16 @@ let rec explore func state clock last_access =
248241
else begin
249242
is_backtracking := true ;
250243
schedule_for_checks := full_schedule;
251-
new_step
244+
[new_step]
252245
end
253246
in
254-
let statedash = state @ [do_run schedule] in
255-
let state_time = (List.length statedash)-1 in
256-
let new_last_access = match j_proc.obj_ptr with Some(ptr) -> IntMap.add ptr state_time last_access | None -> last_access in
257-
let new_clock = IntMap.add j state_time clock in
258-
explore func statedash new_clock new_last_access
247+
let s = do_run schedule in
248+
let new_state = s :: state in
249+
let new_schedule = (s.run_proc, s.run_op, s.run_ptr) :: current_schedule in
250+
let new_time = time + 1 in
251+
let new_last_access = match j_proc.obj_ptr with Some(ptr) -> IntMap.add ptr new_time last_access | None -> last_access in
252+
let new_clock = IntMap.add j new_time clock in
253+
explore func new_time new_state new_schedule new_clock new_last_access
259254
done
260255
end
261256

@@ -277,17 +272,17 @@ let check f =
277272
Printf.printf "Process %d: %s %s\n" last_run_proc (atomic_op_str last_run_op) last_run_ptr
278273
end;
279274
end;
280-
) !schedule_for_checks;
275+
) (List.rev !schedule_for_checks) ;
281276
assert(false)
282277
end;
283278
tracing := tracing_at_start
284279

285280

286281
let trace func =
287-
let schedule = [(0, Start, None)] in
288-
setup_run func schedule ;
289-
let empty_state = do_run schedule :: [] in
282+
let empty_schedule = [(0, Start, None)] in
283+
setup_run func empty_schedule ;
284+
let empty_state = do_run empty_schedule :: [] in
290285
let empty_clock = IntMap.empty in
291286
let empty_last_access = IntMap.empty in
292-
explore func empty_state empty_clock empty_last_access ;
287+
explore func 1 empty_state empty_schedule empty_clock empty_last_access ;
293288
Printf.printf "Finished after %i runs.\n%!" !num_runs

0 commit comments

Comments
 (0)