@@ -14,7 +14,9 @@ use std::{
1414} ;
1515
1616use linera_base:: ensure;
17+ use rocksdb:: { BlockBasedOptions , Cache , DBCompactionStyle } ;
1718use serde:: { Deserialize , Serialize } ;
19+ use sysinfo:: { CpuRefreshKind , MemoryRefreshKind , RefreshKind , System } ;
1820use tempfile:: TempDir ;
1921use thiserror:: Error ;
2022
@@ -49,8 +51,9 @@ const MAX_VALUE_SIZE: usize = 3221225072;
4951// 8388608 and so for offset reason we decrease by 400
5052const MAX_KEY_SIZE : usize = 8388208 ;
5153
52- const DB_CACHE_SIZE : usize = 128 * 1024 * 1024 ; // 128 MiB
53- const DB_MAX_WRITE_BUFFER_NUMBER : i32 = 8 ;
54+ const WRITE_BUFFER_SIZE : usize = 64 * 1024 * 1024 ; // 64 MB
55+ const MAX_WRITE_BUFFER_NUMBER : i32 = 32 ;
56+ const HYPER_CLOCK_CACHE_BLOCK_SIZE : usize = 8 * 1024 ; // 8 KB
5457
5558/// The RocksDB client that we use.
5659type DB = rocksdb:: DBWithThreadMode < rocksdb:: MultiThreaded > ;
@@ -300,21 +303,38 @@ impl RocksDbStoreInternal {
300303 if !std:: path:: Path :: exists ( & path_buf) {
301304 std:: fs:: create_dir ( path_buf. clone ( ) ) ?;
302305 }
306+ let sys = System :: new_with_specifics (
307+ RefreshKind :: nothing ( )
308+ . with_cpu ( CpuRefreshKind :: everything ( ) )
309+ . with_memory ( MemoryRefreshKind :: nothing ( ) . with_ram ( ) ) ,
310+ ) ;
311+ let num_cpus = sys. cpus ( ) . len ( ) as i32 ;
312+ let total_ram = sys. total_memory ( ) as usize ;
303313 let mut options = rocksdb:: Options :: default ( ) ;
304314 options. create_if_missing ( true ) ;
305315 options. create_missing_column_families ( true ) ;
306316 // Flush in-memory buffer to disk more often
307- options. set_write_buffer_size ( DB_CACHE_SIZE ) ;
308- options. set_max_write_buffer_number ( DB_MAX_WRITE_BUFFER_NUMBER ) ;
317+ options. set_write_buffer_size ( WRITE_BUFFER_SIZE ) ;
318+ options. set_max_write_buffer_number ( MAX_WRITE_BUFFER_NUMBER ) ;
309319 options. set_compression_type ( rocksdb:: DBCompressionType :: Lz4 ) ;
310- options. set_level_zero_slowdown_writes_trigger ( -1 ) ;
311- options. set_level_zero_stop_writes_trigger ( 48 ) ;
312- options. set_stats_dump_period_sec ( 60 ) ;
313- options. enable_statistics ( ) ;
314- options. increase_parallelism ( num_cpus:: get ( ) as i32 ) ;
315- options. set_max_background_jobs ( 8 ) ;
320+ options. set_level_zero_slowdown_writes_trigger ( 12 ) ;
321+ options. set_level_zero_stop_writes_trigger ( 20 ) ;
322+ options. increase_parallelism ( num_cpus / 2 ) ;
323+ options. set_max_background_jobs ( num_cpus / 3 ) ;
316324 options. set_level_compaction_dynamic_level_bytes ( true ) ;
317325
326+ options. set_compaction_style ( DBCompactionStyle :: Level ) ;
327+ options. set_target_file_size_base ( WRITE_BUFFER_SIZE as u64 ) ;
328+
329+ let mut block_options = BlockBasedOptions :: default ( ) ;
330+ block_options. set_pin_l0_filter_and_index_blocks_in_cache ( true ) ;
331+ block_options. set_cache_index_and_filter_blocks ( true ) ;
332+ block_options. set_block_cache ( & Cache :: new_hyper_clock_cache (
333+ total_ram / 4 ,
334+ HYPER_CLOCK_CACHE_BLOCK_SIZE ,
335+ ) ) ;
336+ options. set_block_based_table_factory ( & block_options) ;
337+
318338 let db = DB :: open ( & options, path_buf) ?;
319339 let executor = RocksDbStoreExecutor {
320340 db : Arc :: new ( db) ,
0 commit comments