Skip to content

Commit 5a31e0c

Browse files
Move DFIR singleton handling from RefCell to plain local variables
Replace `RefCell<T>` with plain `let mut` local variables for singleton state in DFIR operator codegen. Since subgraphs run sequentially in topological order and singleton references create ordering edges, there is never concurrent access — the RefCell runtime borrow checking was unnecessary overhead. Updated 16 files: process_singletons.rs, meta_graph.rs, 13 operator codegen files (fold, fold_keyed, fold_no_replay, reduce, reduce_keyed, reduce_no_replay, scan, scan_async_blocking, state_by, persist, prefix, repeat_n), lattice_bimorphism operator, and LatticeBimorphismPull runtime. Co-authored-by: Infinity 🤖 <infinity@hydro.run> PR: #2829
1 parent af90c89 commit 5a31e0c

16 files changed

Lines changed: 59 additions & 80 deletions

dfir_lang/src/graph/meta_graph.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -816,10 +816,9 @@ impl DfirGraph {
816816
/// instead of runtime handoffs. Each call to the closure runs one tick.
817817
///
818818
/// The generated code block evaluates to a `Dfir` instance wrapping the
819-
/// closure. Operator prologues (`add_state`, `set_state_lifespan_hook`)
820-
/// run at construction time on the `Context` before it is moved into
821-
/// `Dfir::new`. `Dfir` provides the `Context` to the closure on
822-
/// each tick run.
819+
/// closure. Operator prologues run at construction time before the `Context`
820+
/// is moved into `Dfir::new`. `Dfir` provides the `Context` to the closure
821+
/// on each tick run.
823822
///
824823
/// # Errors
825824
///
@@ -1144,7 +1143,6 @@ impl DfirGraph {
11441143
let arguments = &process_singletons::postprocess_singletons(
11451144
op_inst.arguments_raw.clone(),
11461145
singletons_resolved.clone(),
1147-
context,
11481146
);
11491147
let arguments_handles =
11501148
&process_singletons::postprocess_singletons_handles(

dfir_lang/src/graph/ops/fold.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,20 +82,20 @@ pub const FOLD: OperatorConstraints = OperatorConstraints {
8282
let mut #initializer_func_ident = #init_fn;
8383

8484
#[allow(clippy::redundant_closure_call)]
85-
let #singleton_output_ident = ::std::cell::RefCell::new(#init);
85+
let mut #singleton_output_ident = #init;
8686
};
8787

8888
let write_tick_end = match persistence {
8989
Persistence::Tick => quote_spanned! {op_span=>
9090
#[allow(clippy::redundant_closure_call)]
91-
#singleton_output_ident.replace(#init);
91+
{ #singleton_output_ident = #init; }
9292
},
9393
_ => Default::default(),
9494
};
9595

9696
let assign_accum_ident = quote_spanned! {op_span=>
9797
#[allow(unused_mut)]
98-
let mut #accumulator_ident = #singleton_output_ident.borrow_mut();
98+
let mut #accumulator_ident = &mut #singleton_output_ident;
9999
};
100100
let foreach_body = quote_spanned! {op_span=>
101101
#[inline(always)]

dfir_lang/src/graph/ops/fold_keyed.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,18 +131,18 @@ pub const FOLD_KEYED: OperatorConstraints = OperatorConstraints {
131131
let hashtable_ident = wc.make_ident("hashtable");
132132

133133
let write_prologue = quote_spanned! {op_span=>
134-
let #singleton_output_ident = ::std::cell::RefCell::new(#root::rustc_hash::FxHashMap::<#( #generic_type_args ),*>::default());
134+
let mut #singleton_output_ident = #root::rustc_hash::FxHashMap::<#( #generic_type_args ),*>::default();
135135
};
136136

137137
let write_tick_end = match persistence {
138138
Persistence::Tick => quote_spanned! {op_span=>
139-
#singleton_output_ident.borrow_mut().clear();
139+
#singleton_output_ident.clear();
140140
},
141141
_ => Default::default(),
142142
};
143143

144144
let assign_hashtable_ident = quote_spanned! {op_span=>
145-
let mut #hashtable_ident = #singleton_output_ident.borrow_mut();
145+
let mut #hashtable_ident = &mut #singleton_output_ident;
146146
};
147147

148148
let write_iterator = if Persistence::Mutable == persistence {

dfir_lang/src/graph/ops/fold_no_replay.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,20 +64,20 @@ pub const FOLD_NO_REPLAY: OperatorConstraints = OperatorConstraints {
6464
let mut #initializer_func_ident = #init_fn;
6565

6666
#[allow(clippy::redundant_closure_call)]
67-
let #singleton_output_ident = ::std::cell::RefCell::new(#init);
67+
let mut #singleton_output_ident = #init;
6868
};
6969

7070
let write_tick_end = match persistence {
7171
Persistence::Tick => quote_spanned! {op_span=>
7272
#[allow(clippy::redundant_closure_call)]
73-
#singleton_output_ident.replace(#init);
73+
{ #singleton_output_ident = #init; }
7474
},
7575
_ => Default::default(),
7676
};
7777

7878
let assign_accum_ident = quote_spanned! {op_span=>
7979
#[allow(unused_mut)]
80-
let mut #accumulator_ident = #singleton_output_ident.borrow_mut();
80+
let mut #accumulator_ident = &mut #singleton_output_ident;
8181
};
8282
let foreach_body = quote_spanned! {op_span=>
8383
#[inline(always)]

dfir_lang/src/graph/ops/lattice_bimorphism.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@ pub const LATTICE_BIMORPHISM: OperatorConstraints = OperatorConstraints {
8686
lhs_pull: LhsPull,
8787
rhs_pull: RhsPull,
8888
func: Func,
89-
lhs_state: &'a ::std::cell::RefCell<LhsState>,
90-
rhs_state: &'a ::std::cell::RefCell<RhsState>,
89+
lhs_state: &'a LhsState,
90+
rhs_state: &'a RhsState,
9191
) -> impl #root::dfir_pipes::pull::Pull<
9292
Item = Output,
9393
Meta = (),

dfir_lang/src/graph/ops/persist.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,15 +90,13 @@ pub const PERSIST: OperatorConstraints = OperatorConstraints {
9090
let persistdata_ident = singleton_output_ident;
9191
let vec_ident = wc.make_ident("persistvec");
9292
let write_prologue = quote_spanned! {op_span=>
93-
let #persistdata_ident = ::std::cell::RefCell::new(
94-
::std::vec::Vec::<#generic_type>::new(),
95-
);
93+
let mut #persistdata_ident = ::std::vec::Vec::<#generic_type>::new();
9694
};
9795

9896
let write_iterator = if is_pull {
9997
let input = &inputs[0];
10098
quote_spanned! {op_span=>
101-
let mut #vec_ident = #persistdata_ident.borrow_mut();
99+
let #vec_ident = &mut #persistdata_ident;
102100

103101
let #ident = {
104102
let replay_idx = if #context.is_first_run_this_tick() {
@@ -119,7 +117,7 @@ pub const PERSIST: OperatorConstraints = OperatorConstraints {
119117
} else {
120118
let output = &outputs[0];
121119
quote_spanned! {op_span=>
122-
let mut #vec_ident = #persistdata_ident.borrow_mut();
120+
let #vec_ident = &mut #persistdata_ident;
123121

124122
let #ident = {
125123
fn constrain_types<'ctx, Psh, Item>(vec: &'ctx mut Vec<Item>, output: Psh, is_new_tick: bool) -> impl 'ctx + #root::dfir_pipes::push::Push<Item, ()>

dfir_lang/src/graph/ops/prefix.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,14 @@ pub const PREFIX: OperatorConstraints = OperatorConstraints {
4040

4141
let write_prologue = quote_spanned! {op_span=>
4242
#[allow(clippy::redundant_closure_call)]
43-
let #singleton_output_ident = ::std::cell::RefCell::new(::std::vec::Vec::new());
43+
let mut #singleton_output_ident = ::std::vec::Vec::new();
4444
};
4545

4646
let vec_ident = wc.make_ident("vec");
4747

4848
let input = &inputs[0];
4949
let write_iterator = quote_spanned! {op_span=>
50-
let mut #vec_ident = #singleton_output_ident.borrow_mut();
50+
let #vec_ident = &mut #singleton_output_ident;
5151

5252
let #ident = {
5353
let fut = #root::dfir_pipes::pull::Pull::for_each(#input, |item| {

dfir_lang/src/graph/ops/reduce.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,12 @@ pub const REDUCE: OperatorConstraints = OperatorConstraints {
6363
let [persistence] = wc.persistence_args_disallow_mutable(diagnostics);
6464

6565
let write_prologue = quote_spanned! {op_span=>
66-
let #singleton_output_ident = ::std::cell::RefCell::new(::std::option::Option::None);
66+
let mut #singleton_output_ident = ::std::option::Option::None;
6767
};
6868

6969
let write_tick_end = match persistence {
7070
Persistence::Tick => quote_spanned! {op_span=>
71-
#singleton_output_ident.replace(::std::option::Option::None);
71+
#singleton_output_ident = ::std::option::Option::None;
7272
},
7373
_ => Default::default(),
7474
};
@@ -95,7 +95,7 @@ pub const REDUCE: OperatorConstraints = OperatorConstraints {
9595

9696
let assign_accum_ident = quote_spanned! {op_span=>
9797
#[allow(unused_mut)]
98-
let mut #accumulator_ident = #singleton_output_ident.borrow_mut();
98+
let mut #accumulator_ident = &mut #singleton_output_ident;
9999
};
100100

101101
let write_iterator = if is_pull {

dfir_lang/src/graph/ops/reduce_keyed.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,12 +110,12 @@ pub const REDUCE_KEYED: OperatorConstraints = OperatorConstraints {
110110
let hashtable_ident = wc.make_ident("hashtable");
111111

112112
let write_prologue = quote_spanned! {op_span=>
113-
let #singleton_output_ident = ::std::cell::RefCell::new(#root::rustc_hash::FxHashMap::<#( #generic_type_args ),*>::default());
113+
let mut #singleton_output_ident = #root::rustc_hash::FxHashMap::<#( #generic_type_args ),*>::default();
114114
};
115115

116116
let write_tick_end = match persistence {
117117
Persistence::Tick => quote_spanned! {op_span=>
118-
#singleton_output_ident.borrow_mut().clear();
118+
#singleton_output_ident.clear();
119119
},
120120
_ => Default::default(),
121121
};
@@ -154,7 +154,7 @@ pub const REDUCE_KEYED: OperatorConstraints = OperatorConstraints {
154154
};
155155

156156
quote_spanned! {op_span=>
157-
let mut #hashtable_ident = #singleton_output_ident.borrow_mut();
157+
let mut #hashtable_ident = &mut #singleton_output_ident;
158158

159159
{
160160
#[inline(always)]

dfir_lang/src/graph/ops/reduce_no_replay.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,12 @@ pub const REDUCE_NO_REPLAY: OperatorConstraints = OperatorConstraints {
4646
let [persistence] = wc.persistence_args_disallow_mutable(diagnostics);
4747

4848
let write_prologue = quote_spanned! {op_span=>
49-
let #singleton_output_ident = ::std::cell::RefCell::new(::std::option::Option::None);
49+
let mut #singleton_output_ident = ::std::option::Option::None;
5050
};
5151

5252
let write_tick_end = match persistence {
5353
Persistence::Tick => quote_spanned! {op_span=>
54-
#singleton_output_ident.replace(::std::option::Option::None);
54+
#singleton_output_ident = ::std::option::Option::None;
5555
},
5656
_ => Default::default(),
5757
};
@@ -78,7 +78,7 @@ pub const REDUCE_NO_REPLAY: OperatorConstraints = OperatorConstraints {
7878

7979
let assign_accum_ident = quote_spanned! {op_span=>
8080
#[allow(unused_mut)]
81-
let mut #accumulator_ident = #singleton_output_ident.borrow_mut();
81+
let mut #accumulator_ident = &mut #singleton_output_ident;
8282
};
8383

8484
let write_iterator = if is_pull {

0 commit comments

Comments
 (0)