Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 02b6c18

Browse files
committed
Compress file text using lz4 in salsa
1 parent 2397e7a commit 02b6c18

File tree

9 files changed

+65
-9
lines changed

9 files changed

+65
-9
lines changed

Cargo.lock

Lines changed: 11 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/base-db/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ rust-version.workspace = true
1212
doctest = false
1313

1414
[dependencies]
15+
lz4_flex = { version = "0.11", default-features = false }
16+
1517
la-arena.workspace = true
1618
salsa.workspace = true
1719
rustc-hash.workspace = true

crates/base-db/src/change.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use salsa::Durability;
77
use triomphe::Arc;
88
use vfs::FileId;
99

10-
use crate::{CrateGraph, SourceDatabaseExt, SourceRoot, SourceRootId};
10+
use crate::{CrateGraph, SourceDatabaseExt, SourceDatabaseExt2, SourceRoot, SourceRootId};
1111

1212
/// Encapsulate a bunch of raw `.set` calls on the database.
1313
#[derive(Default)]

crates/base-db/src/lib.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ mod input;
77

88
use std::panic;
99

10+
use salsa::Durability;
1011
use syntax::{ast, Parse, SourceFile};
1112
use triomphe::Arc;
1213

@@ -42,6 +43,7 @@ pub trait Upcast<T: ?Sized> {
4243
fn upcast(&self) -> &T;
4344
}
4445

46+
pub const DEFAULT_FILE_TEXT_LRU_CAP: usize = 16;
4547
pub const DEFAULT_PARSE_LRU_CAP: usize = 128;
4648
pub const DEFAULT_BORROWCK_LRU_CAP: usize = 1024;
4749

@@ -89,7 +91,10 @@ fn parse(db: &dyn SourceDatabase, file_id: FileId) -> Parse<ast::SourceFile> {
8991
#[salsa::query_group(SourceDatabaseExtStorage)]
9092
pub trait SourceDatabaseExt: SourceDatabase {
9193
#[salsa::input]
94+
fn compressed_file_text(&self, file_id: FileId) -> Arc<[u8]>;
95+
9296
fn file_text(&self, file_id: FileId) -> Arc<str>;
97+
9398
/// Path to a file, relative to the root of its source root.
9499
/// Source root of the file.
95100
#[salsa::input]
@@ -101,6 +106,44 @@ pub trait SourceDatabaseExt: SourceDatabase {
101106
fn source_root_crates(&self, id: SourceRootId) -> Arc<[CrateId]>;
102107
}
103108

109+
fn file_text(db: &dyn SourceDatabaseExt, file_id: FileId) -> Arc<str> {
110+
let bytes = db.compressed_file_text(file_id);
111+
let bytes =
112+
lz4_flex::decompress_size_prepended(&bytes).expect("lz4 decompression should not fail");
113+
let text = std::str::from_utf8(&bytes).expect("file contents should be valid UTF-8");
114+
Arc::from(text)
115+
}
116+
117+
pub trait SourceDatabaseExt2 {
118+
fn set_file_text(&mut self, file_id: FileId, text: Arc<str>) {
119+
self.set_file_text_with_durability(file_id, text, Durability::LOW);
120+
}
121+
122+
fn set_file_text_with_durability(
123+
&mut self,
124+
file_id: FileId,
125+
text: Arc<str>,
126+
durability: Durability,
127+
);
128+
}
129+
130+
impl<Db: ?Sized + SourceDatabaseExt> SourceDatabaseExt2 for Db {
131+
fn set_file_text_with_durability(
132+
&mut self,
133+
file_id: FileId,
134+
text: Arc<str>,
135+
durability: Durability,
136+
) {
137+
let bytes = text.as_bytes();
138+
let compressed = lz4_flex::compress_prepend_size(&bytes);
139+
self.set_compressed_file_text_with_durability(
140+
file_id,
141+
Arc::from(compressed.as_slice()),
142+
durability,
143+
)
144+
}
145+
}
146+
104147
fn source_root_crates(db: &dyn SourceDatabaseExt, id: SourceRootId) -> Arc<[CrateId]> {
105148
let graph = db.crate_graph();
106149
let mut crates = graph

crates/hir-def/src/nameres/tests/incremental.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use base_db::{SourceDatabase, SourceDatabaseExt};
1+
use base_db::{SourceDatabase, SourceDatabaseExt2 as _};
22
use test_fixture::WithFixture;
33
use triomphe::Arc;
44

crates/hir-ty/src/tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ mod traits;
1212

1313
use std::env;
1414

15-
use base_db::{FileRange, SourceDatabaseExt};
15+
use base_db::{FileRange, SourceDatabaseExt2 as _};
1616
use expect_test::Expect;
1717
use hir_def::{
1818
body::{Body, BodySourceMap, SyntheticSyntax},

crates/hir-ty/src/tests/incremental.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use base_db::SourceDatabaseExt;
1+
use base_db::SourceDatabaseExt2 as _;
22
use test_fixture::WithFixture;
33
use triomphe::Arc;
44

crates/ide-db/src/apply_change.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ impl RootDatabase {
205205

206206
// SourceDatabaseExt
207207
base_db::FileTextQuery
208+
base_db::CompressedFileTextQuery
208209
base_db::FileSourceRootQuery
209210
base_db::SourceRootQuery
210211
base_db::SourceRootCratesQuery

crates/ide-db/src/lib.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ use std::{fmt, mem::ManuallyDrop};
5151
use base_db::{
5252
salsa::{self, Durability},
5353
AnchoredPath, CrateId, FileId, FileLoader, FileLoaderDelegate, SourceDatabase, Upcast,
54+
DEFAULT_FILE_TEXT_LRU_CAP,
5455
};
5556
use hir::db::{DefDatabase, ExpandDatabase, HirDatabase};
5657
use triomphe::Arc;
@@ -157,6 +158,7 @@ impl RootDatabase {
157158

158159
pub fn update_base_query_lru_capacities(&mut self, lru_capacity: Option<usize>) {
159160
let lru_capacity = lru_capacity.unwrap_or(base_db::DEFAULT_PARSE_LRU_CAP);
161+
base_db::FileTextQuery.in_db_mut(self).set_lru_capacity(DEFAULT_FILE_TEXT_LRU_CAP);
160162
base_db::ParseQuery.in_db_mut(self).set_lru_capacity(lru_capacity);
161163
// macro expansions are usually rather small, so we can afford to keep more of them alive
162164
hir::db::ParseMacroExpansionQuery.in_db_mut(self).set_lru_capacity(4 * lru_capacity);
@@ -166,6 +168,7 @@ impl RootDatabase {
166168
pub fn update_lru_capacities(&mut self, lru_capacities: &FxHashMap<Box<str>, usize>) {
167169
use hir::db as hir_db;
168170

171+
base_db::FileTextQuery.in_db_mut(self).set_lru_capacity(DEFAULT_FILE_TEXT_LRU_CAP);
169172
base_db::ParseQuery.in_db_mut(self).set_lru_capacity(
170173
lru_capacities
171174
.get(stringify!(ParseQuery))
@@ -199,7 +202,7 @@ impl RootDatabase {
199202
// base_db::ProcMacrosQuery
200203

201204
// SourceDatabaseExt
202-
// base_db::FileTextQuery
205+
base_db::FileTextQuery
203206
// base_db::FileSourceRootQuery
204207
// base_db::SourceRootQuery
205208
base_db::SourceRootCratesQuery

0 commit comments

Comments
 (0)