Skip to content

Commit

Permalink
try detecting duplicate strings
Browse files Browse the repository at this point in the history
  • Loading branch information
agourlay committed Feb 1, 2025
1 parent 4713ba4 commit afcd86a
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
19 changes: 15 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ mod utils;

use crate::args::get_args;
use crate::errors::HprofSlurpError;
use crate::result_recorder::RenderedResult;
use crate::slurp::slurp_file;
use std::time::Instant;

Expand All @@ -27,10 +28,20 @@ fn main_result() -> Result<(), HprofSlurpError> {
let rendered_result = slurp_file(file_path, top, debug_mode, list_strings)?;

// Print results
println!("{}", rendered_result.summary);
println!("{}", rendered_result.thread_info);
println!("{}", rendered_result.memory_usage);
if let Some(list_strings) = rendered_result.captured_strings {
let RenderedResult {
summary,
thread_info,
memory_usage,
duplicated_strings,
captured_strings,
} = rendered_result;
println!("{}", summary);
println!("{}", thread_info);
println!("{}", memory_usage);
if let Some(duplicated_strings) = duplicated_strings {
println!("{}", duplicated_strings);
}
if let Some(list_strings) = captured_strings {
println!("{}", list_strings);
}

Expand Down
19 changes: 19 additions & 0 deletions src/result_recorder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ pub struct RenderedResult {
pub summary: String,
pub thread_info: String,
pub memory_usage: String,
pub duplicated_strings: Option<String>,
pub captured_strings: Option<String>,
}

Expand Down Expand Up @@ -194,6 +195,7 @@ impl ResultRecorder {
summary: self.render_summary(),
thread_info: self.render_thread_info(),
memory_usage: self.render_memory_usage(self.top),
duplicated_strings: self.render_duplicated_strings(),
captured_strings: if self.list_strings {
Some(self.render_captured_strings())
} else {
Expand Down Expand Up @@ -332,6 +334,23 @@ impl ResultRecorder {
result
}

fn render_duplicated_strings(&self) -> Option<String> {
let mut strings: Vec<_> = self.utf8_strings_by_id.values().collect();
strings.sort();
let all_len = strings.len();
strings.dedup();
let dedup_len = strings.len();
if all_len == dedup_len {
None
} else {
Some(format!(
"\nFound {} duplicated strings out of {} unique strings\n",
all_len - dedup_len,
all_len
))
}
}

fn render_thread_info(&self) -> String {
let mut thread_info = String::new();

Expand Down

0 comments on commit afcd86a

Please sign in to comment.