-
Notifications
You must be signed in to change notification settings - Fork 541
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
[Compatibility] Added LCS command #843
base: main
Are you sure you want to change the base?
[Compatibility] Added LCS command #843
Conversation
Nicely done!! |
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.
Great contribution! This is a gnarly one to implement 👍
A few initial comments... Let me know if you need any further guidance.
libs/server/Resp/ArrayCommands.cs
Outdated
} | ||
else if (option.EqualsUpperCaseSpanIgnoringCase(CmdStrings.MINMATCHLEN)) | ||
{ | ||
if (!parseState.TryGetInt(tokenIdx++, out var minLen) || minLen < 0) |
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.
Doesn't look like there's an issue with setting minLen to a non-positive number, it just doesn't do anything
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.
It might be a miss from Redis side. Personally, I don't think we should inherit their flaws (Provided this is a flaw from Redis). Do you want to remove this check?
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.
I agree with you 100%, I'm just wondering if this might break any existing clients that rely on this logic for some reason...
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.
Done
{ | ||
var m = str1.Length; | ||
var n = str2.Length; | ||
var dp = new int[m + 1, n + 1]; |
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.
As you're only using the current and previous row, it's sufficient to hold 2 1D arrays.
In general, it is not a good idea to use multidimensional arrays, see: https://stackoverflow.com/questions/597720/differences-between-a-multidimensional-array-and-an-array-of-arrays
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.
I tried with 2D array first, but when using 2 1D arrays I am getting wrong results compared to Redis because it can't maintain the direction when backtracking. This implementation is inspired from https://www.geeksforgeeks.org/longest-common-subsequence-dp-4/. What I can do is that if we just need length (lenOnly option) then I can use 2D array, for others we need multidimensional arrays. I tried a lot with 2 1D arrays, I am pretty sure we can't use it when we need actual LCS back or its indices.
Also, modern .Net has comparable performance between multidimensional and array of arrays. Source: https://stackoverflow.com/a/63031036/7331395 (Will run it locally and test)
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.
Verified the valkey code that also uses multi-dimensional array with regressive calls, I think that might be less performant than our implementation. The space complexity of LCS is O(m * n)
, unless we come up with a new algorithm to find LCS, I don't think we can have lower space complexity.
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.
Link to the code?
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.
Here: They have 1D array with m * n
items using regressive calls to populate it
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.
Let's perf test this as Badrish suggested, we can compare with valkey for funsies
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.
I tested by adding BDN benchmark, [m,n]
, [m][n]
, [m*n]
doesn't make any noticeable difference
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.
Any update on this?
@TalZaccai Fixed review command with an open question |
Might be a good idea to perf test these commands, varying #threads:
another example:
|
I don't think I have proper setup to run Data Load
LCS
GET
@badrishc @TalZaccai Provided the inconsistency with my setup of Feel free to guide me in the right direction if I am doing something wrong with |
Adding the LCS commands to garnet