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 defers from getTypeInfo functions #227

Merged
merged 2 commits into from
Oct 15, 2018
Merged

Conversation

ValarDragon
Copy link
Contributor

@ValarDragon ValarDragon commented Sep 18, 2018

These defers were taking up around .5% of the gaia state machines time,
due to their usage within MarshalJSON and UnmarshalBinaryBare. (exact percentage is a bit unclear)

There is no safety concern for removing these defers, as none of
the methods within the functions should panic.

These defers were taking up 1% of the gaia state machines time,
due to their usage within MarshalJSON and UnmarshalBinaryBare.

There is no safety concern for removing these defers, as none of
the methods within the functions should panic.
Copy link
Contributor

@liamsi liamsi left a comment

Choose a reason for hiding this comment

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

You are right that these two defers aren't absolutely necessary. As a note and in general, I believe that we shouldn't do this kind of optimizations. I would assume that very few go-lang versions later this might be optimized at the go runtime level. And even if now this looks like an innocent change because no panic occurs on the way, it's just too easy to forget an explicit Unlock in one of the exec. paths in the future.

I'm still OK with merging this. The performance gain seems worth it (I see the time consumed dropped from 1% total time to 0.5%? Do you have any absolute numbers maybe?)

I think we should add a comment, where the defers were, though. As a "warning" or to be cautious here. And that this is a optimzation that might be unnecessary in the future.
E.g.
// We do not use defer cdc.mtx.Unlock() here as explicitly unlocking the mtx on each return is currently faster (tested with go1.10.?? and go1.11.?). Please, be careful if you change anything below: make sure that the changes won't panic.

After that, we are good to merge.

Referencing related discussions in golang: golang/go#14939
golang/go#6980

@ValarDragon
Copy link
Contributor Author

ValarDragon commented Oct 5, 2018

I agree with adding comments here. I do disagree with your point about generally doing this sort of optimization. It is a significant improvement now. Amino is already slow, and if we want our libraries to gain adoption they should be fast. I do hope defer speed gets fixed in a new version soon!

I can benchmark these functions here to get numbers. Its a bit hard to read these numbers off the overall SDK benchmarks, are is there some fluctuation in the randomized simulations and we can't run long enough to get reliable benchmarks as we fail due to SDK bugs (lol). Hence the discrepancy between the original post and updated, I wanted to have a less alarmist number due to the randomized process returning .5% in one case.

@liamsi
Copy link
Contributor

liamsi commented Oct 12, 2018

In general, I feel there are other bottlenecks here (and in in the software using amino) where we could better spent our time on.

But OK, let's add comments here and merge this. Don't spend additional time getting benchmark numbers on this. Thanks a lot @ValarDragon!

@melekes
Copy link
Contributor

melekes commented Oct 14, 2018

I think we should only remove defers in performance critical code (+ provide benchmarks). If this is one of those cases, then it's fine by me.

@ValarDragon
Copy link
Contributor Author

After signature verification and database queries, amino is the most performance sensitive part of the code as far as the SDK is concerned. The defers in these functions alone accounted for a non-negligible part of the sdk's total time. (Hence they are performance critical)

Copy link
Contributor

@liamsi liamsi left a comment

Choose a reason for hiding this comment

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

Thanks @ValarDragon

@liamsi liamsi merged commit f7a7b0b into develop Oct 15, 2018
@liamsi liamsi deleted the dev/speedup_gettypeinfo branch October 30, 2018 18:10
@liamsi
Copy link
Contributor

liamsi commented Sep 6, 2019

@ValarDragon I know, some time passed etc. But finally: golang/go@fff4f599fe
massive perf. improvements in defer (~30%).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants