From ca332883cc5e7cff0dbd2bcda11283875ed4eceb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Renault?= Date: Mon, 22 Jul 2024 13:39:20 +0200 Subject: [PATCH] Use an LRU only with 1000 entries --- milli/src/update/index_documents/cache.rs | 41 ++++++------------- .../extract/extract_facet_number_docids.rs | 2 +- .../extract/extract_facet_string_docids.rs | 2 +- .../extract/extract_fid_word_count_docids.rs | 2 +- .../extract/extract_word_docids.rs | 6 +-- .../extract_word_pair_proximity_docids.rs | 2 +- .../extract/extract_word_position_docids.rs | 2 +- milli/src/update/word_prefix_docids.rs | 2 +- .../src/update/words_prefix_integer_docids.rs | 2 +- 9 files changed, 23 insertions(+), 38 deletions(-) diff --git a/milli/src/update/index_documents/cache.rs b/milli/src/update/index_documents/cache.rs index 47f0f44b1..81616744f 100644 --- a/milli/src/update/index_documents/cache.rs +++ b/milli/src/update/index_documents/cache.rs @@ -14,7 +14,7 @@ use crate::CboRoaringBitmapCodec; const ENABLED: bool = true; pub struct SorterCacheDelAddCboRoaringBitmap { - cache: ArcCache, DelAddRoaringBitmap>, + cache: LruCache, DelAddRoaringBitmap>, prefix: &'static [u8; 3], sorter: grenad::Sorter, deladd_buffer: Vec, @@ -30,7 +30,7 @@ impl SorterCacheDelAddCboRoaringBitmap { conn: sled::Db, ) -> Self { SorterCacheDelAddCboRoaringBitmap { - cache: ArcCache::new(cap), + cache: LruCache::new(cap), prefix, sorter, deladd_buffer: Vec::new(), @@ -49,7 +49,7 @@ where return self.write_entry_to_sorter(key, DelAddRoaringBitmap::new_del_u32(n)); } - let (cache, evicted) = self.cache.get_mut(key); + let cache = self.cache.get_mut(key); match cache { Some(DelAddRoaringBitmap { del, add: _ }) => { del.get_or_insert_with(RoaringBitmap::new).insert(n); @@ -62,10 +62,7 @@ where } } - match evicted { - Some((key, value)) => self.write_entry_to_sorter(key, value), - None => Ok(()), - } + Ok(()) } pub fn insert_del( @@ -77,7 +74,7 @@ where return self.write_entry_to_sorter(key, DelAddRoaringBitmap::new_del(bitmap)); } - let (cache, evicted) = self.cache.get_mut(key); + let cache = self.cache.get_mut(key); match cache { Some(DelAddRoaringBitmap { del, add: _ }) => { *del.get_or_insert_with(RoaringBitmap::new) |= bitmap; @@ -90,10 +87,7 @@ where } } - match evicted { - Some((key, value)) => self.write_entry_to_sorter(key, value), - None => Ok(()), - } + Ok(()) } pub fn insert_add_u32(&mut self, key: &[u8], n: u32) -> Result<(), grenad::Error> { @@ -101,7 +95,7 @@ where return self.write_entry_to_sorter(key, DelAddRoaringBitmap::new_add_u32(n)); } - let (cache, evicted) = self.cache.get_mut(key); + let cache = self.cache.get_mut(key); match cache { Some(DelAddRoaringBitmap { del: _, add }) => { add.get_or_insert_with(RoaringBitmap::new).insert(n); @@ -114,10 +108,7 @@ where } } - match evicted { - Some((key, value)) => self.write_entry_to_sorter(key, value), - None => Ok(()), - } + Ok(()) } pub fn insert_add( @@ -129,7 +120,7 @@ where return self.write_entry_to_sorter(key, DelAddRoaringBitmap::new_add(bitmap)); } - let (cache, evicted) = self.cache.get_mut(key); + let cache = self.cache.get_mut(key); match cache { Some(DelAddRoaringBitmap { del: _, add }) => { *add.get_or_insert_with(RoaringBitmap::new) |= bitmap; @@ -142,10 +133,7 @@ where } } - match evicted { - Some((key, value)) => self.write_entry_to_sorter(key, value), - None => Ok(()), - } + Ok(()) } pub fn insert_del_add_u32(&mut self, key: &[u8], n: u32) -> Result<(), grenad::Error> { @@ -153,7 +141,7 @@ where return self.write_entry_to_sorter(key, DelAddRoaringBitmap::new_del_add_u32(n)); } - let (cache, evicted) = self.cache.get_mut(key); + let cache = self.cache.get_mut(key); match cache { Some(DelAddRoaringBitmap { del, add }) => { del.get_or_insert_with(RoaringBitmap::new).insert(n); @@ -167,10 +155,7 @@ where } } - match evicted { - Some((key, value)) => self.write_entry_to_sorter(key, value), - None => Ok(()), - } + Ok(()) } fn write_entry_to_sorter>( @@ -218,7 +203,7 @@ where } pub fn into_sorter(mut self) -> Result, grenad::Error> { - let default_arc = ArcCache::new(NonZeroUsize::MIN); + let default_arc = LruCache::new(NonZeroUsize::MIN); for (key, deladd) in mem::replace(&mut self.cache, default_arc) { self.write_entry_to_sorter(key, deladd)?; } diff --git a/milli/src/update/index_documents/extract/extract_facet_number_docids.rs b/milli/src/update/index_documents/extract/extract_facet_number_docids.rs index b2e5a0a4a..98bb48a18 100644 --- a/milli/src/update/index_documents/extract/extract_facet_number_docids.rs +++ b/milli/src/update/index_documents/extract/extract_facet_number_docids.rs @@ -38,7 +38,7 @@ pub fn extract_facet_number_docids( ); let mut cached_facet_number_docids_sorter = SorterCacheDelAddCboRoaringBitmap::<20, MergeFn>::new( - NonZeroUsize::new(500).unwrap(), + NonZeroUsize::new(1000).unwrap(), facet_number_docids_sorter, b"fnd", super::SLED_DB.clone(), diff --git a/milli/src/update/index_documents/extract/extract_facet_string_docids.rs b/milli/src/update/index_documents/extract/extract_facet_string_docids.rs index 043c3de9b..3bc43dc9b 100644 --- a/milli/src/update/index_documents/extract/extract_facet_string_docids.rs +++ b/milli/src/update/index_documents/extract/extract_facet_string_docids.rs @@ -46,7 +46,7 @@ pub fn extract_facet_string_docids( ); let mut cached_facet_string_docids_sorter = SorterCacheDelAddCboRoaringBitmap::<20, MergeFn>::new( - NonZeroUsize::new(500).unwrap(), + NonZeroUsize::new(1000).unwrap(), facet_string_docids_sorter, b"fsd", SLED_DB.clone(), diff --git a/milli/src/update/index_documents/extract/extract_fid_word_count_docids.rs b/milli/src/update/index_documents/extract/extract_fid_word_count_docids.rs index 768db7225..a8b330655 100644 --- a/milli/src/update/index_documents/extract/extract_fid_word_count_docids.rs +++ b/milli/src/update/index_documents/extract/extract_fid_word_count_docids.rs @@ -41,7 +41,7 @@ pub fn extract_fid_word_count_docids( ); let mut cached_fid_word_count_docids_sorter = SorterCacheDelAddCboRoaringBitmap::<20, MergeFn>::new( - NonZeroUsize::new(500).unwrap(), + NonZeroUsize::new(1000).unwrap(), fid_word_count_docids_sorter, b"fwc", super::SLED_DB.clone(), diff --git a/milli/src/update/index_documents/extract/extract_word_docids.rs b/milli/src/update/index_documents/extract/extract_word_docids.rs index 36b04e87f..63dd7a2f7 100644 --- a/milli/src/update/index_documents/extract/extract_word_docids.rs +++ b/milli/src/update/index_documents/extract/extract_word_docids.rs @@ -50,7 +50,7 @@ pub fn extract_word_docids( max_memory.map(|m| m / 3), ); let mut cached_word_fid_docids_sorter = SorterCacheDelAddCboRoaringBitmap::<20, _>::new( - NonZeroUsize::new(500).unwrap(), + NonZeroUsize::new(1000).unwrap(), word_fid_docids_sorter, b"wfd", SLED_DB.clone(), @@ -111,7 +111,7 @@ pub fn extract_word_docids( max_memory.map(|m| m / 3), ); let mut cached_word_docids_sorter = SorterCacheDelAddCboRoaringBitmap::<20, MergeFn>::new( - NonZeroUsize::new(500).unwrap(), + NonZeroUsize::new(1000).unwrap(), word_docids_sorter, b"wdi", SLED_DB.clone(), @@ -126,7 +126,7 @@ pub fn extract_word_docids( max_memory.map(|m| m / 3), ); let mut cached_exact_word_docids_sorter = SorterCacheDelAddCboRoaringBitmap::<20, MergeFn>::new( - NonZeroUsize::new(500).unwrap(), + NonZeroUsize::new(1000).unwrap(), exact_word_docids_sorter, b"ewd", SLED_DB.clone(), diff --git a/milli/src/update/index_documents/extract/extract_word_pair_proximity_docids.rs b/milli/src/update/index_documents/extract/extract_word_pair_proximity_docids.rs index 43f377153..e0678404a 100644 --- a/milli/src/update/index_documents/extract/extract_word_pair_proximity_docids.rs +++ b/milli/src/update/index_documents/extract/extract_word_pair_proximity_docids.rs @@ -53,7 +53,7 @@ pub fn extract_word_pair_proximity_docids( max_memory.map(|m| m / MAX_DISTANCE as usize), ); SorterCacheDelAddCboRoaringBitmap::<20, MergeFn>::new( - NonZeroUsize::new(500).unwrap(), + NonZeroUsize::new(1000).unwrap(), sorter, b"wpp", super::SLED_DB.clone(), diff --git a/milli/src/update/index_documents/extract/extract_word_position_docids.rs b/milli/src/update/index_documents/extract/extract_word_position_docids.rs index 679196e32..dc12cefb5 100644 --- a/milli/src/update/index_documents/extract/extract_word_position_docids.rs +++ b/milli/src/update/index_documents/extract/extract_word_position_docids.rs @@ -39,7 +39,7 @@ pub fn extract_word_position_docids( ); let mut cached_word_position_docids_sorter = SorterCacheDelAddCboRoaringBitmap::<20, MergeFn>::new( - NonZeroUsize::new(500).unwrap(), + NonZeroUsize::new(1000).unwrap(), word_position_docids_sorter, b"wpd", super::SLED_DB.clone(), diff --git a/milli/src/update/word_prefix_docids.rs b/milli/src/update/word_prefix_docids.rs index 9fb5c8585..d5ca711c2 100644 --- a/milli/src/update/word_prefix_docids.rs +++ b/milli/src/update/word_prefix_docids.rs @@ -66,7 +66,7 @@ impl<'t, 'i> WordPrefixDocids<'t, 'i> { self.max_memory, ); let mut cached_prefix_docids_sorter = SorterCacheDelAddCboRoaringBitmap::<20, MergeFn>::new( - NonZeroUsize::new(500).unwrap(), + NonZeroUsize::new(1000).unwrap(), prefix_docids_sorter, b"pdi", SLED_DB.clone(), diff --git a/milli/src/update/words_prefix_integer_docids.rs b/milli/src/update/words_prefix_integer_docids.rs index bef95155d..92e64625f 100644 --- a/milli/src/update/words_prefix_integer_docids.rs +++ b/milli/src/update/words_prefix_integer_docids.rs @@ -71,7 +71,7 @@ impl<'t, 'i> WordPrefixIntegerDocids<'t, 'i> { ); let mut cached_prefix_integer_docids_sorter = SorterCacheDelAddCboRoaringBitmap::<20, MergeFn>::new( - NonZeroUsize::new(500).unwrap(), + NonZeroUsize::new(1000).unwrap(), prefix_integer_docids_sorter, b"pid", SLED_DB.clone(),