-
Notifications
You must be signed in to change notification settings - Fork 29
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
add univ2 InitPoolSimulator to allow caller to re-use sim instance #330
base: main
Are you sure you want to change the base?
Conversation
const NUM_TOKEN = 2 | ||
|
||
func InitPoolSimulator(entityPool entity.Pool, sim *PoolSimulator) error { | ||
if len(entityPool.Tokens) != NUM_TOKEN || len(entityPool.Reserves) != NUM_TOKEN { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just be cautious, we should do nil check with sim
here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! 've added a check below
var extra Extra | ||
if err := json.Unmarshal([]byte(entityPool.Extra), &extra); err != nil { | ||
return err | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we reuse Extra
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes we could (by putting Extra into the sim struct), but in this case it's quite small so I skipped it (so that I don't have to change NewPoolSimulator v1)
(this case is quite annoying, extra
only used inside this function and nothing refer to it after that, but somehow the compiler still decided to moved it to heap instead, the heap analysis show the reason is the json.Unmarshal
call, but I'm still not sure why)
InitPoolSimulator
take in an instance of pool simulator and fill it instead of allocating new oneBenchmark:
Allocating new simulator every time:
BenchmarkNewPoolSimulator-8 2421100 485.7 ns/op 464 B/op 13 allocs/op
Best case: always re-use the same simulator:
BenchmarkNewPoolSimulatorV2-8 8776808 135.8 ns/op 48 B/op 2 allocs/op
Why did we need it?
Related Issue
Release Note
How Has This Been Tested?
Screenshots (if appropriate):