Repository navigation
Conversation
razdoburdin
left a comment
There was a problem hiding this comment.
Great idea!
It can be even more important for concurrent index. If grow stable storage is used for internal_to_external, it could allow to drop some locks, but it requires additional research.
| // using bucket_type = tsl::detail_robin_hash:: | ||
| // bucket_entry<std::pair<external_id_type, internal_id_type>, false>; | ||
| // external_to_internal_.bucket_count() * sizeof(bucket_type) | ||
| return size() * (sizeof(external_id_type) + sizeof(internal_id_type)) + |
There was a problem hiding this comment.
What's wrong with bucket_count() * sizeof(bucket_type) estimation? It should be ok for trivial types.
|
Would also help filtered search for concurrent index. With a vector, each insert writes only its own slot, but in hash map a collision can move neighbor entries, so the search could read ids without a lock |
…al-to-external mapping - Added methods to estimate memory usage and manage internal capacity. - Updated memory management in MutableVamanaIndex and DynamicFlatIndex to utilize new translator methods. - Improved error handling for invalid external IDs and ensured proper resizing of internal structures. Add concurrency support for IDTranslator::internal_to_external_ map
- Removed unnecessary calls to reserve and shrink in MutableVamanaIndex. - Enhanced memory usage calculations in dynamic_index.cpp to account for translator overhead. - Added new tests for ConcurrentIDTranslator to validate lock-free reads during growth and mutation. - Updated translation tests to use template-based checks for both IDTranslator and ConcurrentIDTranslator. - Implemented additional test cases for replacing stale mappings, handling unmapped IDs, and memory accounting. - Ensured consistent behavior between IDTranslator and ConcurrentIDTranslator during conversions and reloading.
b77a7c5 to
2f1a15b
Compare
razdoburdin
left a comment
There was a problem hiding this comment.
Looks good from my side.
The only comments are style-related and optional to address.
| // hash-map read concurrent with an insert is a data race in the strict sense and a | ||
| // crash in practice: an insert can rehash and free the bucket array a reader is | ||
| // walking. | ||
| // The translator is mutated by `add_points` (insert), `consolidate` (erase), and |
There was a problem hiding this comment.
Please consider rewriting this comments, to make them more compact.
| // Storage and slot access for the internal->external table of ``BasicIDTranslator``. | ||
| // ``load`` yields ``default_val`` for an index beyond the table, and ``store`` fills the | ||
| // slots it has to add below the stored index with its last argument. | ||
| template <typename T, bool Concurrent> struct VectorMapTraits; |
There was a problem hiding this comment.
I think using something like
enum class AccessType {Serial = 0, Concurrent = 1};
would be more clear than pure bool Concurrent.
There was a problem hiding this comment.
This is implementation code inside the detail namespace intended to be used in translation.h only.
Pure bool is simple and enough for the current implementation and considered to refactored in case of making it reusable.
PS. I've also moved the BasicIDTranslator to the detail namespace (with rename to IDTranslatorBase) assuming it's local usage only for
IDTranslator and ConcurrentIDTranslator specializations.
* Compacted comment in dynamic_index.h * Moved BasicIDTranslator template to the detail namespace and renamed to IDTranslatorBase (to follow existing naming style)
This PR includes
IDTranslatorrefactoring using a simple dense table for internal->external mapping.Summary
IDTranslatormaps external IDs (user labels,uint64_t) to internal IDs (slot indices,uint32_t) and back. Onmainboth directions aretsl::robin_maps. This PR replaces the internal-to-external hash map with a table indexed by internal ID, and adds a variant of the translator whose internal-to-external lookups are safe to run without a lock while one writer mutates it.The external-to-internal direction is unchanged: it is still a
tsl::robin_map.What changes
include/svs/core/translation.htemplate <bool Concurrent> class IDTranslatorBaseinsvs::detail, with two aliases:IDTranslator = detail::IDTranslatorBase<false>: the table is astd::vector. Existing code that namessvs::IDTranslatorcompiles unchanged.ConcurrentIDTranslator = detail::IDTranslatorBase<true>: the table is alib::SegmentedVector, which never relocates a slot when it grows, and every slot is read and written throughstd::atomic_ref.get_memory_usage()andstatic estimate_memory_usage(n), so callers no longer compute the translator's footprint from its internals.invalid_external_id(std::numeric_limits<uint64_t>::max()) marks an unmapped slot in the table. It is reserved:insert,insert_translation,replace_stale_and_insertandremap_external_idreject it with anANNExceptionand leave the translator unmodified.has_internal,get_externalandget_external_ormay run without a lock against one writer;Indexes and bindings
concurrent::MutableVamanaIndexholds aConcurrentIDTranslator. Its constructors still accept anIDTranslatorand convert it. Its locking is unchanged: every translator read still takestranslator_mutex_shared. Dropping that lock for internal-to-external reads is left for a follow-up.get_memory_breakdown()in both dynamic Vamana indexes, and the C API dynamic memory estimate, use the new translator methods instead of a hand-written formula.Behavior changes to review
2^64 - 1is reservedmetadata_byteswas2 * n * 12bytes. It is nown * 12plus the table's capacity times 8. The C API estimate goes from 24 to 20 bytes per vector.IDTranslatoris an aliasclass IDTranslator;no longer compiles. There is none in this repository.concurrent::MutableVamanaIndex::view_translator()const ConcurrentIDTranslator&instead ofconst IDTranslator&.size()The on-disk format is unchanged, and both variants read and write it. That matters because the concurrent index saves a
ConcurrentIDTranslatorwhile the shared loader reads anIDTranslator.Tests
tests/svs/core/translation.cppis now a templated test case that runs every section against both variants. New sections cover:remap_internal_idinto a hole and beyond the end of the table;tests/svs/concurrent/translation.cpppreviously duplicated the single-threaded tests. It now holds multi-threaded tests ofConcurrentIDTranslator:std::shared_mutex, which is how the concurrent index uses the translator.concurrent_tsantarget, because the data-race half of the contract is only visible to ThreadSanitizer.