-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Initial POC commit for adding ReadOnlyMemory overloads to StringSet o… #2221
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -686,6 +686,9 @@ public Task<long> StringLengthAsync(RedisKey key, CommandFlags flags = CommandFl | |
public Task<bool> StringSetAsync(KeyValuePair<RedisKey, RedisValue>[] values, When when = When.Always, CommandFlags flags = CommandFlags.None) => | ||
Inner.StringSetAsync(ToInner(values), when, flags); | ||
|
||
public Task<bool> StringSetAsync(ReadOnlyMemory<KeyValuePair<RedisKey, RedisValue>> values, When when = When.Always, CommandFlags flags = CommandFlags.None) => | ||
Inner.StringSetAsync(ToInner(values), when, flags); | ||
|
||
public Task<bool> StringSetAsync(RedisKey key, RedisValue value, TimeSpan? expiry, When when) => | ||
Inner.StringSetAsync(ToInner(key), value, expiry, when); | ||
public Task<bool> StringSetAsync(RedisKey key, RedisValue value, TimeSpan? expiry, When when, CommandFlags flags) => | ||
|
@@ -804,6 +807,25 @@ protected KeyValuePair<RedisKey, RedisValue> ToInner(KeyValuePair<RedisKey, Redi | |
} | ||
} | ||
|
||
protected ReadOnlyMemory<KeyValuePair<RedisKey, RedisValue>> ToInner(ReadOnlyMemory<KeyValuePair<RedisKey, RedisValue>> outer) | ||
{ | ||
if (outer.Length == 0) | ||
{ | ||
return outer; | ||
} | ||
else | ||
{ | ||
KeyValuePair<RedisKey, RedisValue>[] inner = new KeyValuePair<RedisKey, RedisValue>[outer.Length]; | ||
|
||
for (int i = 0; i < outer.Length; ++i) | ||
{ | ||
inner[i] = ToInner(outer.Span[i]); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Accessing .Span is relatively expensive; this should be done outside the loop and resused. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The ++i is very unusual for C#, note There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Makes sense, thanks! I'll make the changes to cache all call sites for .Span. The ++i was copied from line 801 of the diff. I assumed that was intentional and copied it here. Is that an incorrect usage? |
||
} | ||
|
||
return inner; | ||
} | ||
} | ||
|
||
protected RedisValue ToInner(RedisValue outer) => | ||
RedisKey.ConcatenateBytes(Prefix, null, (byte[]?)outer); | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
possibly worth a warning about FireAndForget being dangerous here if the ReadOnlyMemory is going to be recycled/reused?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Definitely seems wise, thank you. I'll add warnings for all of the new methods taking ReadOnlyMemory. If the approach looks good overall, I'll continue expanding the surface area.