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

Improve events heap allocations #27

Merged

Conversation

ValarDragon
Copy link
Member

Description

closes: #XXXX


Before we can merge this PR, please make sure that all the following items have been
checked off. If any of the checklist items are not applicable, please leave them but
write a little note why.

  • Targeted PR against correct branch (see CONTRIBUTING.md)
  • Linked to Github issue with discussion and accepted design OR link to spec that describes this work.
  • Code follows the module structure standards.
  • Wrote unit and integration tests
  • Updated relevant documentation (docs/) or specification (x/<module>/spec/)
  • Added relevant godoc comments.
  • Added a relevant changelog entry to the Unreleased section in CHANGELOG.md
  • Re-reviewed Files changed in the Github PR explorer
  • Review Codecov Report in the comment section below once CI passes

@@ -178,7 +191,7 @@ func (a Attribute) String() string {

// ToKVPair converts an Attribute object into a Tendermint key/value pair.
func (a Attribute) ToKVPair() abci.EventAttribute {
return abci.EventAttribute{Key: toBytes(a.Key), Value: toBytes(a.Value)}
return abci.EventAttribute{Key: []byte(a.Key), Value: []byte(a.Value)}
Copy link
Member Author

@ValarDragon ValarDragon Sep 2, 2021

Choose a reason for hiding this comment

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

This had a signficant impact on performance!

However its still silly that we have to keep re-allocating heap space for the keys, when ~all the time, they are actually constants.

I think we should change the data-backend here to be using []byte, at least until abci events use strings.

sdk.NewAttribute(types.AttributeKeySender, fromAddr.String()),
sdk.NewAttribute(sdk.AttributeKeyAmount, amt.String()),
))
ctx.EventManager().EmitEvent(sdk.NewEvent(
Copy link
Member Author

Choose a reason for hiding this comment

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

Kind of annoying, but this also actually improved performance of the entire benchmark by a couple percent, and moreso the event sub-set of it. It avoids extra heap allocations on the argument, and then avoids a slice copy, instead you just append a stack element.

Copy link
Member

Choose a reason for hiding this comment

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

Hmm is the extra EventTypeMessage actually used anywhere? I assume that's a general event emitted on basically all transactions that have a sender. It definitely seems like it makes more sense to just have the more specific EventTypeTransfer here, but are we sure no one is indexing / searching by the more general type?

Copy link
Member

Choose a reason for hiding this comment

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

Oh wait, you're not actually getting rid of it lol, just separating it into two EmitEvents. The diff got cut off lol.

Copy link
Member Author

Choose a reason for hiding this comment

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

ah woops, I should've copied more of the diff in the comment

Copy link
Member

@UnityChaos UnityChaos left a comment

Choose a reason for hiding this comment

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

LGTM 👍

@@ -210,15 +223,9 @@ func (e Events) ToABCIEvents() []abci.Event {
return res
}

func toBytes(i interface{}) []byte {
switch x := i.(type) {
case []uint8:
Copy link
Member

Choose a reason for hiding this comment

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

I assume this case is never actually used?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, and it was bad to be casting to interface and then reflecting.

sdk.NewAttribute(types.AttributeKeySender, fromAddr.String()),
sdk.NewAttribute(sdk.AttributeKeyAmount, amt.String()),
))
ctx.EventManager().EmitEvent(sdk.NewEvent(
Copy link
Member

Choose a reason for hiding this comment

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

Hmm is the extra EventTypeMessage actually used anywhere? I assume that's a general event emitted on basically all transactions that have a sender. It definitely seems like it makes more sense to just have the more specific EventTypeTransfer here, but are we sure no one is indexing / searching by the more general type?

sdk.NewAttribute(types.AttributeKeySender, fromAddr.String()),
sdk.NewAttribute(sdk.AttributeKeyAmount, amt.String()),
))
ctx.EventManager().EmitEvent(sdk.NewEvent(
Copy link
Member

Choose a reason for hiding this comment

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

Oh wait, you're not actually getting rid of it lol, just separating it into two EmitEvents. The diff got cut off lol.

@ValarDragon ValarDragon merged commit 637af8c into v0.42.9-osmo-v2-upgrade Sep 2, 2021
sunnya97 pushed a commit that referenced this pull request Nov 17, 2021
* Significantly lower number of heap allocations that should be required by events

* Use EmitEvent instead of EmitEvents
sunnya97 pushed a commit that referenced this pull request Nov 17, 2021
* Significantly lower number of heap allocations that should be required by events

* Use EmitEvent instead of EmitEvents
ValarDragon added a commit that referenced this pull request Nov 19, 2021
* Significantly lower number of heap allocations that should be required by events

* Use EmitEvent instead of EmitEvents
sunnya97 pushed a commit that referenced this pull request Jan 24, 2022
* Significantly lower number of heap allocations that should be required by events

* Use EmitEvent instead of EmitEvents
@alexanderbez alexanderbez deleted the improve_events_heap_allocations branch March 30, 2022 13:57
faddat pushed a commit that referenced this pull request Jan 18, 2023
* Significantly lower number of heap allocations that should be required by events

* Use EmitEvent instead of EmitEvents
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.

None yet

2 participants