11use std:: any:: Any ;
2+ use std:: cell:: Cell ;
23use std:: cmp:: Ordering ;
34use std:: marker:: PhantomData ;
45
@@ -26,10 +27,11 @@ where
2627{
2728 mutators : Vec < M > ,
2829 rng : fastrand:: Rng ,
29- search_space_complexity : f64 ,
3030 added_complexity : f64 ,
31- min_complexity : f64 ,
32- max_complexity : f64 ,
31+ initialized : Cell < bool > ,
32+ min_complexity : Cell < f64 > ,
33+ max_complexity : Cell < f64 > ,
34+ search_space_complexity : Cell < f64 > ,
3335 _phantom : PhantomData < T > ,
3436}
3537
@@ -41,57 +43,15 @@ where
4143 #[ no_coverage]
4244 pub fn new ( mutators : Vec < M > , added_complexity : f64 ) -> Self {
4345 assert ! ( !mutators. is_empty( ) ) ;
44- let complexity_from_choice = crate :: mutators:: size_to_cplxity ( mutators. len ( ) ) ;
45-
46- let search_space_complexity = mutators
47- . iter ( )
48- . map (
49- #[ no_coverage]
50- |m| {
51- let cplx = m. global_search_space_complexity ( ) ;
52- if cplx == 0. {
53- complexity_from_choice
54- } else {
55- cplx
56- }
57- } ,
58- )
59- . max_by (
60- #[ no_coverage]
61- |x, y| x. partial_cmp ( y) . unwrap_or ( Ordering :: Equal ) ,
62- )
63- . unwrap ( ) ;
64-
65- let max_complexity = mutators
66- . iter ( )
67- . map (
68- #[ no_coverage]
69- |m| m. max_complexity ( ) + added_complexity,
70- )
71- . max_by (
72- #[ no_coverage]
73- |x1, x2| x1. partial_cmp ( x2) . unwrap_or ( Ordering :: Equal ) ,
74- )
75- . unwrap ( ) ;
76- let min_complexity = mutators
77- . iter ( )
78- . map (
79- #[ no_coverage]
80- |m| m. min_complexity ( ) + added_complexity,
81- )
82- . min_by (
83- #[ no_coverage]
84- |x1, x2| x1. partial_cmp ( x2) . unwrap_or ( Ordering :: Equal ) ,
85- )
86- . unwrap ( ) ;
8746
8847 Self {
8948 mutators,
90- search_space_complexity,
9149 rng : fastrand:: Rng :: default ( ) ,
9250 added_complexity,
93- min_complexity,
94- max_complexity,
51+ initialized : Cell :: new ( false ) ,
52+ min_complexity : Cell :: new ( std:: f64:: INFINITY ) ,
53+ max_complexity : Cell :: default ( ) ,
54+ search_space_complexity : Cell :: default ( ) ,
9555 _phantom : PhantomData ,
9656 }
9757 }
@@ -152,6 +112,65 @@ where
152112 #[ doc( hidden) ]
153113 type UnmutateToken = UnmutateToken < T , M :: UnmutateToken > ;
154114
115+ #[ doc( hidden) ]
116+ #[ no_coverage]
117+ fn initialize ( & self ) {
118+ for mutator in self . mutators . iter ( ) {
119+ mutator. initialize ( ) ;
120+ }
121+
122+ let complexity_from_choice = crate :: mutators:: size_to_cplxity ( self . mutators . len ( ) ) ;
123+
124+ let search_space_complexity = self
125+ . mutators
126+ . iter ( )
127+ . map (
128+ #[ no_coverage]
129+ |m| {
130+ let cplx = m. global_search_space_complexity ( ) ;
131+ if cplx == 0. {
132+ complexity_from_choice
133+ } else {
134+ cplx
135+ }
136+ } ,
137+ )
138+ . max_by (
139+ #[ no_coverage]
140+ |x, y| x. partial_cmp ( y) . unwrap_or ( Ordering :: Equal ) ,
141+ )
142+ . unwrap ( ) ;
143+
144+ let max_complexity = self
145+ . mutators
146+ . iter ( )
147+ . map (
148+ #[ no_coverage]
149+ |m| m. max_complexity ( ) + self . added_complexity ,
150+ )
151+ . max_by (
152+ #[ no_coverage]
153+ |x1, x2| x1. partial_cmp ( x2) . unwrap_or ( Ordering :: Equal ) ,
154+ )
155+ . unwrap ( ) ;
156+ let min_complexity = self
157+ . mutators
158+ . iter ( )
159+ . map (
160+ #[ no_coverage]
161+ |m| m. min_complexity ( ) + self . added_complexity ,
162+ )
163+ . min_by (
164+ #[ no_coverage]
165+ |x1, x2| x1. partial_cmp ( x2) . unwrap_or ( Ordering :: Equal ) ,
166+ )
167+ . unwrap ( ) ;
168+ self . min_complexity . set ( min_complexity) ;
169+ self . max_complexity . set ( max_complexity) ;
170+ self . search_space_complexity . set ( search_space_complexity) ;
171+ self . initialized . set ( true ) ;
172+ }
173+
155174 #[ doc( hidden) ]
156175 #[ no_coverage]
157176 fn default_arbitrary_step ( & self ) -> Self :: ArbitraryStep {
@@ -226,19 +245,19 @@ where
226245 #[ doc( hidden) ]
227246 #[ no_coverage]
228247 fn global_search_space_complexity ( & self ) -> f64 {
229- self . search_space_complexity
248+ self . search_space_complexity . get ( )
230249 }
231250
232251 #[ doc( hidden) ]
233252 #[ no_coverage]
234253 fn max_complexity ( & self ) -> f64 {
235- self . max_complexity
254+ self . max_complexity . get ( )
236255 }
237256
238257 #[ doc( hidden) ]
239258 #[ no_coverage]
240259 fn min_complexity ( & self ) -> f64 {
241- self . min_complexity
260+ self . min_complexity . get ( )
242261 }
243262
244263 #[ doc( hidden) ]
0 commit comments