diff --git a/lib/common/common/src/counter/conditioned_counter.rs b/lib/common/common/src/counter/conditioned_counter.rs index bf6ad9ea73..f3733a9983 100644 --- a/lib/common/common/src/counter/conditioned_counter.rs +++ b/lib/common/common/src/counter/conditioned_counter.rs @@ -21,6 +21,16 @@ impl<'a> ConditionedCounter<'a> { tmp: HardwareCounterCell::disposable(), // We manually accumulate collected values! } } + + /// Never measure hardware. + pub fn never(parent: &'a HardwareCounterCell) -> Self { + Self::new(false, parent) + } + + /// Always measure hardware. + pub fn always(parent: &'a HardwareCounterCell) -> Self { + Self::new(true, parent) + } } impl Deref for ConditionedCounter<'_> { diff --git a/lib/segment/src/index/field_index/field_index_base.rs b/lib/segment/src/index/field_index/field_index_base.rs index 7cb8c466a0..2c0dc3aca4 100644 --- a/lib/segment/src/index/field_index/field_index_base.rs +++ b/lib/segment/src/index/field_index/field_index_base.rs @@ -181,7 +181,7 @@ impl FieldIndex { let query = full_text_index.parse_query(text, hw_counter); for value in FullTextIndex::get_values(payload_value) { let document = full_text_index.parse_document(&value, hw_counter); - if query.check_match(&document, hw_counter) { + if query.check_match(&document) { return Some(true); } } diff --git a/lib/segment/src/index/field_index/full_text_index/compressed_posting/compressed_chunks_reader.rs b/lib/segment/src/index/field_index/full_text_index/compressed_posting/compressed_chunks_reader.rs index ca2d8f26ef..19725e63a3 100644 --- a/lib/segment/src/index/field_index/full_text_index/compressed_posting/compressed_chunks_reader.rs +++ b/lib/segment/src/index/field_index/full_text_index/compressed_posting/compressed_chunks_reader.rs @@ -1,5 +1,5 @@ use bitpacking::BitPacker; -use common::counter::hardware_counter::HardwareCounterCell; +use common::counter::conditioned_counter::ConditionedCounter; use common::types::PointOffsetType; use crate::index::field_index::full_text_index::compressed_posting::compressed_common::{ @@ -11,7 +11,7 @@ pub struct ChunkReader<'a> { chunks: &'a [CompressedPostingChunksIndex], data: &'a [u8], remainder_postings: &'a [PointOffsetType], - hw_counter: &'a HardwareCounterCell, + hw_counter: ConditionedCounter<'a>, } impl<'a> ChunkReader<'a> { @@ -20,7 +20,7 @@ impl<'a> ChunkReader<'a> { chunks: &'a [CompressedPostingChunksIndex], data: &'a [u8], reminder_postings: &'a [PointOffsetType], - hw_counter: &'a HardwareCounterCell, + hw_counter: ConditionedCounter<'a>, ) -> Self { Self { data, diff --git a/lib/segment/src/index/field_index/full_text_index/compressed_posting/compressed_posting_list.rs b/lib/segment/src/index/field_index/full_text_index/compressed_posting/compressed_posting_list.rs index 41ebf01532..30eb7e9b73 100644 --- a/lib/segment/src/index/field_index/full_text_index/compressed_posting/compressed_posting_list.rs +++ b/lib/segment/src/index/field_index/full_text_index/compressed_posting/compressed_posting_list.rs @@ -1,4 +1,5 @@ use bitpacking::BitPacker; +use common::counter::conditioned_counter::ConditionedCounter; use common::counter::hardware_counter::HardwareCounterCell; use common::types::PointOffsetType; @@ -41,7 +42,7 @@ impl CompressedPostingList { &self.chunks, &self.data, &self.remainder_postings, - hw_counter, + ConditionedCounter::never(hw_counter), ) } diff --git a/lib/segment/src/index/field_index/full_text_index/immutable_inverted_index.rs b/lib/segment/src/index/field_index/full_text_index/immutable_inverted_index.rs index e30ba5feb0..c44e4b938e 100644 --- a/lib/segment/src/index/field_index/full_text_index/immutable_inverted_index.rs +++ b/lib/segment/src/index/field_index/full_text_index/immutable_inverted_index.rs @@ -1,7 +1,6 @@ use std::collections::HashMap; use common::counter::hardware_counter::HardwareCounterCell; -use common::mmap_hashmap::BUCKET_OFFSET_OVERHEAD; use common::types::PointOffsetType; use super::inverted_index::InvertedIndex; @@ -71,6 +70,8 @@ impl InvertedIndex for ImmutableInvertedIndex { let posting_readers: Vec<_> = postings .iter() + // We can safely pass hw_counter here because it's not measured. + // Due to lifetime issues, we can't return a disposable counter. .map(|posting| posting.reader(hw_counter)) .collect(); @@ -81,16 +82,8 @@ impl InvertedIndex for ImmutableInvertedIndex { intersect_compressed_postings_iterator(posting_readers, filter) } - fn get_posting_len( - &self, - token_id: TokenId, - hw_counter: &HardwareCounterCell, - ) -> Option { - let len = self.postings.get(token_id as usize).map(|p| p.len()); - hw_counter - .payload_index_io_read_counter() - .incr_delta(size_of::>()); - len + fn get_posting_len(&self, token_id: TokenId, _: &HardwareCounterCell) -> Option { + self.postings.get(token_id as usize).map(|p| p.len()) } fn vocab_with_postings_len_iter(&self) -> impl Iterator + '_ { @@ -105,7 +98,7 @@ impl InvertedIndex for ImmutableInvertedIndex { &self, parsed_query: &ParsedQuery, point_id: PointOffsetType, - hw_counter: &HardwareCounterCell, + _: &HardwareCounterCell, ) -> bool { if parsed_query.tokens.contains(&None) { return false; @@ -114,6 +107,9 @@ impl InvertedIndex for ImmutableInvertedIndex { if self.values_is_empty(point_id) { return false; } + + let disposed_hw = HardwareCounterCell::disposable(); + // Check that all tokens are in document parsed_query .tokens @@ -121,7 +117,7 @@ impl InvertedIndex for ImmutableInvertedIndex { // unwrap crash safety: all tokens exist in the vocabulary if it passes the above check .all(|query_token| { let postings = &self.postings[query_token.unwrap() as usize]; - postings.reader(hw_counter).contains(point_id) + postings.reader(&disposed_hw).contains(point_id) }) } @@ -142,10 +138,7 @@ impl InvertedIndex for ImmutableInvertedIndex { self.points_count } - fn get_token_id(&self, token: &str, hw_counter: &HardwareCounterCell) -> Option { - hw_counter - .payload_index_io_read_counter() - .incr_delta(BUCKET_OFFSET_OVERHEAD + size_of::()); + fn get_token_id(&self, token: &str, _: &HardwareCounterCell) -> Option { self.vocab.get(token).copied() } } diff --git a/lib/segment/src/index/field_index/full_text_index/inverted_index.rs b/lib/segment/src/index/field_index/full_text_index/inverted_index.rs index 413009547e..9cbd4ef8b8 100644 --- a/lib/segment/src/index/field_index/full_text_index/inverted_index.rs +++ b/lib/segment/src/index/field_index/full_text_index/inverted_index.rs @@ -44,15 +44,11 @@ pub struct ParsedQuery { } impl ParsedQuery { - pub fn check_match(&self, document: &Document, hw_counter: &HardwareCounterCell) -> bool { + pub fn check_match(&self, document: &Document) -> bool { if self.tokens.contains(&None) { return false; } - hw_counter - .payload_index_io_read_counter() - .incr_delta((self.tokens.len() + document.tokens().len()) * size_of::()); - // Check that all tokens are in document self.tokens .iter() diff --git a/lib/segment/src/index/field_index/full_text_index/mmap_inverted_index/mmap_postings.rs b/lib/segment/src/index/field_index/full_text_index/mmap_inverted_index/mmap_postings.rs index 54a998622c..41821fc128 100644 --- a/lib/segment/src/index/field_index/full_text_index/mmap_inverted_index/mmap_postings.rs +++ b/lib/segment/src/index/field_index/full_text_index/mmap_inverted_index/mmap_postings.rs @@ -2,6 +2,7 @@ use std::io; use std::io::Write; use std::path::PathBuf; +use common::counter::conditioned_counter::ConditionedCounter; use common::counter::hardware_counter::HardwareCounterCell; use common::types::PointOffsetType; use common::zeros::WriteZerosExt; @@ -65,6 +66,7 @@ pub struct MmapPostings { _path: PathBuf, mmap: Mmap, header: PostingsHeader, + on_disk: bool, } impl MmapPostings { @@ -121,7 +123,7 @@ impl MmapPostings { chunks, data, remainder_postings, - hw_counter, + ConditionedCounter::new(self.on_disk, hw_counter), )) } @@ -227,6 +229,7 @@ impl MmapPostings { _path: path, mmap, header, + on_disk: !populate, }) } } diff --git a/lib/segment/src/index/field_index/full_text_index/mutable_inverted_index.rs b/lib/segment/src/index/field_index/full_text_index/mutable_inverted_index.rs index a91af78894..399a5de3b9 100644 --- a/lib/segment/src/index/field_index/full_text_index/mutable_inverted_index.rs +++ b/lib/segment/src/index/field_index/full_text_index/mutable_inverted_index.rs @@ -1,7 +1,6 @@ use std::collections::{BTreeSet, HashMap}; use common::counter::hardware_counter::HardwareCounterCell; -use common::mmap_hashmap::BUCKET_OFFSET_OVERHEAD; use common::types::PointOffsetType; use super::inverted_index::InvertedIndex; @@ -147,9 +146,8 @@ impl InvertedIndex for MutableInvertedIndex { fn filter( &self, query: ParsedQuery, - hw_counter: &HardwareCounterCell, + _hw_counter: &HardwareCounterCell, ) -> Box + '_> { - let hw_counter = hw_counter.payload_index_io_read_counter(); let postings_opt: Option> = query .tokens .iter() @@ -159,10 +157,6 @@ impl InvertedIndex for MutableInvertedIndex { // dictionary. Posting list entry can be None but it exists. Some(idx) => { let postings = self.postings.get(idx as usize).unwrap().as_ref(); - hw_counter.incr_delta( - size_of::>() - + postings.map(|i| i.len()).unwrap_or(0) * size_of::(), - ); postings } }) @@ -179,21 +173,12 @@ impl InvertedIndex for MutableInvertedIndex { intersect_postings_iterator(postings) } - fn get_posting_len( - &self, - token_id: TokenId, - hw_counter: &HardwareCounterCell, - ) -> Option { - let len = self - .postings + fn get_posting_len(&self, token_id: TokenId, _: &HardwareCounterCell) -> Option { + self.postings .get(token_id as usize) .and_then(|posting| posting.as_ref()) .as_ref() - .map(|x| x.len()); - hw_counter.payload_index_io_read_counter().incr_delta( - size_of::>() + len.unwrap_or(0) * size_of::(), - ); - len + .map(|x| x.len()) } fn vocab_with_postings_len_iter(&self) -> impl Iterator + '_ { @@ -210,10 +195,10 @@ impl InvertedIndex for MutableInvertedIndex { &self, parsed_query: &ParsedQuery, point_id: PointOffsetType, - hw_counter: &HardwareCounterCell, + _: &HardwareCounterCell, ) -> bool { if let Some(doc) = self.get_doc(point_id) { - parsed_query.check_match(doc, hw_counter) + parsed_query.check_match(doc) } else { false } @@ -232,10 +217,7 @@ impl InvertedIndex for MutableInvertedIndex { self.points_count } - fn get_token_id(&self, token: &str, hw_counter: &HardwareCounterCell) -> Option { - hw_counter - .payload_index_io_read_counter() - .incr_delta(BUCKET_OFFSET_OVERHEAD + size_of::()); + fn get_token_id(&self, token: &str, _hw_counter: &HardwareCounterCell) -> Option { self.vocab.get(token).copied() } }