Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

.Net: Optimize Redis GetBatchAsync for Performance Improvement #8365

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -141,13 +141,9 @@ public async Task DeleteCollectionAsync(string collectionName, CancellationToken
public async IAsyncEnumerable<MemoryRecord> GetBatchAsync(string collectionName, IEnumerable<string> keys, bool withEmbeddings = false,
[EnumeratorCancellation] CancellationToken cancellationToken = default)
{
foreach (var key in keys)
await foreach (var result in this.InternalGetBatchAsync(collectionName, keys.ToArray(), withEmbeddings, cancellationToken).ConfigureAwait(false))
atiq-bs23 marked this conversation as resolved.
Show resolved Hide resolved
{
var result = await this.InternalGetAsync(collectionName, key, withEmbeddings, cancellationToken).ConfigureAwait(false);
if (result is not null)
{
yield return result;
}
yield return result;
}
}

Expand Down Expand Up @@ -359,6 +355,49 @@ private static RedisKey GetRedisKey(string collectionName, string key)
timestamp: ParseTimestamp((long?)hashEntries.FirstOrDefault(x => x.Name == "timestamp").Value));
}

private async IAsyncEnumerable<MemoryRecord> InternalGetBatchAsync(string collectionName, string[] keys, bool withEmbeddings, [EnumeratorCancellation] CancellationToken cancellationToken)
{
var batch = this._database.CreateBatch();
var tasks = keys.Select(async key =>
{
cancellationToken.ThrowIfCancellationRequested();

HashEntry[] hashEntries = await this._database.HashGetAllAsync(GetRedisKey(collectionName, key), CommandFlags.None).ConfigureAwait(false);
atiq-bs23 marked this conversation as resolved.
Show resolved Hide resolved

if (hashEntries.Length == 0)
{
return null;
}

if (withEmbeddings)
{
RedisValue embedding = hashEntries.FirstOrDefault(x => x.Name == "embedding").Value;
return MemoryRecord.FromJsonMetadata(
json: hashEntries.FirstOrDefault(x => x.Name == "metadata").Value!,
embedding: embedding.HasValue ? MemoryMarshal.Cast<byte, float>((byte[])embedding!).ToArray() : ReadOnlyMemory<float>.Empty,
key: hashEntries.FirstOrDefault(x => x.Name == "key").Value,
timestamp: ParseTimestamp((long?)hashEntries.FirstOrDefault(x => x.Name == "timestamp").Value));
}

return MemoryRecord.FromJsonMetadata(
json: hashEntries.FirstOrDefault(x => x.Name == "metadata").Value!,
embedding: ReadOnlyMemory<float>.Empty,
key: hashEntries.FirstOrDefault(x => x.Name == "key").Value,
timestamp: ParseTimestamp((long?)hashEntries.FirstOrDefault(x => x.Name == "timestamp").Value));
});

batch.Execute();
var results = await Task.WhenAll(tasks).ConfigureAwait(false);

foreach (var result in results)
{
if (result is not null)
{
yield return result;
}
}
}

private double GetSimilarity(Document document)
{
RedisValue vectorScoreValue = document["vector_score"];
Expand Down
Loading