@@ -7,6 +7,7 @@ mod input;
77
88use std:: panic;
99
10+ use salsa:: Durability ;
1011use syntax:: { ast, Parse , SourceFile } ;
1112use 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 ;
4547pub const DEFAULT_PARSE_LRU_CAP : usize = 128 ;
4648pub 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 ) ]
9092pub 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+
104147fn source_root_crates ( db : & dyn SourceDatabaseExt , id : SourceRootId ) -> Arc < [ CrateId ] > {
105148 let graph = db. crate_graph ( ) ;
106149 let mut crates = graph
0 commit comments