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

Clean up open-interest liquidity tier calculations #1592

Merged
merged 3 commits into from
May 29, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions protocol/x/perpetuals/types/liquidity_tier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,28 @@ func TestLiquidityTierGetMaxAbsFundingClampPpm(t *testing.T) {
}
}

func BenchmarkGetInitialMarginQuoteQuantums(b *testing.B) {
Copy link
Contributor

Choose a reason for hiding this comment

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

On darwin/arm64 for reference:
Before

BenchmarkGetInitialMarginQuoteQuantums-10    	 1322443	       907.5 ns/op	     832 B/op	      44 allocs/op

After

BenchmarkGetInitialMarginQuoteQuantums-10    	 2147283	       573.7 ns/op	     600 B/op	      28 allocs/op

Perhaps in the future we can add some workflow that compare these benchmarks on linux/amd64?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There is a workflow to run on amd64 that runs in CI. I added it, but currently only runs on changes to protocol/lib/. I can change it to run on any changes to protocol/. I'm not very worried about negative impact on performance in this PR given that it's significantly better on arm64 but will be good to keep and eye on in the future

openInterestLowerCap := uint64(1_000_000)
openInterestUpperCap := uint64(2_000_000)
openInterestMiddle := (openInterestLowerCap + openInterestUpperCap) / 2
liquidityTier := &types.LiquidityTier{
OpenInterestLowerCap: openInterestLowerCap,
OpenInterestUpperCap: openInterestUpperCap,
InitialMarginPpm: 200_000, // 20%
}
bigQuoteQuantums := big.NewInt(500_000)
oiLower := new(big.Int).SetUint64(openInterestLowerCap)
oiUpper := new(big.Int).SetUint64(openInterestUpperCap)
oiMiddle := new(big.Int).SetUint64(openInterestMiddle)

b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = liquidityTier.GetInitialMarginQuoteQuantums(bigQuoteQuantums, oiLower)
_ = liquidityTier.GetInitialMarginQuoteQuantums(bigQuoteQuantums, oiUpper)
_ = liquidityTier.GetInitialMarginQuoteQuantums(bigQuoteQuantums, oiMiddle)
}
}

func TestGetInitialMarginQuoteQuantums(t *testing.T) {
tests := map[string]struct {
initialMarginPpm uint32
Expand Down
Loading