Skip to content

Commit

Permalink
move forks into fork folder
Browse files Browse the repository at this point in the history
  • Loading branch information
awalterschulze committed Feb 4, 2025
1 parent e3d6dad commit 243ea54
Show file tree
Hide file tree
Showing 34 changed files with 236 additions and 184 deletions.
56 changes: 56 additions & 0 deletions json/alloc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,59 @@ func BenchmarkAlloc(b *testing.B) {
}
b.ReportAllocs()
}

func BenchmarkPoolDefault(b *testing.B) {
seed := time.Now().UnixNano()
// generate random jsons
num := 1000
r := rand.New(rand.NewSource(seed))
js := randJsons(r, num)

// initialise pool
jparser := NewParser()

// exercise buffer pool
for i := 0; i < num; i++ {
if err := jparser.Init(js[i%num]); err != nil {
b.Fatalf("seed = %v, err = %v", seed, err)
}
if err := debug.Walk(jparser); err != nil {
b.Fatalf("seed = %v, err = %v", seed, err)
}
}
// start benchmark
b.ResetTimer()
for i := 0; i < b.N; i++ {
if err := jparser.Init(js[i%num]); err != nil {
b.Fatalf("seed = %v, err = %v", seed, err)
}
if err := debug.Walk(jparser); err != nil {
b.Fatalf("seed = %v, err = %v", seed, err)
}
}
b.ReportAllocs()
}

func BenchmarkPoolNone(b *testing.B) {
seed := time.Now().UnixNano()
// generate random jsons
num := 1000
r := rand.New(rand.NewSource(seed))
js := randJsons(r, num)

// set pool to no pool
jparser := NewParser()
jparser.(*jsonParser).pool = pool.None()

// start benchmark
b.ResetTimer()
for i := 0; i < b.N; i++ {
if err := jparser.Init(js[i%num]); err != nil {
b.Fatalf("seed = %v, err = %v", seed, err)
}
if err := debug.Walk(jparser); err != nil {
b.Fatalf("seed = %v, err = %v", seed, err)
}
}
b.ReportAllocs()
}
5 changes: 2 additions & 3 deletions json/debug_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,9 @@ func TestRandomDebug(t *testing.T) {
if err := p.Init(data); err != nil {
t.Fatal(err)
}
//l := debug.NewLogger(p, debug.NewLineLogger())
if err := debug.RandomWalk(p, debug.NewRand(), 10, 3); err != nil {
l := debug.NewLogger(p, debug.NewLineLogger())
if err := debug.RandomWalk(l, debug.NewRand(), 10, 3); err != nil {
t.Fatal(err)
}
//t.Logf("original %v vs random %v", debug.Output, m)
}
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
103 changes: 2 additions & 101 deletions json/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,72 +16,14 @@
package json

import (
"bytes"
"io"

"github.com/katydid/parser-go-json/json/internal/fork/strconv"
"github.com/katydid/parser-go-json/json/internal/fork/unquote"
"github.com/katydid/parser-go-json/json/internal/pool"
"github.com/katydid/parser-go-json/json/internal/strconv"
"github.com/katydid/parser-go-json/json/internal/unquote"
"github.com/katydid/parser-go/parser"
)

func scanString(buf []byte) (int, error) {
escaped := false
udigits := -1
if buf[0] != '"' {
return 0, errScanString
}
for i, c := range buf[1:] {
if escaped {
switch c {
case 'b', 'f', 'n', 'r', 't', '\\', '/', '"':
escaped = false
continue
case 'u':
udigits = 0
escaped = false
continue
}
return 0, errScanString
}
if udigits >= 0 {
if '0' <= c && c <= '9' || 'a' <= c && c <= 'f' || 'A' <= c && c <= 'F' {
udigits++
} else {
return 0, errScanString
}
if udigits == 4 {
udigits = -1
}
continue
}
if c == '"' {
return i + 2, nil
}
if c == '\\' {
escaped = true
continue
}
if c < 0x20 {
return 0, errScanString
}
}
return 0, errScanString
}

func isSpace(c byte) bool {
return (c == ' ') || (c == '\n') || (c == '\r') || (c == '\t')
}

func skipSpace(buf []byte) int {
for i, c := range buf {
if !isSpace(c) {
return i
}
}
return len(buf)
}

func unquoteBytes(pool pool.Pool, s []byte) (string, error) {
var ok bool
var t string
Expand Down Expand Up @@ -175,47 +117,6 @@ func (s *jsonParser) scanOpenArray() error {
return s.skipSpace()
}

func (s *jsonParser) scanString() error {
n, err := scanString(s.buf[s.offset:])
if err != nil {
return err
}
if err := s.incOffset(n); err != nil {
return err
}
return s.skipSpace()
}

func (s *jsonParser) scanConst(valBytes []byte, err error) error {
start := s.offset
if orr := s.incOffset(len(valBytes)); orr != nil {
return err
}
end := s.offset
if !bytes.Equal(s.buf[start:end], valBytes) {
return err
}
return s.skipSpace()
}

var trueBytes = []byte{'t', 'r', 'u', 'e'}

func (s *jsonParser) scanTrue() error {
return s.scanConst(trueBytes, errExpectedTrue)
}

var falseBytes = []byte{'f', 'a', 'l', 's', 'e'}

func (s *jsonParser) scanFalse() error {
return s.scanConst(falseBytes, errExpectedFalse)
}

var nullBytes = []byte{'n', 'u', 'l', 'l'}

func (s *jsonParser) scanNull() error {
return s.scanConst(nullBytes, errExpectedNull)
}

func (s *jsonParser) scanDigits() error {
if s.offset >= len(s.buf) {
return io.ErrShortBuffer
Expand Down
80 changes: 0 additions & 80 deletions json/pool_test.go

This file was deleted.

78 changes: 78 additions & 0 deletions json/scan_const.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// Copyright 2025 Walter Schulze
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package json

import "bytes"

func scanConst(buf []byte, valBytes []byte, err error) (int, error) {
if len(buf) < len(valBytes) {
return 0, err
}
if !bytes.Equal(buf[0:len(valBytes)], valBytes) {
return 0, err
}
return len(valBytes), nil
}

var trueBytes = []byte{'t', 'r', 'u', 'e'}

func scanTrue(buf []byte) (int, error) {
return scanConst(buf, trueBytes, errExpectedTrue)
}

func (s *jsonParser) scanTrue() error {
n, err := scanTrue(s.buf[s.offset:])
if err != nil {
return err
}
if err := s.incOffset(n); err != nil {
return err
}
return s.skipSpace()
}

var falseBytes = []byte{'f', 'a', 'l', 's', 'e'}

func scanFalse(buf []byte) (int, error) {
return scanConst(buf, falseBytes, errExpectedFalse)
}

func (s *jsonParser) scanFalse() error {
n, err := scanFalse(s.buf[s.offset:])
if err != nil {
return err
}
if err := s.incOffset(n); err != nil {
return err
}
return s.skipSpace()
}

var nullBytes = []byte{'n', 'u', 'l', 'l'}

func scanNull(buf []byte) (int, error) {
return scanConst(buf, nullBytes, errExpectedNull)
}

func (s *jsonParser) scanNull() error {
n, err := scanNull(s.buf[s.offset:])
if err != nil {
return err
}
if err := s.incOffset(n); err != nil {
return err
}
return s.skipSpace()
}
28 changes: 28 additions & 0 deletions json/scan_space.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright 2025 Walter Schulze
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package json

func isSpace(c byte) bool {
return (c == ' ') || (c == '\n') || (c == '\r') || (c == '\t')
}

func skipSpace(buf []byte) int {
for i, c := range buf {
if !isSpace(c) {
return i
}
}
return len(buf)
}
Loading

0 comments on commit 243ea54

Please sign in to comment.