Skip to content

refactor(dynamic_index): IDTranslator by simple vector for internal to external ID translation - #447

Open
rfsaliev wants to merge 3 commits into
mainfrom
rfsaliev/refactor-translator
Open

rfsaliev wants to merge 3 commits into
mainfrom
rfsaliev/refactor-translator

Conversation

@rfsaliev

@rfsaliev rfsaliev commented Oct 22, 2026 •

Copy link
Copy Markdown
Member

This PR includes IDTranslator refactoring using a simple dense table for internal->external mapping.

Summary

IDTranslator maps external IDs (user labels, uint64_t) to internal IDs (slot indices, uint32_t) and back. On main both directions are tsl::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.h

  • The class becomes template <bool Concurrent> class IDTranslatorBase in svs::detail, with two aliases:
    • IDTranslator = detail::IDTranslatorBase<false>: the table is a std::vector. Existing code that names svs::IDTranslator compiles unchanged.
    • ConcurrentIDTranslator = detail::IDTranslatorBase<true>: the table is a lib::SegmentedVector, which never relocates a slot when it grows, and every slot is read and written through std::atomic_ref.
  • An explicit converting constructor builds one variant from the other.
  • New get_memory_usage() and static 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_insert and remap_external_id reject it with an ANNException and leave the translator unmodified.
  • The class documentation states the concurrency contract for the concurrent variant:
    • writers must be serialized by the caller;
    • has_internal, get_external and get_external_or may run without a lock against one writer;
    • every other method reads the hash map and still needs external synchronization with writers.

Indexes and bindings

  • concurrent::MutableVamanaIndex holds a ConcurrentIDTranslator. Its constructors still accept an IDTranslator and convert it. Its locking is unchanged: every translator read still takes translator_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

Change Effect
External ID 2^64 - 1 is reserved Inserting it now throws. Loading a saved translator that contains it fails.
Table is indexed by internal ID It costs 8 bytes per slot up to the largest internal ID ever stored, so it assumes dense internal IDs. It is not shrunk when translations are deleted or the index is compacted.
Reported memory changes The translator part of metadata_bytes was 2 * n * 12 bytes. It is now n * 12 plus the table's capacity times 8. The C API estimate goes from 24 to 20 bytes per vector.
IDTranslator is an alias A downstream forward declaration class IDTranslator; no longer compiles. There is none in this repository.
concurrent::MutableVamanaIndex::view_translator() Returns const ConcurrentIDTranslator& instead of const IDTranslator&.
size() No longer cross-checks the sizes of the two directions in bounds-checking builds; the table has no entry count to compare.

The on-disk format is unchanged, and both variants read and write it. That matters because the concurrent index saves a ConcurrentIDTranslator while the shared loader reads an IDTranslator.

Tests

  • tests/svs/core/translation.cpp is now a templated test case that runs every section against both variants. New sections cover:
    • unmapped internal IDs: a hole inside the table, an ID beyond its end, and a deleted slot;
    • the reserved external ID on every entry point;
    • remap_internal_id into a hole and beyond the end of the table;
    • reusing a deleted internal ID;
    • memory accounting;
    • conversion between the variants in both directions;
    • a save/load round trip after mutation, loaded as both variants.
  • tests/svs/concurrent/translation.cpp previously duplicated the single-threaded tests. It now holds multi-threaded tests of ConcurrentIDTranslator:
    • lock-free reads while the writer grows the table;
    • lock-free reads while the writer renames, deletes, re-inserts and moves translations;
    • both directions under a std::shared_mutex, which is how the concurrent index uses the translator.
  • That file is added to the concurrent_tsan target, because the data-race half of the contract is only visible to ThreadSanitizer.
  • The memory-usage tests of both dynamic Vamana indexes are updated for the new accounting.

@rfsaliev
rfsaliev requested a review from razdoburdin October 22, 2026 16:18

@razdoburdin razdoburdin left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)) +

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's wrong with bucket_count() * sizeof(bucket_type) estimation? It should be ok for trivial types.

@yuejiaointel

yuejiaointel commented Nov 4, 2026 •

Copy link
Copy Markdown
Contributor

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.
@rfsaliev
rfsaliev force-pushed the rfsaliev/refactor-translator branch from b77a7c5 to 2f1a15b Compare November 26, 2026 15:00

@razdoburdin razdoburdin left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good from my side.
The only comments are style-related and optional to address.

Comment thread include/svs/concurrent/dynamic_index.h Outdated
// 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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please consider rewriting this comments, to make them more compact.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

// 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;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think using something like
enum class AccessType {Serial = 0, Concurrent = 1};
would be more clear than pure bool Concurrent.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@rfsaliev
rfsaliev marked this pull request as ready for review November 27, 2026 12:04
* Compacted comment in dynamic_index.h
* Moved BasicIDTranslator template to the detail namespace and renamed to IDTranslatorBase (to follow existing naming style)

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants