From 046ae1d361d8941a664919e7668a65ae735d4a1b Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 25 Jan 2019 17:20:52 +0300 Subject: more stats --- crates/ra_ide_api/src/status.rs | 154 ++++++++++++++++++++++++++-------- crates/ra_ide_api/src/symbol_index.rs | 9 ++ 2 files changed, 130 insertions(+), 33 deletions(-) diff --git a/crates/ra_ide_api/src/status.rs b/crates/ra_ide_api/src/status.rs index c184ca892..717537fcd 100644 --- a/crates/ra_ide_api/src/status.rs +++ b/crates/ra_ide_api/src/status.rs @@ -1,61 +1,149 @@ -use std::fmt; +use std::{ + fmt, + iter::FromIterator, + sync::Arc, +}; -use ra_syntax::AstNode; +use ra_syntax::{AstNode, TreeArc, SourceFile}; use ra_db::{ - SourceFileQuery, - salsa::{Database, debug::DebugQueryTable}, + SourceFileQuery, FileTextQuery, SourceRootId, + salsa::{Database, debug::{DebugQueryTable, TableEntry}}, }; -use crate::db::RootDatabase; +use crate::{ + FileId, db::RootDatabase, + symbol_index::{SymbolIndex, LibrarySymbolsQuery}, +}; pub(crate) fn status(db: &RootDatabase) -> String { - let file_stats = { - let mut stats = FilesStats::default(); - for entry in db.query(SourceFileQuery).entries::>() { - stats.total += 1; - if let Some(value) = entry.value { - stats.retained += 1; - stats.retained_size = stats - .retained_size - .checked_add(value.syntax().memory_size_of_subtree()) - .unwrap(); - } - } - stats - }; + let files_stats = db.query(FileTextQuery).entries::(); + let syntax_tree_stats = db.query(SourceFileQuery).entries::(); + let symbols_stats = db + .query(LibrarySymbolsQuery) + .entries::(); let n_defs = { let interner: &hir::HirInterner = db.as_ref(); 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)] struct FilesStats { total: usize, - retained: usize, - retained_size: usize, + size: Bytes, } impl fmt::Display for FilesStats { 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>> for FilesStats { + fn from_iter(iter: T) -> FilesStats + where + T: IntoIterator>>, + { + 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!( fmt, - "{} parsed_files, {} ({}{}) retained", - self.total, self.retained, size, suff + "{} trees, {} ({}) retained", + self.total, self.retained, self.retained_size, ) } } -fn human_bytes(bytes: usize) -> (usize, &'static str) { - if bytes < 4096 { - return (bytes, " bytes"); +impl FromIterator>> for SyntaxTreeStats { + fn from_iter(iter: T) -> SyntaxTreeStats + where + T: IntoIterator>>, + { + 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 } - let kb = bytes / 1024; - if kb < 4096 { - return (kb, "kb"); +} + +#[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>> for LibrarySymbolsStats { + fn from_iter(iter: T) -> LibrarySymbolsStats + where + T: IntoIterator>>, + { + 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 for Bytes { + fn add_assign(&mut self, x: usize) { + self.0 += x; } - let mb = kb / 1024; - (mb, "mb") } diff --git a/crates/ra_ide_api/src/symbol_index.rs b/crates/ra_ide_api/src/symbol_index.rs index 1b5d1eb1d..69a669b8d 100644 --- a/crates/ra_ide_api/src/symbol_index.rs +++ b/crates/ra_ide_api/src/symbol_index.rs @@ -23,6 +23,7 @@ use std::{ cmp::Ordering, hash::{Hash, Hasher}, sync::Arc, + mem, }; use fst::{self, Streamer}; @@ -136,6 +137,14 @@ impl SymbolIndex { 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::() + } + pub(crate) fn for_files( files: impl ParallelIterator)>, ) -> SymbolIndex { -- cgit v1.2.3