Skip to content

Commit

Permalink
Buffer increase (#738)
Browse files Browse the repository at this point in the history
Increase Scanner buffer size for Stanza reader
  • Loading branch information
strajansebastian authored and smira committed May 14, 2018
1 parent c7a3a10 commit d31144b
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 1 deletion.
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,4 @@ List of contributors, in chronological order:
* Ludovico Cavedon (https://github.com/cavedon)
* Petr Jediny (https://github.com/pjediny)
* Maximilian Stein (https://github.com/steinymity)
* Strajan Sebastian (https://github.com/strajansebastian)
8 changes: 7 additions & 1 deletion deb/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ import (
// Stanza or paragraph of Debian control file
type Stanza map[string]string

// MaxFieldSize is maximum stanza field size in bytes
const MaxFieldSize = 2 * 1024 * 1024

// Canonical order of fields in stanza
// Taken from: http://bazaar.launchpad.net/~ubuntu-branches/ubuntu/vivid/apt/vivid/view/head:/apt-pkg/tagfile.cc#L504
var (
Expand Down Expand Up @@ -214,7 +217,10 @@ type ControlFileReader struct {

// NewControlFileReader creates ControlFileReader, it wraps with buffering
func NewControlFileReader(r io.Reader) *ControlFileReader {
return &ControlFileReader{scanner: bufio.NewScanner(bufio.NewReaderSize(r, 32768))}
scnr := bufio.NewScanner(bufio.NewReaderSize(r, 32768))
scnr.Buffer(nil, MaxFieldSize)

return &ControlFileReader{scanner: scnr}
}

// ReadStanza reeads one stanza from control file
Expand Down
12 changes: 12 additions & 0 deletions deb/format_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package deb
import (
"bufio"
"bytes"
"os"
"strings"

. "gopkg.in/check.v1"
Expand Down Expand Up @@ -135,6 +136,17 @@ func (s *ControlFileSuite) TestCanonicalCase(c *C) {
c.Check(canonicalCase("packaGe-lIst"), Equals, "Package-List")
}

func (s *ControlFileSuite) TestLongFields(c *C) {
f, err := os.Open("long.stanza")
c.Assert(err, IsNil)
defer f.Close()

r := NewControlFileReader(f)
stanza, e := r.ReadStanza(false)
c.Assert(e, IsNil)
c.Assert(len(stanza["Provides"]), Equals, 586929)
}

func (s *ControlFileSuite) BenchmarkReadStanza(c *C) {
for i := 0; i < c.N; i++ {
reader := bytes.NewBufferString(controlFile)
Expand Down
12 changes: 12 additions & 0 deletions deb/long.stanza

Large diffs are not rendered by default.

0 comments on commit d31144b

Please sign in to comment.