@@ -163,21 +163,6 @@ impl InjectedImports {
163
163
164
164
// Gets the cost of an instruction.
165
165
pub fn instruction_to_cost ( i : & Operator ) -> u64 {
166
- match i {
167
- // The following instructions are mostly signaling the start/end of code blocks,
168
- // so we assign 0 cost to them.
169
- Operator :: Block { .. } => 0 ,
170
- Operator :: Else => 0 ,
171
- Operator :: End => 0 ,
172
- Operator :: Loop { .. } => 0 ,
173
-
174
- // Default cost of an instruction is 1.
175
- _ => 1 ,
176
- }
177
- }
178
-
179
- // Gets the cost of an instruction.
180
- pub fn instruction_to_cost_new ( i : & Operator ) -> u64 {
181
166
// This aims to be a complete list of all instructions that can be executed, with certain exceptions.
182
167
// The exceptions are: SIMD instructions, atomic instructions, and the dynamic cost of
183
168
// of operations such as table/memory fill, copy, init. This
@@ -788,7 +773,6 @@ pub(super) fn instrument(
788
773
special_indices,
789
774
subnet_type,
790
775
dirty_page_overhead,
791
- metering_type,
792
776
)
793
777
}
794
778
@@ -872,7 +856,6 @@ fn replace_system_api_functions(
872
856
special_indices : SpecialIndices ,
873
857
subnet_type : SubnetType ,
874
858
dirty_page_overhead : NumInstructions ,
875
- metering_type : MeteringType ,
876
859
) {
877
860
let api_indexes = calculate_api_indexes ( module) ;
878
861
let number_of_func_imports = module
@@ -884,12 +867,9 @@ fn replace_system_api_functions(
884
867
// Collect a single map of all the function indexes that need to be
885
868
// replaced.
886
869
let mut func_index_replacements = BTreeMap :: new ( ) ;
887
- for ( api, ( ty, body) ) in replacement_functions (
888
- special_indices,
889
- subnet_type,
890
- dirty_page_overhead,
891
- metering_type,
892
- ) {
870
+ for ( api, ( ty, body) ) in
871
+ replacement_functions ( special_indices, subnet_type, dirty_page_overhead)
872
+ {
893
873
if let Some ( old_index) = api_indexes. get ( & api) {
894
874
let type_idx = add_func_type ( module, ty) ;
895
875
let new_index = ( number_of_func_imports + module. functions . len ( ) ) as u32 ;
@@ -1198,9 +1178,8 @@ fn inject_metering(
1198
1178
metering_type : MeteringType ,
1199
1179
) {
1200
1180
let points = match metering_type {
1201
- MeteringType :: Old => injections_old ( code) ,
1202
1181
MeteringType :: None => Vec :: new ( ) ,
1203
- MeteringType :: New => injections_new ( code) ,
1182
+ MeteringType :: New => injections ( code) ,
1204
1183
} ;
1205
1184
let points = points. iter ( ) . filter ( |point| match point. cost_detail {
1206
1185
InjectionPointCostDetail :: StaticCost {
@@ -1531,79 +1510,20 @@ fn inject_try_grow_wasm_memory(
1531
1510
}
1532
1511
}
1533
1512
1534
- // This function scans through the Wasm code and creates an injection point
1535
- // at the beginning of every basic block (straight-line sequence of instructions
1536
- // with no branches) and before each bulk memory instruction. An injection point
1537
- // contains a "hint" about the context of every basic block, specifically if
1538
- // it's re-entrant or not. This version over-estimates the cost of code with
1539
- // returns and jumps.
1540
- fn injections_old ( code : & [ Operator ] ) -> Vec < InjectionPoint > {
1541
- let mut res = Vec :: new ( ) ;
1542
- let mut stack = Vec :: new ( ) ;
1543
- use Operator :: * ;
1544
- // The function itself is a re-entrant code block.
1545
- let mut curr = InjectionPoint :: new_static_cost ( 0 , Scope :: ReentrantBlockStart , 0 ) ;
1546
- for ( position, i) in code. iter ( ) . enumerate ( ) {
1547
- curr. cost_detail . increment_cost ( instruction_to_cost ( i) ) ;
1548
- match i {
1549
- // Start of a re-entrant code block.
1550
- Loop { .. } => {
1551
- stack. push ( curr) ;
1552
- curr = InjectionPoint :: new_static_cost ( position + 1 , Scope :: ReentrantBlockStart , 0 ) ;
1553
- }
1554
- // Start of a non re-entrant code block.
1555
- If { .. } | Block { .. } => {
1556
- stack. push ( curr) ;
1557
- curr =
1558
- InjectionPoint :: new_static_cost ( position + 1 , Scope :: NonReentrantBlockStart , 0 ) ;
1559
- }
1560
- // End of a code block but still more code left.
1561
- Else | Br { .. } | BrIf { .. } | BrTable { .. } => {
1562
- res. push ( curr) ;
1563
- curr = InjectionPoint :: new_static_cost ( position + 1 , Scope :: BlockEnd , 0 ) ;
1564
- }
1565
- // `End` signals the end of a code block. If there's nothing more on the stack, we've
1566
- // gone through all the code.
1567
- End => {
1568
- res. push ( curr) ;
1569
- curr = match stack. pop ( ) {
1570
- Some ( val) => val,
1571
- None => break ,
1572
- } ;
1573
- }
1574
- // Bulk memory instructions require injected metering __before__ the instruction
1575
- // executes so that size arguments can be read from the stack at runtime.
1576
- MemoryFill { .. }
1577
- | MemoryCopy { .. }
1578
- | MemoryInit { .. }
1579
- | TableCopy { .. }
1580
- | TableInit { .. }
1581
- | TableFill { .. } => {
1582
- res. push ( InjectionPoint :: new_dynamic_cost ( position) ) ;
1583
- }
1584
- // Nothing special to be done for other instructions.
1585
- _ => ( ) ,
1586
- }
1587
- }
1588
-
1589
- res. sort_by_key ( |k| k. position ) ;
1590
- res
1591
- }
1592
-
1593
1513
// This function scans through the Wasm code and creates an injection point
1594
1514
// at the beginning of every basic block (straight-line sequence of instructions
1595
1515
// with no branches) and before each bulk memory instruction. An injection point
1596
1516
// contains a "hint" about the context of every basic block, specifically if
1597
1517
// it's re-entrant or not.
1598
- fn injections_new ( code : & [ Operator ] ) -> Vec < InjectionPoint > {
1518
+ fn injections ( code : & [ Operator ] ) -> Vec < InjectionPoint > {
1599
1519
let mut res = Vec :: new ( ) ;
1600
1520
use Operator :: * ;
1601
1521
// The function itself is a re-entrant code block.
1602
1522
// Start with at least one fuel being consumed because even empty
1603
1523
// functions should consume at least some fuel.
1604
1524
let mut curr = InjectionPoint :: new_static_cost ( 0 , Scope :: ReentrantBlockStart , 1 ) ;
1605
1525
for ( position, i) in code. iter ( ) . enumerate ( ) {
1606
- curr. cost_detail . increment_cost ( instruction_to_cost_new ( i) ) ;
1526
+ curr. cost_detail . increment_cost ( instruction_to_cost ( i) ) ;
1607
1527
match i {
1608
1528
// Start of a re-entrant code block.
1609
1529
Loop { .. } => {
0 commit comments