Skip to content

Commit

Permalink
perf: clear from test suite result
Browse files Browse the repository at this point in the history
  • Loading branch information
DaniPopes committed Jun 1, 2024
1 parent eab4717 commit 0fe8a04
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
5 changes: 5 additions & 0 deletions crates/common/src/contracts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ impl ContractsByArtifact {
Self(Arc::new(map))
}

/// Clears all contracts.
pub fn clear(&mut self) {
*self = Self::default();
}

/// Finds a contract which has a similar bytecode as `code`.
pub fn find_by_creation_code(&self, code: &[u8]) -> Option<ArtifactWithContractRef> {
self.iter().find(|(_, contract)| {
Expand Down
11 changes: 7 additions & 4 deletions crates/forge/bin/cmd/test/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -392,12 +392,12 @@ impl TestArgs {
let mut outcome = TestOutcome::empty(self.allow_failure);

let mut any_test_failed = false;
for (contract_name, suite_result) in rx {
for (contract_name, mut suite_result) in rx {
let tests = &suite_result.test_results;

// Set up trace identifiers.
let known_contracts = suite_result.known_contracts.clone();
let mut identifier = TraceIdentifiers::new().with_local(&known_contracts);
let known_contracts = &suite_result.known_contracts;
let mut identifier = TraceIdentifiers::new().with_local(known_contracts);

// Avoid using etherscan for gas report as we decode more traces and this will be
// expensive.
Expand All @@ -407,7 +407,7 @@ impl TestArgs {

// Build the trace decoder.
let mut builder = CallTraceDecoderBuilder::new()
.with_known_contracts(&known_contracts)
.with_known_contracts(known_contracts)
.with_verbosity(verbosity);
// Signatures are of no value for gas reports.
if !self.gas_report {
Expand Down Expand Up @@ -524,6 +524,9 @@ impl TestArgs {
// Print suite summary.
shell::println(suite_result.summary())?;

// Free memory.
suite_result.keep_only_results();

// Add the suite result to the outcome.
outcome.results.insert(contract_name, suite_result);
outcome.last_run_decoder = Some(decoder);
Expand Down
5 changes: 5 additions & 0 deletions crates/forge/src/result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,11 @@ impl SuiteResult {
Self { duration, test_results, warnings, libraries, known_contracts }
}

/// Frees memory that is not used for the final output.
pub fn keep_only_results(&mut self) {
ContractsByArtifact::clear(&mut self.known_contracts);
}

/// Returns an iterator over all individual succeeding tests and their names.
pub fn successes(&self) -> impl Iterator<Item = (&String, &TestResult)> {
self.tests().filter(|(_, t)| t.status == TestStatus::Success)
Expand Down

0 comments on commit 0fe8a04

Please sign in to comment.