Skip to content

Cache: Read to a sized buffer - #3976

Merged
joe-elliott merged 6 commits into
grafana:mainfrom
joe-elliott:less-allocs
Aug 19, 2024
Merged

Cache: Read to a sized buffer#3976
joe-elliott merged 6 commits into
grafana:mainfrom
joe-elliott:less-allocs

Conversation

@joe-elliott

Copy link
Copy Markdown
Collaborator

What this PR does:
Improves the memory performance of the caching middleware by dropping io.ReadAll in favor of reading to a sized buffer.

Also

  • moves the response read functionality into a shared area for future use
  • removes one of our 2 mock caches

Checklist

  • Tests updated
  • Documentation added
  • CHANGELOG.md updated - the order of entries should be [CHANGE], [FEATURE], [ENHANCEMENT], [BUGFIX]
Signed-off-by: Joe Elliott <number101010@gmail.com>
Signed-off-by: Joe Elliott <number101010@gmail.com>
Signed-off-by: Joe Elliott <number101010@gmail.com>
Signed-off-by: Joe Elliott <number101010@gmail.com>
return resp, nil
}

buffer, err := api.ReadBodyToBuffer(resp)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

if maxItemSize is set to 0, then we will always cache, no matter the size.

is my understanding correct?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

should we handle the case where maxItemSize is a negative value??

@electron0zero electron0zero left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

minor comments, rest lgtm :shipit:

Signed-off-by: Joe Elliott <number101010@gmail.com>
Signed-off-by: Joe Elliott <number101010@gmail.com>
@joe-elliott
joe-elliott merged commit 7961fb7 into grafana:main Aug 19, 2024
mattdurham pushed a commit to mattdurham/tempo that referenced this pull request Jun 18, 2026
* Read to a sized buffer

Signed-off-by: Joe Elliott <number101010@gmail.com>

* changelog

Signed-off-by: Joe Elliott <number101010@gmail.com>

* removed sync pool

Signed-off-by: Joe Elliott <number101010@gmail.com>

* comments and correct alloc pattern

Signed-off-by: Joe Elliott <number101010@gmail.com>

* Added max item sz test

Signed-off-by: Joe Elliott <number101010@gmail.com>

* handle negative vals

Signed-off-by: Joe Elliott <number101010@gmail.com>

---------

Signed-off-by: Joe Elliott <number101010@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants