-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ft: optimize NewPoolV2 to not do unnecessary checksums (#8)
- Loading branch information
Showing
5 changed files
with
99 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package entities | ||
|
||
import ( | ||
"bytes" | ||
|
||
"github.com/daoleno/uniswap-sdk-core/entities" | ||
) | ||
|
||
// SortsBefore returns true if the address of token a sorts before the address of the token b. | ||
func SortsBefore(a, b *entities.Token) (bool, error) { | ||
if a.ChainId() != b.ChainId() { | ||
return false, entities.ErrDifferentChain | ||
} | ||
if a.Address == b.Address { | ||
return false, entities.ErrSameAddress | ||
} | ||
return bytes.Compare(a.Address[:], b.Address[:]) < 0, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package entities | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/daoleno/uniswap-sdk-core/entities" | ||
"github.com/ethereum/go-ethereum/common" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
// BenchmarkSortsBefore | ||
// BenchmarkSortsBefore/daoleno_SortsBefore | ||
// BenchmarkSortsBefore/daoleno_SortsBefore-16 565605 2026 ns/op | ||
// BenchmarkSortsBefore/KyberSwap_SortsBefore | ||
// BenchmarkSortsBefore/KyberSwap_SortsBefore-16 160554603 7.101 ns/op | ||
func BenchmarkSortsBefore(b *testing.B) { | ||
tokenA := entities.NewToken(1, common.HexToAddress("0xB8c77482e45F1F44dE1745F52C74426C631bDD52"), | ||
18, "BNB", "BNB") | ||
tokenB := entities.NewToken(1, common.HexToAddress("0xb62132e35a6c13ee1ee0f84dc5d40bad8d815206"), | ||
18, "NEXO", "Nexo") | ||
var before bool | ||
var err error | ||
|
||
b.Run("daoleno SortsBefore", func(b *testing.B) { | ||
for i := 0; i < b.N; i++ { | ||
before, err = tokenA.SortsBefore(tokenB) | ||
} | ||
b.StopTimer() | ||
assert.False(b, before) | ||
assert.NoError(b, err) | ||
}) | ||
|
||
b.Run("KyberSwap SortsBefore", func(b *testing.B) { | ||
for i := 0; i < b.N; i++ { | ||
before, err = SortsBefore(tokenA, tokenB) | ||
} | ||
b.StopTimer() | ||
assert.False(b, before) | ||
assert.NoError(b, err) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters