MCPcopy Create free account
hub / github.com/bytecodealliance/wasmtime / build_search_tree

Method build_search_tree

cranelift/frontend/src/switch.rs:103–151  ·  view source on GitHub ↗

Binary search for the right `ContiguousCaseRange`.

(
        bx: &mut FunctionBuilder,
        val: Value,
        otherwise: Block,
        contiguous_case_ranges: &'a [ContiguousCaseRange],
    )

Source from the content-addressed store, hash-verified

101
102 /// Binary search for the right `ContiguousCaseRange`.
103 fn build_search_tree<'a>(
104 bx: &mut FunctionBuilder,
105 val: Value,
106 otherwise: Block,
107 contiguous_case_ranges: &'a [ContiguousCaseRange],
108 ) {
109 // If no switch cases were added to begin with, we can just emit `jump otherwise`.
110 if contiguous_case_ranges.is_empty() {
111 bx.ins().jump(otherwise, &[]);
112 return;
113 }
114
115 // Avoid allocation in the common case
116 if contiguous_case_ranges.len() <= 3 {
117 Self::build_search_branches(bx, val, otherwise, contiguous_case_ranges);
118 return;
119 }
120
121 let mut stack = Vec::new();
122 stack.push((None, contiguous_case_ranges));
123
124 while let Some((block, contiguous_case_ranges)) = stack.pop() {
125 if let Some(block) = block {
126 bx.switch_to_block(block);
127 }
128
129 if contiguous_case_ranges.len() <= 3 {
130 Self::build_search_branches(bx, val, otherwise, contiguous_case_ranges);
131 } else {
132 let split_point = contiguous_case_ranges.len() / 2;
133 let (left, right) = contiguous_case_ranges.split_at(split_point);
134
135 let left_block = bx.create_block();
136 let right_block = bx.create_block();
137
138 let first_index = right[0].first_index;
139 let should_take_right_side =
140 icmp_imm_u128(bx, IntCC::UnsignedGreaterThanOrEqual, val, first_index);
141 bx.ins()
142 .brif(should_take_right_side, right_block, &[], left_block, &[]);
143
144 bx.seal_block(left_block);
145 bx.seal_block(right_block);
146
147 stack.push((Some(left_block), left));
148 stack.push((Some(right_block), right));
149 }
150 }
151 }
152
153 /// Linear search for the right `ContiguousCaseRange`.
154 fn build_search_branches<'a>(

Callers

nothing calls this directly

Calls 11

icmp_imm_u128Function · 0.85
jumpMethod · 0.80
switch_to_blockMethod · 0.80
create_blockMethod · 0.80
newFunction · 0.50
is_emptyMethod · 0.45
insMethod · 0.45
lenMethod · 0.45
pushMethod · 0.45
popMethod · 0.45
seal_blockMethod · 0.45

Tested by

no test coverage detected