Skip to content

Wrap Voyager C++ headers in the voyager namespace (fixes #52) - #138

Open
Sanaullah-Turab wants to merge 3 commits into
spotify:mainfrom
Sanaullah-Turab:fix/52-cpp-namespace-wrap
Open

Sanaullah-Turab wants to merge 3 commits into
spotify:mainfrom
Sanaullah-Turab:fix/52-cpp-namespace-wrap

Conversation

@Sanaullah-Turab

Copy link
Copy Markdown

Description

Voyager's C++ headers were declared in the global namespace instead of a Voyager specific one, which is a problem for anyone wanting to use the C++ library standalone since it risks symbol collisions with other code. This PR wraps all remaining Voyager specific headers in namespace voyager { ... }, following the pattern already used in Metadata.h from an earlier PR, without touching the vendored hnswlib code.

Related Issues

Closes #52

Changes Made

C++

Wrapped the following files in namespace voyager:

  • cpp/src/Enums.h
  • cpp/src/std_utils.h
  • cpp/src/array_utils.h
  • cpp/src/E4M3.h
  • cpp/src/StreamUtils.h
  • cpp/src/Index.h
  • cpp/src/TypedIndex.h

hnswlib.h, hnswalg.h, visited_list_pool.h, and Spaces/*.h were left untouched since they are vendored and already scoped under namespace hnswlib.

Updated cpp/test/test_main.cpp to qualify Index, SpaceType, StorageDataType, Metadata, and related test utilities with voyager::.

One deviation worth flagging for review: the vendored hnswalg.h uses the custom InputStream/OutputStream types that just moved into namespace voyager, and I didn't want to edit vendored files to fix this. Instead I added a small alias block at the end of StreamUtils.h:

namespace hnswlib {
  using voyager::InputStream;
  using voyager::OutputStream;
  using voyager::FileOutputStream;
  using voyager::readBinaryPOD;
  using voyager::writeBinaryPOD;
}

This lets hnswalg.h resolve the types inside its own namespace with zero edits to vendored code. Open to a different approach here if maintainers prefer something else.

Also fixed an unrelated small bug I hit along the way: TypedIndex.h had IndexCannotBeShrunkError and IndexFullError incorrectly prefixed with hnswlib::, when those exceptions are actually defined in the global namespace. Removed the incorrect prefix so they resolve correctly.

Python

Updated python/src/bindings.cpp to qualify Index, SpaceType, StorageDataType, Metadata, InputStream, OutputStream, and related utilities with voyager::.

Updated python/src/PythonInputStream.h and python/src/PythonOutputStream.h so their base classes are voyager::InputStream and voyager::OutputStream.

Also fixed a small unrelated bug in tox.ini where tox -e format was running ruff format --diff, which only checks formatting instead of applying it. Removed the --diff flag so the format command actually formats the code.

Java

No changes.

Testing

C++:

  • Ran make check-formatting and make format (clang-format v16)
  • Built and ran make test via make VoyagerTests
  • 3/3 tests passed

Python:

  • Ran tox -e format to apply formatting
  • Ran tox to build the native extension and run the full test suite
  • 5,351 tests passed in around 58 seconds

Checklist

  • My code follows the code style of this project.
  • I have added and/or updated appropriate documentation (if applicable).
  • All new and existing tests pass locally with these changes.
  • I have run static code analysis (if available) and resolved any issues.
  • I have considered backward compatibility (if applicable).
  • I have confirmed that this PR does not introduce any security vulnerabilities.

Additional Comments

The maintainers previously noted they don't guarantee C++ interface compatibility across versions for this exact reason, so this change is expected and welcome rather than a breaking surprise. Happy to adjust the StreamUtils.h alias approach if there's a preferred way to bridge the vendored hnswlib code with the new namespace.

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.

cpp wrappers are using default namespace

1 participant