Skip to content

Commit ef03822

Browse files
committed
Cleanup
1 parent 940a904 commit ef03822

File tree

1 file changed

+24
-40
lines changed

1 file changed

+24
-40
lines changed

src/chunker.rs

Lines changed: 24 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -125,12 +125,8 @@ impl Chunker {
125125
stack_analyzer.analyze_blocks(chunk)
126126
}
127127

128-
pub fn undo(
129-
&mut self,
130-
mut num_unclosed_ifs: i32, //TODO: We should be able to use undo_info.num_unclosed_ifs
131-
mut undo_info: UndoInfo,
132-
) -> (Vec<Box<StructuredScript>>, usize) {
133-
if num_unclosed_ifs == 0 {
128+
pub fn undo(&mut self, mut undo_info: UndoInfo) -> (Vec<Box<StructuredScript>>, usize) {
129+
if undo_info.num_unclosed_ifs == 0 {
134130
return (vec![], 0);
135131
}
136132

@@ -140,14 +136,14 @@ impl Chunker {
140136
loop {
141137
let builder = match undo_info.call_stack.pop() {
142138
Some(builder) => builder,
143-
None => panic!("num_unclosed_ifs != 0 but the undo_info call_stack is empty"), // the last block in the call stack
139+
None => panic!("Not all OP_IF or OP_NOTIF are closed in the chunk but undoing/removing scripts from the end of the chunk violates the set tolerance. Number of unmatched OP_IF/OP_NOTIF: {}", undo_info.num_unclosed_ifs), // the last block in the call stack
144140
};
145141
if builder.contains_flow_op() {
146142
if builder.is_script_buf() && builder.len() == 1 {
147-
num_unclosed_ifs -= builder.num_unclosed_ifs();
143+
undo_info.num_unclosed_ifs -= builder.num_unclosed_ifs();
148144
removed_len += builder.len();
149145
removed_scripts.push(builder);
150-
if num_unclosed_ifs == 0 {
146+
if undo_info.num_unclosed_ifs == 0 {
151147
break;
152148
}
153149
} else {
@@ -158,7 +154,6 @@ impl Chunker {
158154
undo_info.call_stack.push(Box::new(sub_builder.clone()));
159155
}
160156
Block::Script(script_buf) => {
161-
//TODO: Can we avoid cloning or creating a builder here?
162157
// Split the script_buf at OP_IF/OP_NOTIF and OP_ENDIF
163158
let mut tmp_script = ScriptBuf::new();
164159
for instruction_res in script_buf.instructions() {
@@ -181,32 +176,29 @@ impl Chunker {
181176
}
182177
}
183178
if !tmp_script.is_empty() {
184-
undo_info.call_stack.push(Box::new(
185-
StructuredScript::new("")
186-
.push_script(tmp_script),
187-
));
179+
undo_info.call_stack.push(Box::new(
180+
StructuredScript::new("").push_script(tmp_script),
181+
));
188182
}
189183
}
190184
}
191185
}
192186
}
193187
} else {
194-
// No OP_IF, OP_NOTIF or OP_ENDIF in that structured script so just remove it
188+
// No OP_IF, OP_NOTIF or OP_ENDIF in that structured script so we will not include
189+
// it in the chunk.
195190
removed_len += builder.len();
196191
removed_scripts.push(builder);
197192
}
198193
}
199194

200195
self.call_stack.extend(removed_scripts);
201-
assert!(num_unclosed_ifs >= 0, "More OP_ENDIF's than OP_IF's after undo step. (This means there is a bug in the undo logic.)");
202-
assert_eq!(num_unclosed_ifs, 0, "Unable to make up for the OP_IF's in this chunk. Consider a larger target size or more tolerance. Unclosed OP_IF's: {:?}, removed_len: {}, undo.call_stack: {:?}, chunks: {:?}", num_unclosed_ifs, removed_len, undo_info.call_stack, self.chunks.iter().map(|chunk| chunk.size).collect::<Vec<_>>());
203196
(undo_info.call_stack, removed_len)
204197
}
205198

206199
fn find_next_chunk(&mut self) -> Chunk {
207200
let mut chunk_scripts = vec![];
208201
let mut chunk_len = 0;
209-
let mut num_unclosed_ifs = 0;
210202

211203
// All not selected StructuredScripts that have to be added to the call_stack again
212204
let mut undo_info = UndoInfo::new();
@@ -221,42 +213,35 @@ impl Chunker {
221213
};
222214

223215
assert!(
224-
num_unclosed_ifs + builder.num_unclosed_ifs() >= 0,
216+
undo_info.num_unclosed_ifs + builder.num_unclosed_ifs() >= 0,
225217
"More OP_ENDIF's than OP_IF's in the script. num_unclosed_if: {:?}, builder: {:?}",
226-
num_unclosed_ifs,
218+
undo_info.num_unclosed_ifs,
227219
builder.num_unclosed_ifs()
228220
);
229221

230222
// TODO: Use stack analysis to find best possible chunk border
231223
let block_len = builder.len();
232-
if chunk_len + block_len < self.target_chunk_size - self.tolerance {
233-
// Case 1: Builder is too small. target - tolerance not yet reached with it.
234-
num_unclosed_ifs += builder.num_unclosed_ifs();
235-
chunk_scripts.push(Box::new(builder));
236-
chunk_len += block_len;
237-
} else if chunk_len + block_len <= self.target_chunk_size {
238-
// Case 2: Adding the current builder remains a valid solution.
224+
if chunk_len + block_len <= self.target_chunk_size {
225+
// Adding the current builder remains a valid solution.
239226
// TODO: Check with stack analyzer to see if adding the builder is better or not.
240-
num_unclosed_ifs += builder.num_unclosed_ifs();
227+
// Consider the tolerance for that.
241228
chunk_len += block_len;
242-
if num_unclosed_ifs == 0 {
243-
// We are going to keep this structured script in the chunk
244-
// Reset the undo information
229+
if undo_info.num_unclosed_ifs + builder.num_unclosed_ifs() == 0 {
230+
// We will keep this structured script in the chunk.
231+
// Reset the undo information.
245232
chunk_scripts.extend(undo_info.reset());
246233
chunk_scripts.push(Box::new(builder));
247234
} else {
248-
// Update the undo information in case we need to remove this StructuredScript
249-
// from the chunk again
235+
// Update the undo information as we need to remove this StructuredScript
236+
// from the chunk if the if's are not closed in it eventually.
250237
undo_info.update(builder);
251238
}
252239
// Reset the depth parameter
253240
depth = 0;
254241
} else if chunk_len + block_len > self.target_chunk_size
255-
&& (chunk_len < self.target_chunk_size - self.tolerance
256-
|| chunk_len == 0
257-
|| (chunk_len < self.target_chunk_size && depth <= max_depth))
242+
&& (chunk_len < self.target_chunk_size && depth <= max_depth)
258243
{
259-
// Case 3: Current builder too large and there is no acceptable solution yet
244+
// Current builder too large and there is no acceptable solution yet
260245
// Even if we have an acceptable solution we check if there is a better one in next depth calls
261246
// Chunk inside a call of the current builder.
262247
// Add all its calls to the call_stack.
@@ -275,7 +260,6 @@ impl Chunker {
275260
contains_call = true;
276261
}
277262
Block::Script(script_buf) => {
278-
//TODO: Can we avoid cloning or creating a builder here?
279263
self.call_stack.push(Box::new(
280264
StructuredScript::new("").push_script(script_buf.clone()),
281265
));
@@ -294,8 +278,8 @@ impl Chunker {
294278
}
295279
}
296280

297-
// Undo the lately added scripts until the num_unclosed_ifs is 0.
298-
let undo_result = self.undo(num_unclosed_ifs, undo_info);
281+
// Remove scripts from the end of the chunk until all if's are closed.
282+
let undo_result = self.undo(undo_info);
299283
chunk_scripts.extend(undo_result.0);
300284
chunk_len -= undo_result.1;
301285

0 commit comments

Comments
 (0)