Skip to content

Commit

Permalink
Alex/frpc 19 update polyglot dependencies to fix versioning (#42)
Browse files Browse the repository at this point in the history
* Fix go version of subfolder to use v2 subfolder for new major version

* Update github actions

* Fix generator import statement

* Fix tests and update changlog

* Tidy go mods
  • Loading branch information
SuperManifolds authored Apr 23, 2024
1 parent bc0aad2 commit 1b318fb
Show file tree
Hide file tree
Showing 93 changed files with 6,833 additions and 1,209 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/benchmarks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ jobs:
cache: true
- name: Run Benchmarks
run: make benchmark-polyglot
working-directory: benchmarks
working-directory: v2/benchmarks
10 changes: 7 additions & 3 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,12 @@ jobs:
go-version: "1.21"
check-latest: true
cache: true
- name: Run Tests
- name: Run Tests (Go v1)
run: go test -v ./...
- name: Run Tests (Go v2)
run: |
cd v2
go test -v ./...
rust:
runs-on: ubuntu-latest
steps:
Expand All @@ -33,7 +37,7 @@ jobs:
run: cargo check
- name: Cargo check wasm32-wasi
run: cargo check --target wasm32-wasi
- name: Run Tests
- name: Run Tests (Rust)
run: cargo test
typescript:
runs-on: ubuntu-latest
Expand All @@ -52,7 +56,7 @@ jobs:
key: ${{ runner.os }}-${{ hashFiles('*.json') }}
- name: Install Node Dependencies with NPM
run: npm install
- name: Run Tests
- name: Run Tests (JS)
run: npm run test
c:
runs-on: ubuntu-latest
Expand Down
19 changes: 3 additions & 16 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,18 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

## [v2.1.0] 2024-04-04]
## [v2.0.0] 2024-04-23]

### Changes

- Updated the names of error values in Go to fit with Go's standard code-style conventions

## [v2.0.2] - 2024-03-26
- significant performance improvements for the golang implementation
this update makes necessary changes to the public interface of the library to accomplish this

### Fixes

- Fixed a bug in Polyglot Go where the capacity of the buffer would not grow properly resulting in silent short writes and corrupted data

## [v2.0.1] - 2024-03-12

### Changes

- Made Buffer.Grow() in the Polyglot Go library public

## [v2.0.0] - 2024-03-14

### Changes

- Significant performance improvements for the Golang implementation
This update makes necessary changes to the public interface of the library to accomplish this

## [v1.1.4] - 2023-10-12

### Fixes
Expand Down
60 changes: 13 additions & 47 deletions buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,64 +20,30 @@ const (
defaultSize = 512
)

type Buffer struct {
b []byte
offset int
}

func NewBuffer() *Buffer {
return &Buffer{
b: make([]byte, defaultSize),
offset: 0,
}
}

func NewBufferSize(size int) *Buffer {
return &Buffer{
b: make([]byte, size),
offset: 0,
}
}

func NewBufferFromBytes(b []byte) *Buffer {
return &Buffer{
b: b,
offset: 0,
}
}
type Buffer []byte

func (buf *Buffer) Reset() {
buf.offset = 0
}

func (buf *Buffer) MoveOffset(offset int) {
buf.offset += offset
*buf = (*buf)[:0]
}

func (buf *Buffer) Grow(n int) {
if cap(buf.b)-buf.offset < n {
if cap(buf.b) < n {
buf.b = append(buf.b[:buf.offset], make([]byte, n+cap(buf.b)-buf.offset)...)
} else {
buf.b = append(buf.b[:buf.offset], make([]byte, cap(buf.b)*2-buf.offset)...)
}
func (buf *Buffer) Write(b []byte) int {
if cap(*buf)-len(*buf) < len(b) {
*buf = append((*buf)[:len(*buf)], b...)
} else {
*buf = (*buf)[:len(*buf)+copy((*buf)[len(*buf):cap(*buf)], b)]
}
return len(b)
}

func (buf *Buffer) Write(b []byte) int {
buf.Grow(len(b))
buf.offset += copy(buf.b[buf.offset:cap(buf.b)], b)
return len(b)
func NewBuffer() *Buffer {
c := make(Buffer, 0, defaultSize)
return &c
}

func (buf *Buffer) Bytes() []byte {
return buf.b[:buf.offset]
return *buf
}

func (buf *Buffer) Len() int {
return buf.offset
}

func (buf *Buffer) Cap() int {
return cap(buf.b)
return len(*buf)
}
41 changes: 20 additions & 21 deletions buffer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,35 +27,30 @@ import (
func TestWrite(t *testing.T) {
t.Parallel()

p := NewBuffer()
p := *NewBuffer()

b := make([]byte, 32)
_, err := rand.Read(b)
assert.NoError(t, err)

p.Write(b)
assert.EqualValues(t, b, p.Bytes())
assert.EqualValues(t, b, p)

p.Reset()
assert.NotEqual(t, b, p.Bytes())
assert.Equal(t, 0, p.Len())
assert.Equal(t, 512, p.Cap())
assert.NotEqual(t, b, p)
assert.Equal(t, 0, len(p))
assert.Equal(t, 512, cap(p))

b = make([]byte, 1024)
_, err = rand.Read(b)
assert.NoError(t, err)

p.Write(b)

assert.EqualValues(t, b, p.Bytes())
assert.Equal(t, 1024, p.Len())
assert.GreaterOrEqual(t, p.Cap(), 1024)

p.Write(b)
assert.EqualValues(t, b, p)
assert.Equal(t, 1024, len(p))
assert.GreaterOrEqual(t, cap(p), 1024)

assert.EqualValues(t, append(b, b...), p.Bytes())
assert.Equal(t, 2048, p.Len())
assert.GreaterOrEqual(t, p.Cap(), 2048)
}

type embedStruct struct {
Expand Down Expand Up @@ -292,7 +287,7 @@ func TestCompleteChain(t *testing.T) {
val := new(testStruct)
var err error

d := Decoder(p.Bytes())
d := GetDecoder(p.Bytes())

val.err, err = d.Error()
assert.NoError(t, err)
Expand Down Expand Up @@ -375,12 +370,13 @@ func TestCompleteChain(t *testing.T) {
}
assert.Equal(t, test.m, val.m)

assert.Equal(t, 0, len(*d))
assert.Equal(t, 0, len(d.b))
d.Return()

p.Reset()
n := testing.AllocsPerRun(100, func() {
Encoder(p).Error(test.err).String(test.test).Bytes(test.b).Uint8(test.num1).Uint16(test.num2).Uint32(test.num3).Uint64(test.num4).Bool(test.truth).Nil()
d = Decoder(p.Bytes())
d = GetDecoder(p.Bytes())
val.err, err = d.Error()
val.test, err = d.String()
val.b, err = d.Bytes(val.b)
Expand All @@ -390,23 +386,24 @@ func TestCompleteChain(t *testing.T) {
val.num4, err = d.Uint64()
val.truth, err = d.Bool()
isNil = d.Nil()
d.Return()
p.Reset()
})
assert.Equal(t, float64(4), n)
assert.Equal(t, float64(3), n)
}

func TestNilSlice(t *testing.T) {
s := make([]string, 0)
p := NewBuffer()
Encoder(p).Slice(uint32(len(s)), StringKind)

d := Decoder(p.Bytes())
d := GetDecoder(p.Bytes())
j, err := d.Slice(StringKind)
assert.NoError(t, err)
assert.Equal(t, uint32(len(s)), j)

j, err = d.Slice(StringKind)
assert.ErrorIs(t, err, ErrInvalidSlice)
assert.ErrorIs(t, err, InvalidSlice)
assert.Zero(t, j)
}

Expand All @@ -418,13 +415,15 @@ func TestError(t *testing.T) {
p := NewBuffer()
Encoder(p).Error(v)

d := Decoder(p.Bytes())
d := GetDecoder(p.Bytes())
_, err := d.String()
assert.ErrorIs(t, err, ErrInvalidString)
assert.ErrorIs(t, err, InvalidString)

val, err := d.Error()
assert.NoError(t, err)
assert.ErrorIs(t, val, v)

d.Return()
}

func TestLen(t *testing.T) {
Expand Down
Loading

0 comments on commit 1b318fb

Please sign in to comment.