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

Introduce bitMatrix to replace mat.Dense #33

Merged
merged 4 commits into from
Apr 6, 2021
Merged

Conversation

liamsi
Copy link
Member

@liamsi liamsi commented Apr 5, 2021

This PR introduces a structure that replaces mat.Dense and gets rid of the gonum dependency.
Hence, it is half the story of closes #22.

It could use some light fuzzing but given that this is essentially a simple bit-mask I don't think that is urgent.

Can open a separate PR to actually use the structure, or, replace mat.Dense in here.

@liamsi liamsi requested a review from adlerjohn April 5, 2021 20:14
@liamsi liamsi marked this pull request as ready for review April 5, 2021 20:21
bit_matrix.go Show resolved Hide resolved
@liamsi liamsi marked this pull request as draft April 5, 2021 20:33
@liamsi
Copy link
Member Author

liamsi commented Apr 5, 2021

As requested, this PR also replaces gonum now. It would have been nice to measure the impact with benchmarks but we already know the ballpark of improvment (see #22).

@liamsi liamsi marked this pull request as ready for review April 5, 2021 20:48
@liamsi liamsi force-pushed the ismail/use_bit_matirx branch from 2d7373d to 945314b Compare April 5, 2021 20:53
@liamsi liamsi requested a review from evan-forbes April 5, 2021 20:53
@liamsi liamsi force-pushed the ismail/use_bit_matirx branch from 945314b to c1330f1 Compare April 5, 2021 20:55
@liamsi liamsi force-pushed the ismail/use_bit_matirx branch from c1330f1 to 7346cfe Compare April 5, 2021 20:58
Copy link
Member

@adlerjohn adlerjohn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason not to use big.Int as the backing type instead of a []uint64?

bit_matrix.go Outdated Show resolved Hide resolved
bit_matrix.go Outdated Show resolved Hide resolved
bit_matrix.go Outdated Show resolved Hide resolved
@evan-forbes
Copy link
Member

evan-forbes commented Apr 5, 2021

TestBlockRecovery fails sometimes using this branch, but always passes with the version that the lazyledger-core master branch is using. I'm still investigating though. It might just be the test.

func TestFuzzTestBlockRecovery(t *testing.T) {
	ctx, _ := context.WithTimeout(context.Background(), time.Second*10)
	for {
		select {
		case <-ctx.Done():
			return
		default:
			TestBlockRecovery(t)
		}
	}
}
    --- FAIL: TestFuzzTestBlockRecovery/missing_1/2_shares#295 (0.00s)
        read_test.go:220: 
                Error Trace:    read_test.go:220
                Error:          Received unexpected error:
                                failed to solve data square
                Test:           TestFuzzTestBlockRecovery/missing_1/2_shares#295

edit: see below comment, the square was being repaired fine, but an error was being returned along with the properly repaired square.

Comment on lines 206 to 186
if solved {
break
} else if !progressMade {
if !progressMade {
return ErrUnrepairableDataSquare
}
Copy link
Member

@evan-forbes evan-forbes Apr 6, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh I think this is issue for the failing lazyledger-core test mentioned in the other comment, the square is being repaired fine, but an error is being returned. Reverting this change allows for the other tests to pass.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch!! I'll revert these changes in the for-loop here. They are orthogonal to the bit-mask anyways.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 3eca580.
I'll retest with the above test to make sure that this fixed the issue.

Copy link
Member Author

@liamsi liamsi Apr 6, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An alternative could be to change that if-else-if to: if !progressMade && !solved (or !(progressMade || solved)) and make the for loop for !solved. That should be equivalent. I think the for-loop would be easier to grasp directly as "repeat until solved". Currently, you either trust the comment Keep repeating until the square is solved or you have to scroll 105 complex looking lines of code to make sure that this what is indeed happening: https://github.com/lazyledger/rsmt2d/blob/master/extendeddatacrossword.go#L92-L207

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Either way, thanks for finding that issue and for pointing me to the solution even :-)

@liamsi
Copy link
Member Author

liamsi commented Apr 6, 2021

Is there a reason not to use big.Int as the backing type instead of a []uint64?

Is there a good reason to use bigInt instead?

With big.Int can not directly operate on the underlying []uint to operate on the bits and would need to use Bits() which returns almost what we need (and already use). Almost because the underlying "words" used are Word uint and uint's size depends on the platform used: https://golang.org/pkg/builtin/#uint

uint is an unsigned integer type that is at least 32 bits in size.

Then, big.Int is signed which do not need.

Using big.Int is possible but it would just make the code more difficult to read imo.

@@ -16,7 +16,7 @@ type dataSquare struct {

func newDataSquare(data [][]byte, treeCreator TreeConstructorFn) (*dataSquare, error) {
width := int(math.Ceil(math.Sqrt(float64(len(data)))))
if int(math.Pow(float64(width), 2)) != len(data) {
if width*width != len(data) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've sneaked in another unrelated change here: int(math.Pow(float64(width), 2)) is just a complicated way of computing width*width.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Saw that, and approve.

@liamsi liamsi requested a review from musalbas April 6, 2021 11:12
@liamsi
Copy link
Member Author

liamsi commented Apr 6, 2021

can someone with write access approve the PR (or request changes): cc @musalbas @evan-forbes

Copy link
Member

@evan-forbes evan-forbes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

awesome 👍 👍

@liamsi liamsi merged commit 645430d into master Apr 6, 2021
@liamsi liamsi deleted the ismail/use_bit_matirx branch April 6, 2021 13:56
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.

Proposal: use light-weight bitmask instead of gonums's mat.Dense
3 participants