Skip to content

Commit 6b65f87

Browse files
committed
add back signatures, even if empty, flatten multiple selectors per feedback #9216 (comment)
1 parent 9501589 commit 6b65f87

File tree

2 files changed

+52
-72
lines changed

2 files changed

+52
-72
lines changed

crates/forge/src/gas_report.rs

+14-32
Original file line numberDiff line numberDiff line change
@@ -181,17 +181,6 @@ impl Display for GasReport {
181181

182182
impl GasReport {
183183
fn format_json_output(&self) -> String {
184-
#[inline]
185-
fn format_gas_info(gas_info: &GasInfo) -> serde_json::Value {
186-
json!({
187-
"calls": gas_info.calls,
188-
"min": gas_info.min,
189-
"mean": gas_info.mean,
190-
"median": gas_info.median,
191-
"max": gas_info.max,
192-
})
193-
}
194-
195184
serde_json::to_string(
196185
&self
197186
.contracts
@@ -205,27 +194,20 @@ impl GasReport {
205194
let functions = contract
206195
.functions
207196
.iter()
208-
.map(|(fname, sigs)| {
209-
// If there is only one signature, display the gas info directly.
210-
let function_value = if sigs.len() == 1 {
211-
format_gas_info(sigs.values().next().unwrap())
212-
} else {
213-
// If there are multiple signatures, e.g. overloads like:
214-
// - `foo(uint256)`
215-
// - `foo(int256)`
216-
// display the gas info as a map with the signature as the key.
217-
let signatures = sigs
218-
.iter()
219-
.map(|(sig, gas_info)| {
220-
let display_name = sig.replace(':', "");
221-
(display_name, format_gas_info(gas_info))
222-
})
223-
.collect::<BTreeMap<_, _>>();
224-
225-
json!(signatures)
226-
};
227-
228-
(fname.to_string(), function_value)
197+
.flat_map(|(_, sigs)| {
198+
sigs.iter().map(|(sig, gas_info)| {
199+
let display_name = sig.replace(':', "");
200+
(
201+
display_name,
202+
json!({
203+
"calls": gas_info.calls,
204+
"min": gas_info.min,
205+
"mean": gas_info.mean,
206+
"median": gas_info.median,
207+
"max": gas_info.max,
208+
}),
209+
)
210+
})
229211
})
230212
.collect::<BTreeMap<_, _>>();
231213

crates/forge/tests/cli/cmd.rs

+38-40
Original file line numberDiff line numberDiff line change
@@ -1601,7 +1601,7 @@ forgetest!(gas_report_all_contracts, |prj, cmd| {
16011601
"size": 255
16021602
},
16031603
"functions": {
1604-
"foo": {
1604+
"foo()": {
16051605
"calls": 1,
16061606
"min": 45387,
16071607
"mean": 45387,
@@ -1617,7 +1617,7 @@ forgetest!(gas_report_all_contracts, |prj, cmd| {
16171617
"size": 256
16181618
},
16191619
"functions": {
1620-
"baz": {
1620+
"baz()": {
16211621
"calls": 1,
16221622
"min": 260712,
16231623
"mean": 260712,
@@ -1633,7 +1633,7 @@ forgetest!(gas_report_all_contracts, |prj, cmd| {
16331633
"size": 255
16341634
},
16351635
"functions": {
1636-
"bar": {
1636+
"bar()": {
16371637
"calls": 1,
16381638
"min": 64984,
16391639
"mean": 64984,
@@ -1685,7 +1685,7 @@ forgetest!(gas_report_all_contracts, |prj, cmd| {
16851685
"size": 255
16861686
},
16871687
"functions": {
1688-
"foo": {
1688+
"foo()": {
16891689
"calls": 1,
16901690
"min": 45387,
16911691
"mean": 45387,
@@ -1701,7 +1701,7 @@ forgetest!(gas_report_all_contracts, |prj, cmd| {
17011701
"size": 256
17021702
},
17031703
"functions": {
1704-
"baz": {
1704+
"baz()": {
17051705
"calls": 1,
17061706
"min": 260712,
17071707
"mean": 260712,
@@ -1717,7 +1717,7 @@ forgetest!(gas_report_all_contracts, |prj, cmd| {
17171717
"size": 255
17181718
},
17191719
"functions": {
1720-
"bar": {
1720+
"bar()": {
17211721
"calls": 1,
17221722
"min": 64984,
17231723
"mean": 64984,
@@ -1769,7 +1769,7 @@ forgetest!(gas_report_all_contracts, |prj, cmd| {
17691769
"size": 255
17701770
},
17711771
"functions": {
1772-
"foo": {
1772+
"foo()": {
17731773
"calls": 1,
17741774
"min": 45387,
17751775
"mean": 45387,
@@ -1785,7 +1785,7 @@ forgetest!(gas_report_all_contracts, |prj, cmd| {
17851785
"size": 256
17861786
},
17871787
"functions": {
1788-
"baz": {
1788+
"baz()": {
17891789
"calls": 1,
17901790
"min": 260712,
17911791
"mean": 260712,
@@ -1801,7 +1801,7 @@ forgetest!(gas_report_all_contracts, |prj, cmd| {
18011801
"size": 255
18021802
},
18031803
"functions": {
1804-
"bar": {
1804+
"bar()": {
18051805
"calls": 1,
18061806
"min": 64984,
18071807
"mean": 64984,
@@ -1860,7 +1860,7 @@ forgetest!(gas_report_all_contracts, |prj, cmd| {
18601860
"size": 255
18611861
},
18621862
"functions": {
1863-
"foo": {
1863+
"foo()": {
18641864
"calls": 1,
18651865
"min": 45387,
18661866
"mean": 45387,
@@ -1876,7 +1876,7 @@ forgetest!(gas_report_all_contracts, |prj, cmd| {
18761876
"size": 256
18771877
},
18781878
"functions": {
1879-
"baz": {
1879+
"baz()": {
18801880
"calls": 1,
18811881
"min": 260712,
18821882
"mean": 260712,
@@ -1892,7 +1892,7 @@ forgetest!(gas_report_all_contracts, |prj, cmd| {
18921892
"size": 255
18931893
},
18941894
"functions": {
1895-
"bar": {
1895+
"bar()": {
18961896
"calls": 1,
18971897
"min": 64984,
18981898
"mean": 64984,
@@ -1935,7 +1935,7 @@ forgetest!(gas_report_some_contracts, |prj, cmd| {
19351935
"size": 255
19361936
},
19371937
"functions": {
1938-
"foo": {
1938+
"foo()": {
19391939
"calls": 1,
19401940
"min": 45387,
19411941
"mean": 45387,
@@ -1973,7 +1973,7 @@ forgetest!(gas_report_some_contracts, |prj, cmd| {
19731973
"size": 255
19741974
},
19751975
"functions": {
1976-
"bar": {
1976+
"bar()": {
19771977
"calls": 1,
19781978
"min": 64984,
19791979
"mean": 64984,
@@ -2014,7 +2014,7 @@ forgetest!(gas_report_some_contracts, |prj, cmd| {
20142014
"size": 256
20152015
},
20162016
"functions": {
2017-
"baz": {
2017+
"baz()": {
20182018
"calls": 1,
20192019
"min": 260712,
20202020
"mean": 260712,
@@ -2069,7 +2069,7 @@ forgetest!(gas_report_ignore_some_contracts, |prj, cmd| {
20692069
"size": 256
20702070
},
20712071
"functions": {
2072-
"baz": {
2072+
"baz()": {
20732073
"calls": 1,
20742074
"min": 260712,
20752075
"mean": 260712,
@@ -2085,7 +2085,7 @@ forgetest!(gas_report_ignore_some_contracts, |prj, cmd| {
20852085
"size": 255
20862086
},
20872087
"functions": {
2088-
"bar": {
2088+
"bar()": {
20892089
"calls": 1,
20902090
"min": 64984,
20912091
"mean": 64984,
@@ -2136,7 +2136,7 @@ forgetest!(gas_report_ignore_some_contracts, |prj, cmd| {
21362136
"size": 255
21372137
},
21382138
"functions": {
2139-
"foo": {
2139+
"foo()": {
21402140
"calls": 1,
21412141
"min": 45387,
21422142
"mean": 45387,
@@ -2152,7 +2152,7 @@ forgetest!(gas_report_ignore_some_contracts, |prj, cmd| {
21522152
"size": 256
21532153
},
21542154
"functions": {
2155-
"baz": {
2155+
"baz()": {
21562156
"calls": 1,
21572157
"min": 260712,
21582158
"mean": 260712,
@@ -2231,7 +2231,7 @@ warning: ContractThree is listed in both 'gas_reports' and 'gas_reports_ignore'.
22312231
"size": 255
22322232
},
22332233
"functions": {
2234-
"foo": {
2234+
"foo()": {
22352235
"calls": 1,
22362236
"min": 45387,
22372237
"mean": 45387,
@@ -2247,7 +2247,7 @@ warning: ContractThree is listed in both 'gas_reports' and 'gas_reports_ignore'.
22472247
"size": 256
22482248
},
22492249
"functions": {
2250-
"baz": {
2250+
"baz()": {
22512251
"calls": 1,
22522252
"min": 260712,
22532253
"mean": 260712,
@@ -2263,7 +2263,7 @@ warning: ContractThree is listed in both 'gas_reports' and 'gas_reports_ignore'.
22632263
"size": 255
22642264
},
22652265
"functions": {
2266-
"bar": {
2266+
"bar()": {
22672267
"calls": 1,
22682268
"min": 64984,
22692269
"mean": 64984,
@@ -2283,7 +2283,7 @@ warning: ContractThree is listed in both 'gas_reports' and 'gas_reports_ignore'.
22832283
"#]]);
22842284
});
22852285

2286-
forgetest!(gas_report_multiple_selectors, |prj, cmd| {
2286+
forgetest!(gas_report_flatten_multiple_selectors, |prj, cmd| {
22872287
prj.insert_ds_test();
22882288
prj.add_source(
22892289
"Counter.sol",
@@ -2351,35 +2351,33 @@ contract CounterTest is DSTest {
23512351
"size": 250
23522352
},
23532353
"functions": {
2354-
"a": {
2354+
"a()": {
23552355
"calls": 1,
23562356
"min": 2261,
23572357
"mean": 2261,
23582358
"median": 2261,
23592359
"max": 2261
23602360
},
2361-
"b": {
2361+
"b()": {
23622362
"calls": 1,
23632363
"min": 2305,
23642364
"mean": 2305,
23652365
"median": 2305,
23662366
"max": 2305
23672367
},
2368-
"setNumber": {
2369-
"setNumber(int256)": {
2370-
"calls": 2,
2371-
"min": 23648,
2372-
"mean": 33604,
2373-
"median": 33604,
2374-
"max": 43560
2375-
},
2376-
"setNumber(uint256)": {
2377-
"calls": 2,
2378-
"min": 23604,
2379-
"mean": 33560,
2380-
"median": 33560,
2381-
"max": 43516
2382-
}
2368+
"setNumber(int256)": {
2369+
"calls": 2,
2370+
"min": 23648,
2371+
"mean": 33604,
2372+
"median": 33604,
2373+
"max": 43560
2374+
},
2375+
"setNumber(uint256)": {
2376+
"calls": 2,
2377+
"min": 23604,
2378+
"mean": 33560,
2379+
"median": 33560,
2380+
"max": 43516
23832381
}
23842382
}
23852383
}

0 commit comments

Comments
 (0)