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

Do not allocate MessageIDs on the heap #319

Merged
merged 1 commit into from
Jul 10, 2020

Conversation

dferstay
Copy link
Contributor

@dferstay dferstay commented Jul 9, 2020

Passing a function parameter by pointer (or writing a pointer into a
channel) will cause the Go escape analysis to allocate the parameter on
the heap.

This change passes messageID struct instances by value instead of by
pointer; this keeps messageID structs on the stack.

Each message produced or consumed by the library is associated with
a MessageID; keeping instances of the MessageID structure on the stack
reduces heap allocation and associated GC cost.

Signed-off-by: Daniel Ferstay dferstay@splunk.com

Motivation

Reduce the amount of per-Message heap allocation performed by the library

Modifications

This change modifies the Consumer and Producer code paths to pass messageID struct instances by value instead of by
pointer; this keeps the messageID structures on the stack and off of the heap.

Verifying this change

  • Make sure that the change passes the CI checks.

This change is already covered by existing tests, such as:

  • pulsar/consumer_multitopic_test.go
  • pulsar/consumer_partition_test.go
  • pulsar/consumer_regex_test.go
  • pulsar/consumer_test.go
  • pulsar/impl_message_test.go
  • pulsar/negative_acks_tracker_test.go
  • pulsar/producer_test.go
  • pulsar/reader_test.go

Does this pull request potentially affect one of the following parts:

  • Dependencies (does it add or upgrade a dependency): no
  • The public API: no
  • The schema: no
  • The default values of configurations: no
  • The wire protocol: no

Documentation

  • Does this pull request introduce a new feature? no

@dferstay dferstay force-pushed the stack-allocated-message-id branch 2 times, most recently from cced68c to 89fb889 Compare July 9, 2020 04:42
Passing a function parameter by pointer (or writing a pointer into a
channel) will cause the Go escape analysis to allocate the parameter on
the heap.

This change passes messageID struct instances by value instead of by
pointer; this keeps messageID structs on the stack.

Each message produced or consumed by the library is associated with
a MessageID; keeping instances of the MessageID structure on the stack
reduces heap allocation and associated GC cost.

Signed-off-by: Daniel Ferstay <dferstay@splunk.com>
@dferstay dferstay force-pushed the stack-allocated-message-id branch from 89fb889 to a68dff7 Compare July 9, 2020 04:51
@dferstay
Copy link
Contributor Author

dferstay commented Jul 9, 2020

Below is the alloc_space view of a pprof heap profile collected during a large scale test of a high-throughput service that uses pulsar-client-go to send messages to Pulsar. Note that messageID structures account for 6.07% of all allocation; this percentage grows the longer the service runs as messageIDs escape to the heap for every message produced.

message-id-alloc-space

Copy link
Member

@wolfstudy wolfstudy left a comment

Choose a reason for hiding this comment

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

Cool, LGTM +1

@merlimat merlimat merged commit 9065cb7 into apache:master Jul 10, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants