Permalink
Browse files

Buffer stdout (and stderr) for 10x speedup

  • Loading branch information...
benhoyt committed Aug 14, 2018
1 parent ae08b14 commit 60745c3503ba3d99297816f5c7b5364a08ec47ab
Showing with 12 additions and 2 deletions.
  1. +12 −2 interp/interp.go
@@ -55,7 +55,9 @@ func (r returnValue) Error() string {
type Interp struct {
program *Program
output io.Writer
flushOutput bool
errorOutput io.Writer
flushError bool
vars map[string]value
arrays map[string]map[string]value
argc int
@@ -102,11 +104,13 @@ func New(program *Program, output, errorOutput io.Writer) *Interp {
p.program = program

if output == nil {
output = os.Stdout
output = bufio.NewWriterSize(os.Stdout, 64*1024)
p.flushOutput = true
}
p.output = output
if errorOutput == nil {
errorOutput = os.Stderr
errorOutput = bufio.NewWriterSize(os.Stderr, 64*1024)
p.flushError = true
}
p.errorOutput = errorOutput

@@ -171,6 +175,12 @@ func (p *Interp) closeAll() {
for _, cmd := range p.commands {
_ = cmd.Wait()
}
if p.flushOutput {
p.output.(*bufio.Writer).Flush()
}
if p.flushError {
p.errorOutput.(*bufio.Writer).Flush()
}
}

func (p *Interp) execBegin(begin []Stmts) error {

0 comments on commit 60745c3

Please sign in to comment.