mirror of
https://github.com/rust-lang/rust-analyzer
synced 2025-01-26 11:55:04 +00:00
Merge #641
641: more stats r=matklad a=matklad Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
commit
6df1f71b7d
2 changed files with 133 additions and 36 deletions
|
@ -1,61 +1,149 @@
|
||||||
use std::fmt;
|
use std::{
|
||||||
|
fmt,
|
||||||
use ra_syntax::AstNode;
|
iter::FromIterator,
|
||||||
use ra_db::{
|
sync::Arc,
|
||||||
SourceFileQuery,
|
|
||||||
salsa::{Database, debug::DebugQueryTable},
|
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::db::RootDatabase;
|
use ra_syntax::{AstNode, TreeArc, SourceFile};
|
||||||
|
use ra_db::{
|
||||||
|
SourceFileQuery, FileTextQuery, SourceRootId,
|
||||||
|
salsa::{Database, debug::{DebugQueryTable, TableEntry}},
|
||||||
|
};
|
||||||
|
|
||||||
|
use crate::{
|
||||||
|
FileId, db::RootDatabase,
|
||||||
|
symbol_index::{SymbolIndex, LibrarySymbolsQuery},
|
||||||
|
};
|
||||||
|
|
||||||
pub(crate) fn status(db: &RootDatabase) -> String {
|
pub(crate) fn status(db: &RootDatabase) -> String {
|
||||||
let file_stats = {
|
let files_stats = db.query(FileTextQuery).entries::<FilesStats>();
|
||||||
let mut stats = FilesStats::default();
|
let syntax_tree_stats = db.query(SourceFileQuery).entries::<SyntaxTreeStats>();
|
||||||
for entry in db.query(SourceFileQuery).entries::<Vec<_>>() {
|
let symbols_stats = db
|
||||||
stats.total += 1;
|
.query(LibrarySymbolsQuery)
|
||||||
if let Some(value) = entry.value {
|
.entries::<LibrarySymbolsStats>();
|
||||||
stats.retained += 1;
|
|
||||||
stats.retained_size = stats
|
|
||||||
.retained_size
|
|
||||||
.checked_add(value.syntax().memory_size_of_subtree())
|
|
||||||
.unwrap();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
stats
|
|
||||||
};
|
|
||||||
let n_defs = {
|
let n_defs = {
|
||||||
let interner: &hir::HirInterner = db.as_ref();
|
let interner: &hir::HirInterner = db.as_ref();
|
||||||
interner.len()
|
interner.len()
|
||||||
};
|
};
|
||||||
format!("{}\nn_defs {}\n", file_stats, n_defs)
|
format!(
|
||||||
|
"{}\n{}\n{}\nn_defs {}\n",
|
||||||
|
files_stats, symbols_stats, syntax_tree_stats, n_defs
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
struct FilesStats {
|
struct FilesStats {
|
||||||
total: usize,
|
total: usize,
|
||||||
retained: usize,
|
size: Bytes,
|
||||||
retained_size: usize,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl fmt::Display for FilesStats {
|
impl fmt::Display for FilesStats {
|
||||||
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
||||||
let (size, suff) = human_bytes(self.retained_size);
|
write!(fmt, "{} ({}) files", self.total, self.size)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl FromIterator<TableEntry<FileId, Arc<String>>> for FilesStats {
|
||||||
|
fn from_iter<T>(iter: T) -> FilesStats
|
||||||
|
where
|
||||||
|
T: IntoIterator<Item = TableEntry<FileId, Arc<String>>>,
|
||||||
|
{
|
||||||
|
let mut res = FilesStats::default();
|
||||||
|
for entry in iter {
|
||||||
|
res.total += 1;
|
||||||
|
res.size += entry.value.unwrap().len();
|
||||||
|
}
|
||||||
|
res
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Default)]
|
||||||
|
struct SyntaxTreeStats {
|
||||||
|
total: usize,
|
||||||
|
retained: usize,
|
||||||
|
retained_size: Bytes,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl fmt::Display for SyntaxTreeStats {
|
||||||
|
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
||||||
write!(
|
write!(
|
||||||
fmt,
|
fmt,
|
||||||
"{} parsed_files, {} ({}{}) retained",
|
"{} trees, {} ({}) retained",
|
||||||
self.total, self.retained, size, suff
|
self.total, self.retained, self.retained_size,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn human_bytes(bytes: usize) -> (usize, &'static str) {
|
impl FromIterator<TableEntry<FileId, TreeArc<SourceFile>>> for SyntaxTreeStats {
|
||||||
if bytes < 4096 {
|
fn from_iter<T>(iter: T) -> SyntaxTreeStats
|
||||||
return (bytes, " bytes");
|
where
|
||||||
|
T: IntoIterator<Item = TableEntry<FileId, TreeArc<SourceFile>>>,
|
||||||
|
{
|
||||||
|
let mut res = SyntaxTreeStats::default();
|
||||||
|
for entry in iter {
|
||||||
|
res.total += 1;
|
||||||
|
if let Some(value) = entry.value {
|
||||||
|
res.retained += 1;
|
||||||
|
res.retained_size += value.syntax().memory_size_of_subtree();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
res
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Default)]
|
||||||
|
struct LibrarySymbolsStats {
|
||||||
|
total: usize,
|
||||||
|
fst_size: Bytes,
|
||||||
|
symbols_size: Bytes,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl fmt::Display for LibrarySymbolsStats {
|
||||||
|
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
||||||
|
write!(
|
||||||
|
fmt,
|
||||||
|
"{} ({} + {}) symbols",
|
||||||
|
self.total, self.fst_size, self.symbols_size
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl FromIterator<TableEntry<SourceRootId, Arc<SymbolIndex>>> for LibrarySymbolsStats {
|
||||||
|
fn from_iter<T>(iter: T) -> LibrarySymbolsStats
|
||||||
|
where
|
||||||
|
T: IntoIterator<Item = TableEntry<SourceRootId, Arc<SymbolIndex>>>,
|
||||||
|
{
|
||||||
|
let mut res = LibrarySymbolsStats::default();
|
||||||
|
for entry in iter {
|
||||||
|
let value = entry.value.unwrap();
|
||||||
|
res.total += value.len();
|
||||||
|
res.fst_size += value.fst_size();
|
||||||
|
res.symbols_size += value.symbols_size();
|
||||||
|
}
|
||||||
|
res
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Default)]
|
||||||
|
struct Bytes(usize);
|
||||||
|
|
||||||
|
impl fmt::Display for Bytes {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
|
let bytes = self.0;
|
||||||
|
if bytes < 4096 {
|
||||||
|
return write!(f, "{} bytes", bytes);
|
||||||
|
}
|
||||||
|
let kb = bytes / 1024;
|
||||||
|
if kb < 4096 {
|
||||||
|
return write!(f, "{}kb", kb);
|
||||||
|
}
|
||||||
|
let mb = kb / 1024;
|
||||||
|
write!(f, "{}mb", mb)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl std::ops::AddAssign<usize> for Bytes {
|
||||||
|
fn add_assign(&mut self, x: usize) {
|
||||||
|
self.0 += x;
|
||||||
}
|
}
|
||||||
let kb = bytes / 1024;
|
|
||||||
if kb < 4096 {
|
|
||||||
return (kb, "kb");
|
|
||||||
}
|
|
||||||
let mb = kb / 1024;
|
|
||||||
(mb, "mb")
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,6 +23,7 @@ use std::{
|
||||||
cmp::Ordering,
|
cmp::Ordering,
|
||||||
hash::{Hash, Hasher},
|
hash::{Hash, Hasher},
|
||||||
sync::Arc,
|
sync::Arc,
|
||||||
|
mem,
|
||||||
};
|
};
|
||||||
|
|
||||||
use fst::{self, Streamer};
|
use fst::{self, Streamer};
|
||||||
|
@ -136,6 +137,14 @@ impl SymbolIndex {
|
||||||
self.symbols.len()
|
self.symbols.len()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub(crate) fn fst_size(&self) -> usize {
|
||||||
|
self.map.as_fst().size()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn symbols_size(&self) -> usize {
|
||||||
|
self.symbols.len() * mem::size_of::<FileSymbol>()
|
||||||
|
}
|
||||||
|
|
||||||
pub(crate) fn for_files(
|
pub(crate) fn for_files(
|
||||||
files: impl ParallelIterator<Item = (FileId, TreeArc<SourceFile>)>,
|
files: impl ParallelIterator<Item = (FileId, TreeArc<SourceFile>)>,
|
||||||
) -> SymbolIndex {
|
) -> SymbolIndex {
|
||||||
|
|
Loading…
Reference in a new issue