Skip to content

Commit

Permalink
internal/format: buffer newlineWriter writes
Browse files Browse the repository at this point in the history
Most writes in the cmd/age Writer stack are chunk-sized, so
approximately 64KiB. However, the newlineWriter, which splits lines at
64 columns, was doing a Write on the underlying Writer for each line,
making chunks effectively 48 bytes (before base64). There is no
buffering underneath it, so it was resulting in a lot of write syscalls.

Add a reusable bytes.Buffer to buffer the output of each
(*newlineWriter).Write call, and Write it all at once on the
destination.

This makes --armor just 50% slower than plain, instead of 10x.

Fixes #167
  • Loading branch information
FiloSottile committed Jan 3, 2021
1 parent 00bffce commit cba1ebc
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 13 deletions.
3 changes: 2 additions & 1 deletion cmd/age/age.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,8 @@ func main() {
}
}

var in, out io.ReadWriter = os.Stdin, os.Stdout
var in io.Reader = os.Stdin
var out io.Writer = os.Stdout
if name := flag.Arg(0); name != "" && name != "-" {
f, err := os.Open(name)
if err != nil {
Expand Down
28 changes: 16 additions & 12 deletions internal/format/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,29 +55,33 @@ func NewlineWriter(dst io.Writer) io.Writer {
type newlineWriter struct {
dst io.Writer
written int
buf bytes.Buffer
}

func (w *newlineWriter) Write(p []byte) (n int, err error) {
func (w *newlineWriter) Write(p []byte) (int, error) {
if w.buf.Len() != 0 {
panic("age: internal error: non-empty newlineWriter.buf")
}
for len(p) > 0 {
remainingInLine := ColumnsPerLine - (w.written % ColumnsPerLine)
if remainingInLine == ColumnsPerLine && w.written != 0 {
if _, err := w.dst.Write([]byte("\n")); err != nil {
return n, err
}
w.buf.Write([]byte("\n"))
}
toWrite := remainingInLine
if toWrite > len(p) {
toWrite = len(p)
}
nn, err := w.dst.Write(p[:toWrite])
n += nn
w.written += nn
p = p[nn:]
if err != nil {
return n, err
}
n, _ := w.buf.Write(p[:toWrite])
w.written += n
p = p[n:]
}
if _, err := w.buf.WriteTo(w.dst); err != nil {
// We always return n = 0 on error because it's hard to work back to the
// input length that ended up written out. Not ideal, but Write errors
// are not recoverable anyway.
return 0, err
}
return n, nil
return len(p), nil
}

const intro = "age-encryption.org/v1\n"
Expand Down

0 comments on commit cba1ebc

Please sign in to comment.