Skip to content

Conversation

jamesryanbell
Copy link

Add reserve parameter to all USearchIndex constructors

This addresses #441

Summary

This PR adds an optional reserve parameter to all three USearchIndex constructors to allow pre-allocation of memory for incoming vectors, improving performance when the expected number of vectors is known in advance.

Changes Made

1. First Constructor (Parameter-based)

  • Before:
    public USearchIndex(MetricKind metricKind, ScalarKind quantization, ulong dimensions, 
                        ulong connectivity = 0, ulong expansionAdd = 0, ulong expansionSearch = 0, 
                        bool multi = false)
  • After:
    public USearchIndex(MetricKind metricKind, ScalarKind quantization, ulong dimensions, 
                        ulong connectivity = 0, ulong expansionAdd = 0, ulong expansionSearch = 0, 
                        bool multi = false, ulong reserve = 0)

2. Second Constructor (IndexOptions-based)

  • Before:
    public USearchIndex(IndexOptions options)
  • After:
    public USearchIndex(IndexOptions options, ulong reserve = 0)
  • Added reserve logic: if (reserve > 0) { this.IncreaseCapacity(reserve); }
  • Added XML documentation for the new parameter

3. Third Constructor (File-based)

  • Before:
    public USearchIndex(string path, bool view = false)
  • After:
    public USearchIndex(string path, bool view = false, ulong reserve = 0)
  • Added reserve logic: if (reserve > 0) { this.IncreaseCapacity(reserve); }
  • Added XML documentation for the new parameter

Benefits

  • Performance: Pre-allocates memory to avoid reallocations during vector insertion
  • Consistency: All constructors now support the same reserve functionality
  • Backward Compatibility: Default value of 0 ensures existing code continues to work

Usage Examples

// Pre-allocate for 1000 vectors
var index1 = new USearchIndex(MetricKind.Cos, ScalarKind.Float32, 128, reserve: 1000);

// Pre-allocate when loading from file
var index2 = new USearchIndex("index.usearch", reserve: 5000);

// Pre-allocate with IndexOptions
var options = new IndexOptions { /* ... */ };
var index3 = new USearchIndex(options, reserve: 2000);

Breaking Changes

None - this is a purely additive change with default parameter values.

@jamesryanbell
Copy link
Author

Hi @ashvardanian, do you know when someone will be able to review this please

@ashvardanian
Copy link
Contributor

Hi @jamesryanbell! Thanks for the PR!

I don't yet see the relationship between the proposed APIs and multi-threading in #441.

Why do we need a reserve argument in multiple methods as opposed to a single reserve method?

@jamesryanbell
Copy link
Author

Hi @ashvardanian, when I try and create an index using multiple threads I run into an issue where the capacity is not increased. This change would allow me to set the capacity before the parallel operation.

I can't resize increase the capacity currently as IncreaseCapacity is a private method

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.

2 participants