Skip to content

Commit

Permalink
perf: use buffered output
Browse files Browse the repository at this point in the history
  • Loading branch information
adrienaury committed Sep 29, 2023
1 parent 1de4564 commit 8657822
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 13 deletions.
24 changes: 22 additions & 2 deletions internal/infra/datarowreader_jsonline.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ import (
"github.com/cgi-fr/mimo/pkg/mimo"
)

const linebreak byte = 10

type DataRowReaderJSONLine struct {
input *bufio.Scanner
output *bufio.Writer
Expand Down Expand Up @@ -72,8 +74,10 @@ func (drr *DataRowReaderJSONLine) ReadDataRowAndWrite() (mimo.DataRow, error) {
var data mimo.DataRow

if drr.input.Scan() {
if _, err := drr.output.Write(append(drr.input.Bytes(), '\n')); err != nil {
return nil, fmt.Errorf("%w", err)
if drr.output != nil {
if err := drr.writeLine(); err != nil {
return nil, err
}
}

data = mimo.DataRow{}
Expand All @@ -93,6 +97,22 @@ func (drr *DataRowReaderJSONLine) ReadDataRowAndWrite() (mimo.DataRow, error) {
return data, nil
}

func (drr *DataRowReaderJSONLine) writeLine() error {
if _, err := drr.output.Write(drr.input.Bytes()); err != nil {
return fmt.Errorf("%w", err)
}

if err := drr.output.WriteByte(linebreak); err != nil {
return fmt.Errorf("%w", err)
}

return nil
}

func (drr *DataRowReaderJSONLine) Flush() error {
if drr.output == nil {
return nil
}

return fmt.Errorf("%w", drr.output.Flush())
}
14 changes: 3 additions & 11 deletions pkg/mimo/benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
package mimo_test

import (
"io"
"os"
"testing"

"github.com/cgi-fr/mimo/internal/infra"
Expand All @@ -35,13 +33,11 @@ func BenchmarkInMemory(b *testing.B) {
b.FailNow()
}

file, err := os.Open("testdata/masked.jsonl")
maskedReader, err := infra.NewDataRowReaderJSONLineFromFile("testdata/masked.jsonl")
if err != nil {
b.FailNow()
}

maskedReader := infra.NewDataRowReaderJSONLine(file, io.Discard)

driver := mimo.NewDriver(
realReader,
maskedReader,
Expand Down Expand Up @@ -76,13 +72,11 @@ func BenchmarkOnDisk(b *testing.B) {
b.FailNow()
}

file, err := os.Open("testdata/masked.jsonl")
maskedReader, err := infra.NewDataRowReaderJSONLineFromFile("testdata/masked.jsonl")
if err != nil {
b.FailNow()
}

maskedReader := infra.NewDataRowReaderJSONLine(file, io.Discard)

driver := mimo.NewDriver(
realReader,
maskedReader,
Expand Down Expand Up @@ -123,13 +117,11 @@ func BenchmarkAllOptions(b *testing.B) {
b.FailNow()
}

file, err := os.Open("testdata/single-100-2.jsonl")
maskedReader, err := infra.NewDataRowReaderJSONLineFromFile("testdata/single-100-2.jsonl")
if err != nil {
b.FailNow()
}

maskedReader := infra.NewDataRowReaderJSONLine(file, io.Discard)

driver := mimo.NewDriver(
realReader,
maskedReader,
Expand Down

0 comments on commit 8657822

Please sign in to comment.