Skip to content

Commit

Permalink
Avoid allocation of the distance cache during deserialization of an e…
Browse files Browse the repository at this point in the history
…xisting graph
  • Loading branch information
theolivenbaum committed Mar 5, 2020
1 parent 4225ce3 commit 6bf5edf
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion Src/HNSW.Net/SmallWorld.cs
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,10 @@ public static SmallWorld<TItem, TDistance> DeserializeGraph(IReadOnlyList<TItem>
// see https://github.com/neuecc/MessagePack-CSharp/pull/663

var parameters = MessagePackSerializer.Deserialize<Parameters>(stream);

//Overwrite previous InitialDistanceCacheSize parameter, so we don't waste time/memory allocating a distance cache for an already existing graph
parameters.InitialDistanceCacheSize = 0;

var world = new SmallWorld<TItem, TDistance>(distance, generator, parameters);
world.Graph.Deserialize(items, stream);
return world;
Expand Down Expand Up @@ -267,7 +271,8 @@ public Parameters()
public bool EnableDistanceCacheForConstruction { get; set; }

/// <summary>
/// Gets or sets a the initial distance cache size
/// Gets or sets a the initial distance cache size.
/// Note: This value is reset to 0 on deserialization to avoid allocating the distance cache for pre-built graphs.
/// </summary>
public int InitialDistanceCacheSize { get; set; }

Expand Down

0 comments on commit 6bf5edf

Please sign in to comment.