Skip to content

Add fix to reduce memory and cpu consumption #4690

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

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

ankur22
Copy link
Contributor

@ankur22 ankur22 commented Apr 14, 2025

What?

Using a single bytes.Buffer across the lifespan of the connection when reading from the websocket.

Why?

By reducing how much memory it uses as well as how often it is allocated, we should also reduce the cpu usage since GC will have an easier time cleaning up.

Checklist

  • I have performed a self-review of my code.
  • I have commented on my code, particularly in hard-to-understand areas.
  • I have added tests for my changes.
  • I have run linter and tests locally (make check) and all pass.

Checklist: Documentation (only for k6 maintainers and if relevant)

Please do not merge this PR until the following items are filled out.

  • I have added the correct milestone and labels to the PR.
  • I have updated the release notes: link
  • I have updated or added an issue to the k6-documentation: grafana/k6-docs#NUMBER if applicable
  • I have updated or added an issue to the TypeScript definitions: grafana/k6-DefinitelyTyped#NUMBER if applicable

Related PR(s)/Issue(s)

By reducing how much memory it uses as well as how often it is
allocated, we should also reduce the cpu usage since GC will have an
easier time cleaning up.
@ankur22 ankur22 requested a review from a team as a code owner April 14, 2025 16:03
@ankur22 ankur22 requested review from oleiade and codebien and removed request for a team April 14, 2025 16:03
@ankur22 ankur22 marked this pull request as draft April 14, 2025 16:03
Copy link
Member

@oleiade oleiade left a comment

Choose a reason for hiding this comment

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

🚀

@codebien
Copy link
Contributor

@ankur22 I see this pull request being marked as draft, but there was a review request at the same time? Can you clarify your expectation, please?

In addition, I don't see a pool used here, it sounds you probably want to introduce one, to maximize the effort. Did you consider it?

@ankur22
Copy link
Contributor Author

ankur22 commented Apr 15, 2025

@ankur22 I see this pull request being marked as draft, but there was a review request at the same time? Can you clarify your expectation, please?

@codebien and @oleiade sorry for the noise! This is indeed a draft PR... i'm not sure how to open one without it auto assigning reviewers.

In addition, I don't see a pool used here, it sounds you probably want to introduce one, to maximize the effort. Did you consider it?

Good shout, do you have any material you can point me to so that I can understand the implementation of a pool of memory?

EDIT: I think you're referring to sync.Pool. Sounds promising, i'll give it a go 👍

@ankur22 ankur22 removed the request for review from codebien April 15, 2025 08:31
@codebien
Copy link
Contributor

not sure how to open one without it auto assigning reviewers

@ankur22 If you open it directly as a draft, then reviewers are not assigned. They will be when you turn it into a Ready for review pull request.

EDIT: I think you're referring to sync.Pool. Sounds promising, i'll give it a go 👍

Yes, my proposal was around using sync.Pool for the buffers to limit the allocation and reduce GC.

// Clone the bytes from the recvBuffer to buf before unmarshaling
// the message. Without this, the unmarshaling will fail as it
// reuses the buf as the underlying bytes buffer.
buf := bytes.Clone(buffer.Bytes())
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@codebien thanks for the sync.Pool suggestion. I think there's some promise in going down that root. However, it's not going to be a straight forward change. Since the browser module works with events, it's not straight forward to keep track of all the events and have them put the buffer back in the pool. We'd need to refactor a lot of the codebase to be able to put the underlying buffer back in the pool, either by keeping track of the data or by allowing the handlers of the event to put the buffer back into the pool 😭

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