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

remove some of the reliance on the exp pkg #2405

Conversation

poopoothegorilla
Copy link
Contributor

@poopoothegorilla poopoothegorilla commented Dec 1, 2023

Notes:

  • Updates the x/exp pkg

Why this should be merged

  • the signature of the SortFunc has changed in the exp pkg from func(i, j T) bool to func(i, j T) int so upgrading now will prevent issues in the future

How this works

How this was tested

  • should be handled by test suite

utils/sorting.go Outdated
Comment on lines 23 to 26
if a.Less(b) {
return -1
}
return 1
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think this correctly handles the case that a is equal to b

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ill add that case :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

took another look at the code... seems there are a few options

  1. add an addition method to the Sortable interface Equals(T) bool and implement that on all types that have the Less Method implemented
  2. take the slight performance hit of returning a -1, or 1 ... since returning 0 would just skip a swap operation. which is how it currently operates
  3. We change the Less method to be similar to Compare and update that on all implementations

let me know if i might be missing some easier approach and if you have a preference on which i should move forward on

Copy link

Choose a reason for hiding this comment

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

Can we use sort.Slice instead?

func Slice(x any, less func(i, j int) bool)

https://pkg.go.dev/sort#Slice

Copy link

Choose a reason for hiding this comment

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

sort.Slice uses reflection and is slower than slices.SortFunc / slices.Sort

Copy link

Choose a reason for hiding this comment

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

Personally, I like option 3 the best. Something like this seems reasonable:

type Sortable[T any] interface {
	Compare(T) int
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

looks like we will have to keep using the exp pkg here... i thought that the slices update was in 1.20. looks like its in 1.21. I will update that and just move forward with option 3

Copy link
Contributor

Choose a reason for hiding this comment

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

I had originally thought we would just do:

if a.Less(b) {
	return -1
}
if b.Less(a) {
	return 1
}
return 0

But changing the Sortable interface is a better long term strategy imo.

Copy link
Contributor

Choose a reason for hiding this comment

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

As for the go version change - we likely won't update to 1.21 until 1.22 is released

Copy link

Choose a reason for hiding this comment

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

It is trivial to implement sort.Interface:

type sortables[T Sortable[T]] []T

func (s *sortables[T]) Len() int           { return len(*s) }
func (s *sortables[T]) Less(i, j int) bool { return (*s)[i].Less((*s)[j]) }
func (s *sortables[T]) Swap(i, j int)      { (*s)[i], (*s)[j] = (*s)[j], (*s)[i] }

Then you can just cast and call: sort.Sort(sortables(s))

@poopoothegorilla
Copy link
Contributor Author

poopoothegorilla commented Dec 1, 2023

this seems like it might be a bigger task

./../go/pkg/mod/github.com/ava-labs/coreth@v0.12.9-rc.5/plugin/evm/atomic_tx_repository.go:276:28: type func(i *Tx, j *Tx) bool of func(i, j *Tx) bool {…} does not match inferred type func(a *Tx, b *Tx) int for func(a E, b E) int
../../go/pkg/mod/github.com/ava-labs/coreth@v0.12.9-rc.5/plugin/evm/import_tx.go:119:37: type func(i EVMOutput, j EVMOutput) bool of func(i, j EVMOutput) bool {…} does not match inferred type func(a EVMOutput, b EVMOutput) int for func(a E, b E) int
../../go/pkg/mod/github.com/ava-labs/coreth@v0.12.9-rc.5/plugin/evm/tx.go:270:28: type func(i *Tx, j *Tx) bool of func(i, j *Tx) bool {…} does not match inferred type func(a *Tx, b *Tx) int for func(a E, b E) int

@StephenButtolph
Copy link
Contributor

this seems like it might be a bigger task

I can take a stab at updating the golang.org/x/exp version if you (@poopoothegorilla) wouldn't mind. The interaction between coreth here and avalanchego is pretty annoying

@poopoothegorilla
Copy link
Contributor Author

this seems like it might be a bigger task

I can take a stab at updating the golang.org/x/exp version if you (@poopoothegorilla) wouldn't mind. The interaction between coreth here and avalanchego is pretty annoying

That is fine with me. Just for context this was preventing some upgrades

@StephenButtolph
Copy link
Contributor

Closing this in favor of: #2424. Thanks for suggesting this!

@poopoothegorilla poopoothegorilla deleted the jtw/remove-exp-slices-req branch December 5, 2023 14:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

4 participants