Skip to content

Commit

Permalink
restore byteReader.window()
Browse files Browse the repository at this point in the history
The unary version was added to try to improve the performance of
parseString, but 7 compiler versions on, it doens't seem to pull its
weight. Make the code match the version on the slides for consistency.
  • Loading branch information
davecheney committed Nov 2, 2023
1 parent 00534bc commit c4c2691
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 14 deletions.
4 changes: 2 additions & 2 deletions reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ func (b *byteReader) release(n int) {

// window returns the current window.
// The window is invalidated by calls to release or extend.
func (b *byteReader) window(offset int) []byte {
return b.data[b.offset+offset:]
func (b *byteReader) window() []byte {
return b.data[b.offset:]
}

// tuning constants for byteReader.extend.
Expand Down
4 changes: 2 additions & 2 deletions reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func BenchmarkCountWhitespace(b *testing.B) {

func countWhitespace(br *byteReader) int {
n := 0
w := br.window(0)
w := br.window()
for {
for _, c := range w {
if whitespace[c] {
Expand All @@ -40,6 +40,6 @@ func countWhitespace(br *byteReader) int {
if br.extend() == 0 {
return n
}
w = br.window(0)
w = br.window()
}
}
18 changes: 9 additions & 9 deletions scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ var whitespace = [256]bool{
// -, 0-9 A number
func (s *Scanner) Next() []byte {
s.br.release(s.pos)
w := s.br.window(0)
w := s.br.window()
loop:
for pos, c := range w {
// strip any leading whitespace.
Expand Down Expand Up @@ -92,7 +92,7 @@ loop:
// ensure the number is correct.
s.pos = s.parseNumber(c)
}
return s.br.window(0)[:s.pos]
return s.br.window()[:s.pos]
}

// it's all whitespace, ignore it
Expand All @@ -103,13 +103,13 @@ loop:
// eof
return nil
}
w = s.br.window(0)
w = s.br.window()
goto loop
}

func validateToken(br *byteReader, expected string) int {
for {
w := br.window(0)
w := br.window()
n := len(expected)
if len(w) >= n {
if string(w[:n]) != expected {
Expand All @@ -131,7 +131,7 @@ func validateToken(br *byteReader, expected string) int {
// " before the end of the byteReader.
func (s *Scanner) parseString() int {
escaped := false
w := s.br.window(1)
w := s.br.window()[1:]
pos := 0
for {
for _, c := range w {
Expand All @@ -152,7 +152,7 @@ func (s *Scanner) parseString() int {
// EOF.
return 0
}
w = s.br.window(pos + 1)
w = s.br.window()[pos+1:]
}
}

Expand All @@ -169,14 +169,14 @@ func (s *Scanner) parseNumber(c byte) int {
)

pos := 0
w := s.br.window(0)
w := s.br.window()
// int vs uint8 costs 10% on canada.json
var state uint8 = begin

// handle the case that the first character is a hyphen
if c == '-' {
pos++
w = s.br.window(1)
w = s.br.window()[1:] // TODO - can we subslice w here?
}

for {
Expand Down Expand Up @@ -256,7 +256,7 @@ func (s *Scanner) parseNumber(c byte) int {
return 0
}
}
w = s.br.window(pos)
w = s.br.window()[pos:]
}
}

Expand Down
2 changes: 1 addition & 1 deletion scanner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ func BenchmarkParseNumber(b *testing.B) {
},
}
scanner.br.extend()
n := scanner.parseNumber(scanner.br.window(0)[0])
n := scanner.parseNumber(scanner.br.window()[0])
if n != len(tc) {
b.Fatalf("expected: %v, got: %v", len(tc), n)
}
Expand Down

0 comments on commit c4c2691

Please sign in to comment.