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 memory usage #110

Merged
merged 1 commit into from
Nov 6, 2020
Merged

Improve memory usage #110

merged 1 commit into from
Nov 6, 2020

Conversation

hannahhoward
Copy link
Collaborator

Goals

Remove some unneccesary mem copies identified in testing. Specifically:

Implementation

  • The go-ipld-prime Storer interface has an unneccesary copy of block bytes due to the need to create a bytes.Buffer to mimic an io.Writer. This creates a shortcut around that and implements it by default for the StorerFromBlockstore method
  • When we switched to google's protobuf library, we didn't implement logic to avoid temporary buffers when writing protobuff message. This implements that using go-buffer-bool (similar to go-bitswap)

@@ -346,12 +347,19 @@ func (gsm *graphSyncMessage) ToProto() (*pb.Message, error) {
}

func (gsm *graphSyncMessage) ToNet(w io.Writer) error {
pbw := ggio.NewDelimitedWriter(w)
Copy link
Member

Choose a reason for hiding this comment

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

You could also consider pooling the writers. That's what we do in the DHT.

Although this also works and is quite clean.

@@ -55,7 +59,11 @@ func (ubs *UnverifiedBlockStore) VerifyBlock(lnk ipld.Link) ([]byte, error) {
if err != nil {
return nil, err
}
_, err = buffer.Write(data)
if settable, ok := buffer.(settableWriter); ok {
Copy link
Member

Choose a reason for hiding this comment

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

This may be a bit too evil, but...

if buf, ok := buffer.(*bytes.Buffer); ok {
  *buf = *bytes.NewBuffer(data)
} else {
  buffer.Write(data)
}

reduce copies of temporary buffers in message write and unverified block store
@hannahhoward hannahhoward force-pushed the feat/memory-improvements branch from 55ed84a to d082f38 Compare November 6, 2020 00:52
@hannahhoward hannahhoward merged commit cc536a0 into master Nov 6, 2020
@aschmahmann aschmahmann mentioned this pull request Feb 18, 2021
73 tasks
@mvdan mvdan deleted the feat/memory-improvements branch December 15, 2021 14:16
marten-seemann pushed a commit that referenced this pull request Mar 2, 2023
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.

2 participants