Skip to content

Commit

Permalink
use count2
Browse files Browse the repository at this point in the history
  • Loading branch information
AsterDY committed Dec 14, 2023
1 parent afc00d2 commit 687bd24
Show file tree
Hide file tree
Showing 6 changed files with 302 additions and 91 deletions.
99 changes: 96 additions & 3 deletions internal/native/avx/native_amd64_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 13 additions & 13 deletions internal/native/avx2/native_amd64_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion internal/native/dispatch_amd64.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ var stubs = []loader.GoC{
{"_validate_one", &S_validate_one, &__ValidateOne},
{"_validate_utf8", &S_validate_utf8, &__ValidateUTF8},
{"_validate_utf8_fast", &S_validate_utf8_fast, &__ValidateUTF8Fast},
{"_count_elems", &S_count_elems, &__CountElems},
{"_count_elems2", &S_count_elems, &__CountElems},
}

func useAVX() {
Expand Down
99 changes: 96 additions & 3 deletions internal/native/native_amd64_test.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -644,15 +644,15 @@ func Test_count_elems(t *testing.T) {
wantRet int
wantP int
}{
{
{
name: "0",
args: args{`}`, 0},
wantRet: -2,
wantP: 0,
},
{
name: "1",
args: args{`1}`, 0},
args: args{`1]`, 0},
wantRet: 1,
wantP: 2,
},
Expand All @@ -662,7 +662,12 @@ func Test_count_elems(t *testing.T) {
wantRet: 5,
wantP: 22,
},

// {
// name: "4",
// args: args{`{"a":1,"b":[2,3]}`, 0},
// wantRet: 3,
// wantP: 17,
// },
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand All @@ -676,6 +681,54 @@ func Test_count_elems(t *testing.T) {
}
}

func Test_count_elems2(t *testing.T) {
type args struct {
s string
p int
}
tests := []struct {
name string
args args
wantRet int
wantP int
}{
{
name: "0",
args: args{`{}`, 0},
wantRet: 1,
wantP: 2,
},
{
name: "1",
args: args{`[1]`, 0},
wantRet: 1,
wantP: 3,
},
{
name: "3",
args: args{`[1,"2",false,null,true]`, 0},
wantRet: 5,
wantP: 23,
},
{
name: "4",
args: args{`{"a":1,"b":[2,3]}`, 0},
wantRet: 3,
wantP: 17,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if gotRet := count_elems2(&tt.args.s, &tt.args.p); gotRet != tt.wantRet {
t.Errorf("count_elems() = %v, want %v", gotRet, tt.wantRet)
}
if tt.args.p != tt.wantP {
t.Errorf("p = %v, want %v", tt.args.p, tt.wantP)
}
})
}
}

func BenchmarkCountElems(b *testing.B) {
var maker = func(N int) string {
var s = `[`
Expand Down Expand Up @@ -714,3 +767,43 @@ func BenchmarkCountElems(b *testing.B) {
}
})
}


func BenchmarkCountElems2(b *testing.B) {
var maker = func(N int) string {
var s = `[`
for i :=0; i<N; i++ {
s += strconv.Itoa(i)
if i != N-1 {
s += ","
}
}
s += `]`
return s
}

b.Run("1", func(b *testing.B) {
s := maker(1)
b.ResetTimer()
for i:=0; i<b.N; i++ {
var p = 0
_ = count_elems2(&s, &p)
}
})
b.Run("10", func(b *testing.B) {
s := maker(10)
b.ResetTimer()
for i:=0; i<b.N; i++ {
var p = 0
_ = count_elems2(&s, &p)
}
})
b.Run("100", func(b *testing.B) {
s := maker(100)
b.ResetTimer()
for i:=0; i<b.N; i++ {
var p = 0
_ = count_elems2(&s, &p)
}
})
}
Loading

0 comments on commit 687bd24

Please sign in to comment.