@@ -4,7 +4,7 @@ use std::{
44 collections:: { hash_map:: Entry , HashMap } ,
55 env,
66 error:: Error ,
7- fs:: { create_dir_all, metadata, read_dir, remove_dir_all} ,
7+ fs:: { self , create_dir_all, metadata, read_dir, remove_dir_all} ,
88 mem:: { transmute, ManuallyDrop } ,
99 path:: Path ,
1010 sync:: Arc ,
@@ -63,6 +63,7 @@ pub struct LmdbBackingStorage {
6363 meta_db : Database ,
6464 forward_task_cache_db : Database ,
6565 reverse_task_cache_db : Database ,
66+ fresh_db : bool ,
6667}
6768
6869impl LmdbBackingStorage {
@@ -144,7 +145,10 @@ impl LmdbBackingStorage {
144145 let _ = remove_dir_all ( base_path) ;
145146 path = base_path. join ( "temp" ) ;
146147 }
147- create_dir_all ( & path) . context ( "Creating database directory failed" ) ?;
148+ let fresh_db = fs:: exists ( & path) . map_or ( false , |exists| !exists) ;
149+ if fresh_db {
150+ create_dir_all ( & path) . context ( "Creating database directory failed" ) ?;
151+ }
148152
149153 #[ cfg( target_arch = "x86" ) ]
150154 const MAP_SIZE : usize = usize:: MAX ;
@@ -175,6 +179,7 @@ impl LmdbBackingStorage {
175179 meta_db,
176180 forward_task_cache_db,
177181 reverse_task_cache_db,
182+ fresh_db,
178183 } )
179184 }
180185
@@ -430,6 +435,12 @@ impl BackingStorage for LmdbBackingStorage {
430435 tx : Option < ReadTransaction > ,
431436 task_type : & CachedTaskType ,
432437 ) -> Option < TaskId > {
438+ // Performance optimization when the database was empty
439+ // It's assumed that no cache entries are removed from the memory cache, but we might change
440+ // that in future.
441+ if self . fresh_db {
442+ return None ;
443+ }
433444 fn lookup (
434445 this : & LmdbBackingStorage ,
435446 tx : & RoTransaction < ' _ > ,
@@ -462,6 +473,12 @@ impl BackingStorage for LmdbBackingStorage {
462473 tx : Option < ReadTransaction > ,
463474 task_id : TaskId ,
464475 ) -> Option < Arc < CachedTaskType > > {
476+ // Performance optimization when the database was empty
477+ // It's assumed that no cache entries are removed from the memory cache, but we might change
478+ // that in future.
479+ if self . fresh_db {
480+ return None ;
481+ }
465482 fn lookup (
466483 this : & LmdbBackingStorage ,
467484 tx : & RoTransaction < ' _ > ,
@@ -492,6 +509,12 @@ impl BackingStorage for LmdbBackingStorage {
492509 task_id : TaskId ,
493510 category : TaskDataCategory ,
494511 ) -> Vec < CachedDataItem > {
512+ // Performance optimization when the database was empty
513+ // It's assumed that no cache entries are removed from the memory cache, but we might change
514+ // that in future.
515+ if self . fresh_db {
516+ return Vec :: new ( ) ;
517+ }
495518 fn lookup (
496519 this : & LmdbBackingStorage ,
497520 tx : & RoTransaction < ' _ > ,
0 commit comments