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

feat/btree: add BTree with scale #3536

Closed
wants to merge 7 commits into from
Closed

Conversation

kanishkatn
Copy link
Contributor

@kanishkatn kanishkatn commented Oct 13, 2023

Changes

Adds BTree with scale encoding and decoding apis by creating a wrapper around the tidwall's library.

Tests

go test pkg/scale

Issues

Closes #3480

Primary Reviewer

@timwu20

@codecov
Copy link

codecov bot commented Oct 13, 2023

Codecov Report

Attention: 46 lines in your changes are missing coverage. Please review.

Comparison is base (619be1b) 50.18% compared to head (5615c8a) 50.21%.
Report is 24 commits behind head on development.

Additional details and impacted files
@@               Coverage Diff               @@
##           development    #3536      +/-   ##
===============================================
+ Coverage        50.18%   50.21%   +0.02%     
===============================================
  Files              229      230       +1     
  Lines            28595    28692      +97     
===============================================
+ Hits             14350    14407      +57     
- Misses           12721    12748      +27     
- Partials          1524     1537      +13     

Copy link
Member

@edwardmack edwardmack left a comment

Choose a reason for hiding this comment

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

I'm concerned about adding dependency on "github.com/tidwall/btree" to scale package, I think they is a way to define scale encoding/decoding outside of the scale package.

pkg/scale/btree.go Outdated Show resolved Hide resolved
Copy link
Contributor

@timwu20 timwu20 left a comment

Choose a reason for hiding this comment

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

I'm curious as to where we need the btree.BTree? We utilize btree.Map pretty extensively in feat/grandpa.

@kanishkatn
Copy link
Contributor Author

I'm curious as to where we need the btree.BTree? We utilize btree.Map pretty extensively in feat/grandpa.

Have you seen #3314?

@kanishkatn
Copy link
Contributor Author

I'm concerned about adding dependency on "github.com/tidwall/btree" to scale package, I think they is a way to define scale encoding/decoding outside of the scale package.

I looked into moving BTree outside the scale package; in order to do that, we'll have to export encodeState, decodeState and fieldScaleIndicesCache.

Could you elaborate on your concerns?

pkg/scale/decode.go Outdated Show resolved Hide resolved
pkg/scale/btree.go Outdated Show resolved Hide resolved
@kanishkatn kanishkatn force-pushed the feat/k/btree-scale branch 2 times, most recently from 7fe033a to 2a0db41 Compare October 26, 2023 10:08
@edwardmack
Copy link
Member

edwardmack commented Oct 26, 2023

I'm concerned about adding dependency on "github.com/tidwall/btree" to scale package, I think they is a way to define scale encoding/decoding outside of the scale package.

I looked into moving BTree outside the scale package; in order to do that, we'll have to export encodeState, decodeState and fieldScaleIndicesCache.

Could you elaborate on your concerns?

It's just that this is the first time (that I'm aware of) that we've added outside packages to the scale package, so that piqued my curiosity. I was more curious than concerned and now I have a better understanding.

@jimjbrettj
Copy link
Contributor

I'm curious as to where we need the btree.BTree? We utilize btree.Map pretty extensively in feat/grandpa.

Have you seen #3314?

@kanishkatn Could you please elaborate more on this? While I see that you use the btree.BTree I do not see any scale interactions with respect to btree.BTree so I am still left with the question of where we need scale to have btree.BTree support.

pkg/scale/decode.go Outdated Show resolved Hide resolved
@kanishkatn
Copy link
Contributor Author

kanishkatn commented Oct 26, 2023

I'm curious as to where we need the btree.BTree? We utilize btree.Map pretty extensively in feat/grandpa.

Have you seen #3314?

@kanishkatn Could you please elaborate more on this? While I see that you use the btree.BTree I do not see any scale interactions with respect to btree.BTree so I am still left with the question of where we need scale to have btree.BTree support.

Here's another one: #3344. It has a custom encoding right now; I might look into changing it once I implement pebbleDB.
I think that's about it since I recently switched to the Map in a few structs.

I hope that helps!

pkg/scale/decode.go Outdated Show resolved Hide resolved
pkg/scale/decode.go Outdated Show resolved Hide resolved
pkg/scale/btree.go Outdated Show resolved Hide resolved
Copy link
Contributor

@jimjbrettj jimjbrettj left a comment

Choose a reason for hiding this comment

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

Looks good to me, just have some questions and nits

@jimjbrettj
Copy link
Contributor

I am going to change the primary reviewer of this PR to @timwu20. Given that Tim is the author of this package and has the most knowledge of it I would like to double-check with him before merging this in.

@jimjbrettj jimjbrettj assigned timwu20 and unassigned timwu20 Oct 26, 2023
Copy link
Contributor

@timwu20 timwu20 left a comment

Choose a reason for hiding this comment

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

I think we should take a different approach to supporting the tidwall/btree package. I propose implementing custom unmarshalling/marshalling support in pkg/scale (#3558) first, and then creating a thin wrapper around the tidwall/btree package that implements the Unmarshaller and Marshaller interfaces for the types that we need. We should make it clear that we're just doing this to support SCALE for the btree types. This should also reduce the changes required within scale to support the btree types.

@kanishkatn kanishkatn changed the title pkg/scale: add btree feat/btree: add BTree with scale Dec 15, 2023
@kanishkatn
Copy link
Contributor Author

I've updated this to use the changes from #3617

Comment on lines +100 to +103
type Map[K constraints.Ordered, V any] struct {
*btree.Map[K, V]
Degree int
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Why do we need to hold *btree.Map in a struct with the Degree? I was expecting this Map to just be btree.Map which reflects the same interface with the added MarshalSCALE and UnmarshalSCALE methods. I took a stab at what I was expecting in this branch.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It is so that the user can set the degree beforehand and not worry about filtering the data after unmarshal.

Copy link
Contributor Author

@kanishkatn kanishkatn Jan 10, 2024

Choose a reason for hiding this comment

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

Let me rephrase. It gives the option for the user to set the Degree. One usecase that I see it supporting is, if there are 100 entries in the db and I care about only top 10, I can set that during the creation.

The usecase leads to lesser memory usage.

Copy link
Contributor Author

@kanishkatn kanishkatn Jan 10, 2024

Choose a reason for hiding this comment

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

But yeah, I guess I don't need to store it.

I'm not familiar with your implementation style, feels like a lot of repetitive code.

// at the time of decoding.
type Tree struct {
*btree.BTree
Comparator func(a, b interface{}) bool
Copy link
Contributor

Choose a reason for hiding this comment

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

Not sure why you need this attribute given it's not used in any of the MarshalSCALE or UnmarshalSCALE methods.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Please find the follow up questions below to get a better understanding.

Do you mean the comparator? Is the comment not descriptive enough?

@q9f q9f added A-stale issue or PR is deprecated and needs to be closed. S-scale issues related to the pkg/scale package. labels Jan 16, 2024
@q9f q9f closed this Jan 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-stale issue or PR is deprecated and needs to be closed. S-scale issues related to the pkg/scale package.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add btree support to scale
6 participants