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

bufio: optimize struct memory layout to reduce size #70391

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/bufio/bufio.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,10 @@ type Reader struct {
lastRuneSize int // size of last rune read for UnreadRune; -1 means invalid
}

const minReadBufferSize = 16
const maxConsecutiveEmptyReads = 100
const (
minReadBufferSize = 16
maxConsecutiveEmptyReads = 100
)

// NewReaderSize returns a new [Reader] whose buffer has at least the specified
// size. If the argument io.Reader is already a [Reader] with large enough
Expand Down Expand Up @@ -575,9 +577,9 @@ func (b *Reader) writeBuf(w io.Writer) (int64, error) {
// the underlying [io.Writer].
type Writer struct {
err error
wr io.Writer
buf []byte
n int
wr io.Writer
}

// NewWriterSize returns a new [Writer] whose buffer has at least the specified
Expand Down
4 changes: 2 additions & 2 deletions src/bufio/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ import (
// on a reader, should use [bufio.Reader] instead.
type Scanner struct {
r io.Reader // The reader provided by the client.
err error // Sticky error.
split SplitFunc // The function to split the tokens.
maxTokenSize int // Maximum size of a token; modified by tests.
token []byte // Last token returned by split.
buf []byte // Buffer used as argument to split.
maxTokenSize int // Maximum size of a token; modified by tests.
start int // First non-processed byte in buf.
end int // End of data in buf.
err error // Sticky error.
empties int // Count of successive empty tokens.
scanCalled bool // Scan has been called; buffer is in use.
done bool // Scan has finished.
Expand Down