Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@devanbenz
Copy link

@devanbenz devanbenz commented Nov 6, 2025

io.Copy & io.CopyN uses a default window size of 32KB during copy

Copy(N) calls copyBuffer with buf set to nil
https://cs.opensource.google/go/go/+/refs/tags/go1.25.4:src/io/io.go;l=387-389

When buf is nil go will set the default buffer size to 32KB
https://cs.opensource.google/go/go/+/refs/tags/go1.25.4:src/io/io.go;l=418

This PR replaces io.CopyN with io.CopyBuffer in tar.Stream. It also adds a new configuration value tar-stream-buffer-size which can be adjusted to allow for larger or smaller tar buffer sizes to be used during backup operations.

It is suggested to keep the default as is, while testing different buffer sizes to be used with CopyBuffer I found that 1MB was the best performance-wise.

@devanbenz devanbenz marked this pull request as ready for review November 7, 2025 16:58
Comment on lines +170 to +174
# The size of tar buffer window size in bytes while running tar
# streaming operations such as renaming and copying tar files during backups.
# The default value is 1MB. This should only change if backups are having performance issues
# and you understand that this value is a heuristic that may need to be tweaked.
# tar-stream-buffer-size = 1048576
Copy link
Member

Choose a reason for hiding this comment

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

It looks like the tar buffer size is only used in the backup code paths. If this is correct, I wonder if backup-stream-buffer-size would be a better name.

Copy link
Member

@gwossum gwossum left a comment

Choose a reason for hiding this comment

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

io.CopyN did some additional error checking that io.CopyBuffer does not. We should probably do the error checking ourselves now.

Copy link
Member

@gwossum gwossum left a comment

Choose a reason for hiding this comment

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

I think we're throwing away the error now.

if err != nil {
return err
} else if bytesWritten != h.Size {
return fmt.Errorf("error while copying buffer, expected %d bytes but wrote %d", h.Size, bytesWritten)
Copy link
Member

Choose a reason for hiding this comment

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

Maybe add the method the error occurred in so we can track it down if needed.

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