Skip to content

Commit a739a57

Browse files
bors[bot]matklad
andauthored
Merge #9841
9841: internal: print total size of source code in analysis-stats r=matklad a=matklad bors r+ 🤖 Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
2 parents 62c8572 + a379bd7 commit a739a57

File tree

2 files changed

+35
-5
lines changed

2 files changed

+35
-5
lines changed

crates/rust-analyzer/src/cli/analysis_stats.rs

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,17 @@ use hir_def::{body::BodySourceMap, expr::ExprId, FunctionId};
1414
use hir_ty::{TyExt, TypeWalk};
1515
use ide::{Analysis, AnalysisHost, LineCol, RootDatabase};
1616
use ide_db::base_db::{
17-
salsa::{self, ParallelDatabase},
18-
SourceDatabaseExt,
17+
salsa::{self, debug::DebugQueryTable, ParallelDatabase},
18+
SourceDatabase, SourceDatabaseExt,
1919
};
2020
use itertools::Itertools;
2121
use oorandom::Rand32;
22+
use profile::{Bytes, StopWatch};
2223
use project_model::CargoConfig;
2324
use rayon::prelude::*;
2425
use rustc_hash::FxHashSet;
2526
use stdx::format_to;
26-
use syntax::AstNode;
27+
use syntax::{AstNode, SyntaxNode};
2728
use vfs::{Vfs, VfsPath};
2829

2930
use crate::cli::{
@@ -33,7 +34,6 @@ use crate::cli::{
3334
progress_report::ProgressReport,
3435
report_metric, Result, Verbosity,
3536
};
36-
use profile::StopWatch;
3737

3838
/// Need to wrap Snapshot to provide `Clone` impl for `map_with`
3939
struct Snap<DB>(DB);
@@ -137,6 +137,21 @@ impl flags::AnalysisStats {
137137
eprintln!("{}", profile::countme::get_all());
138138
}
139139

140+
if self.source_stats {
141+
let mut total_file_size = Bytes::default();
142+
for e in ide_db::base_db::ParseQuery.in_db(db).entries::<Vec<_>>() {
143+
total_file_size += syntax_len(db.parse(e.key).syntax_node())
144+
}
145+
146+
let mut total_macro_file_size = Bytes::default();
147+
for e in hir::db::ParseMacroExpansionQuery.in_db(db).entries::<Vec<_>>() {
148+
if let Some((val, _)) = db.parse_macro_expansion(e.key).value {
149+
total_macro_file_size += syntax_len(val.syntax_node())
150+
}
151+
}
152+
eprintln!("source files: {}, macro files: {}", total_file_size, total_macro_file_size);
153+
}
154+
140155
if self.memory_usage && verbosity.is_verbose() {
141156
print_memory_usage(host, vfs);
142157
}
@@ -361,3 +376,9 @@ fn shuffle<T>(rng: &mut Rand32, slice: &mut [T]) {
361376
fn percentage(n: u64, total: u64) -> u64 {
362377
(n * 100).checked_div(total).unwrap_or(100)
363378
}
379+
380+
fn syntax_len(node: SyntaxNode) -> usize {
381+
// Macro expanded code doesn't contain whitespace, so erase *all* whitespace
382+
// to make macro and non-macro code comparable.
383+
node.to_string().replace(|it: char| it.is_ascii_whitespace(), "").len()
384+
}

crates/rust-analyzer/src/cli/flags.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use ide_ssr::{SsrPattern, SsrRule};
77
use crate::cli::Verbosity;
88

99
xflags::xflags! {
10-
src "./src/bin/flags.rs"
10+
src "./src/cli/flags.rs"
1111

1212
/// LSP server for the Rust programming language.
1313
cmd rust-analyzer {
@@ -60,6 +60,8 @@ xflags::xflags! {
6060
optional --parallel
6161
/// Collect memory usage statistics.
6262
optional --memory-usage
63+
/// Print the total length of all source and macro files (whitespace is not counted).
64+
optional --source-stats
6365

6466
/// Only analyze items matching this path.
6567
optional -o, --only path: String
@@ -156,6 +158,7 @@ pub struct AnalysisStats {
156158
pub randomize: bool,
157159
pub parallel: bool,
158160
pub memory_usage: bool,
161+
pub source_stats: bool,
159162
pub only: Option<String>,
160163
pub with_deps: bool,
161164
pub no_sysroot: bool,
@@ -190,9 +193,15 @@ pub struct ProcMacro;
190193
impl RustAnalyzer {
191194
pub const HELP: &'static str = Self::HELP_;
192195

196+
#[allow(dead_code)]
193197
pub fn from_env() -> xflags::Result<Self> {
194198
Self::from_env_()
195199
}
200+
201+
#[allow(dead_code)]
202+
pub fn from_vec(args: Vec<std::ffi::OsString>) -> xflags::Result<Self> {
203+
Self::from_vec_(args)
204+
}
196205
}
197206
// generated end
198207

0 commit comments

Comments
 (0)