Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: new reflect impl for non-amd64 arch #49

Merged
merged 6 commits into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions frugal.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,31 @@ package frugal
import (
"github.com/cloudwego/frugal/internal/binary/decoder"
"github.com/cloudwego/frugal/internal/binary/encoder"
"github.com/cloudwego/frugal/internal/reflect"
"github.com/cloudwego/frugal/iov"
)

// EncodedSize measures the encoded size of val.
func EncodedSize(val interface{}) int {
if nojit {
return reflect.EncodedSize(val)
}
return encoder.EncodedSize(val)
}

// EncodeObject serializes val into buf with Thrift Binary Protocol, with optional Zero-Copy iov.BufferWriter.
// buf must be large enough to contain the entire serialization result.
func EncodeObject(buf []byte, mem iov.BufferWriter, val interface{}) (int, error) {
if nojit {
return reflect.Encode(buf, val) // TODO: impl iov.BufferWriter
}
return encoder.EncodeObject(buf, mem, val)
}

// DecodeObject deserializes buf into val with Thrift Binary Protocol.
func DecodeObject(buf []byte, val interface{}) (int, error) {
if nojit {
return reflect.Decode(buf, val)
}
return decoder.DecodeObject(buf, val)
}
7 changes: 5 additions & 2 deletions internal/binary/defs/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func ResolveFields(vt reflect.Type) ([]Field, error) {
}

/* still not found, do the actual resolving */
if fv, ex = doResolveFields(vt); ex != nil {
if fv, ex = DoResolveFields(vt); ex != nil {
return nil, ex
}

Expand All @@ -118,7 +118,10 @@ func ResolveFields(vt reflect.Type) ([]Field, error) {
return fv, nil
}

func doResolveFields(vt reflect.Type) ([]Field, error) {
// DoResolveFields ... no cache, use ResolveFields instead.
// it's only used by reflect pkg for less objects,
// coz reflect pkg has its own cache.
func DoResolveFields(vt reflect.Type) ([]Field, error) {
var err error
var ret []Field
var mem reflect.Value
Expand Down
44 changes: 44 additions & 0 deletions internal/reflect/bitset.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright 2024 CloudWeGo Authors
*
* 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 reflect

import "sync"

type bitset struct {
data [1024]uint64 // 635536 bits for field id
}

var bitsetPool = sync.Pool{
New: func() interface{} {
return &bitset{}
},
}

func (s *bitset) set(i uint16) {
x, y := i>>8, i&7 // i/64, i%64
s.data[x] |= 1 << y
}

func (s *bitset) unset(i uint16) {
x, y := i>>8, i&7 // i/64, i%64
s.data[x] &= ^(1 << y)
}

func (s *bitset) test(i uint16) bool {
x, y := i>>8, i&7 // i/64, i%64
return (s.data[x] & (1 << y)) != 0
}
44 changes: 44 additions & 0 deletions internal/reflect/bitset_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright 2024 CloudWeGo Authors
*
* 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 reflect

import (
"testing"

"github.com/stretchr/testify/require"
)

func TestBitset(t *testing.T) {
s := &bitset{}
for i := uint16(0); i < ^uint16(0); i++ {
if i%2 == 0 {
s.set(i)
}
if i%4 == 0 {
s.unset(i)
}
}
for i := uint16(0); i < ^uint16(0); i++ {
if i%4 == 0 {
require.False(t, s.test(i))
} else if i%2 == 0 {
require.True(t, s.test(i))
} else {
require.False(t, s.test(i))
}
}
}
Loading
Loading