Skip to content

Commit 1b318fb

Browse files
Alex/frpc 19 update polyglot dependencies to fix versioning (#42)
* 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
1 parent bc0aad2 commit 1b318fb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

93 files changed

+6833
-1209
lines changed

.github/workflows/benchmarks.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,4 @@ jobs:
1616
cache: true
1717
- name: Run Benchmarks
1818
run: make benchmark-polyglot
19-
working-directory: benchmarks
19+
working-directory: v2/benchmarks

.github/workflows/tests.yaml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,12 @@ jobs:
1414
go-version: "1.21"
1515
check-latest: true
1616
cache: true
17-
- name: Run Tests
17+
- name: Run Tests (Go v1)
1818
run: go test -v ./...
19+
- name: Run Tests (Go v2)
20+
run: |
21+
cd v2
22+
go test -v ./...
1923
rust:
2024
runs-on: ubuntu-latest
2125
steps:
@@ -33,7 +37,7 @@ jobs:
3337
run: cargo check
3438
- name: Cargo check wasm32-wasi
3539
run: cargo check --target wasm32-wasi
36-
- name: Run Tests
40+
- name: Run Tests (Rust)
3741
run: cargo test
3842
typescript:
3943
runs-on: ubuntu-latest
@@ -52,7 +56,7 @@ jobs:
5256
key: ${{ runner.os }}-${{ hashFiles('*.json') }}
5357
- name: Install Node Dependencies with NPM
5458
run: npm install
55-
- name: Run Tests
59+
- name: Run Tests (JS)
5660
run: npm run test
5761
c:
5862
runs-on: ubuntu-latest

CHANGELOG.md

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,31 +7,18 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

88
## [Unreleased]
99

10-
## [v2.1.0] 2024-04-04]
10+
## [v2.0.0] 2024-04-23]
1111

1212
### Changes
1313

1414
- Updated the names of error values in Go to fit with Go's standard code-style conventions
15-
16-
## [v2.0.2] - 2024-03-26
15+
- significant performance improvements for the golang implementation
16+
this update makes necessary changes to the public interface of the library to accomplish this
1717

1818
### Fixes
1919

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

22-
## [v2.0.1] - 2024-03-12
23-
24-
### Changes
25-
26-
- Made Buffer.Grow() in the Polyglot Go library public
27-
28-
## [v2.0.0] - 2024-03-14
29-
30-
### Changes
31-
32-
- Significant performance improvements for the Golang implementation
33-
This update makes necessary changes to the public interface of the library to accomplish this
34-
3522
## [v1.1.4] - 2023-10-12
3623

3724
### Fixes

buffer.go

Lines changed: 13 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -20,64 +20,30 @@ const (
2020
defaultSize = 512
2121
)
2222

23-
type Buffer struct {
24-
b []byte
25-
offset int
26-
}
27-
28-
func NewBuffer() *Buffer {
29-
return &Buffer{
30-
b: make([]byte, defaultSize),
31-
offset: 0,
32-
}
33-
}
34-
35-
func NewBufferSize(size int) *Buffer {
36-
return &Buffer{
37-
b: make([]byte, size),
38-
offset: 0,
39-
}
40-
}
41-
42-
func NewBufferFromBytes(b []byte) *Buffer {
43-
return &Buffer{
44-
b: b,
45-
offset: 0,
46-
}
47-
}
23+
type Buffer []byte
4824

4925
func (buf *Buffer) Reset() {
50-
buf.offset = 0
51-
}
52-
53-
func (buf *Buffer) MoveOffset(offset int) {
54-
buf.offset += offset
26+
*buf = (*buf)[:0]
5527
}
5628

57-
func (buf *Buffer) Grow(n int) {
58-
if cap(buf.b)-buf.offset < n {
59-
if cap(buf.b) < n {
60-
buf.b = append(buf.b[:buf.offset], make([]byte, n+cap(buf.b)-buf.offset)...)
61-
} else {
62-
buf.b = append(buf.b[:buf.offset], make([]byte, cap(buf.b)*2-buf.offset)...)
63-
}
29+
func (buf *Buffer) Write(b []byte) int {
30+
if cap(*buf)-len(*buf) < len(b) {
31+
*buf = append((*buf)[:len(*buf)], b...)
32+
} else {
33+
*buf = (*buf)[:len(*buf)+copy((*buf)[len(*buf):cap(*buf)], b)]
6434
}
35+
return len(b)
6536
}
6637

67-
func (buf *Buffer) Write(b []byte) int {
68-
buf.Grow(len(b))
69-
buf.offset += copy(buf.b[buf.offset:cap(buf.b)], b)
70-
return len(b)
38+
func NewBuffer() *Buffer {
39+
c := make(Buffer, 0, defaultSize)
40+
return &c
7141
}
7242

7343
func (buf *Buffer) Bytes() []byte {
74-
return buf.b[:buf.offset]
44+
return *buf
7545
}
7646

7747
func (buf *Buffer) Len() int {
78-
return buf.offset
79-
}
80-
81-
func (buf *Buffer) Cap() int {
82-
return cap(buf.b)
48+
return len(*buf)
8349
}

buffer_test.go

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -27,35 +27,30 @@ import (
2727
func TestWrite(t *testing.T) {
2828
t.Parallel()
2929

30-
p := NewBuffer()
30+
p := *NewBuffer()
3131

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

3636
p.Write(b)
37-
assert.EqualValues(t, b, p.Bytes())
37+
assert.EqualValues(t, b, p)
3838

3939
p.Reset()
40-
assert.NotEqual(t, b, p.Bytes())
41-
assert.Equal(t, 0, p.Len())
42-
assert.Equal(t, 512, p.Cap())
40+
assert.NotEqual(t, b, p)
41+
assert.Equal(t, 0, len(p))
42+
assert.Equal(t, 512, cap(p))
4343

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

4848
p.Write(b)
4949

50-
assert.EqualValues(t, b, p.Bytes())
51-
assert.Equal(t, 1024, p.Len())
52-
assert.GreaterOrEqual(t, p.Cap(), 1024)
53-
54-
p.Write(b)
50+
assert.EqualValues(t, b, p)
51+
assert.Equal(t, 1024, len(p))
52+
assert.GreaterOrEqual(t, cap(p), 1024)
5553

56-
assert.EqualValues(t, append(b, b...), p.Bytes())
57-
assert.Equal(t, 2048, p.Len())
58-
assert.GreaterOrEqual(t, p.Cap(), 2048)
5954
}
6055

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

295-
d := Decoder(p.Bytes())
290+
d := GetDecoder(p.Bytes())
296291

297292
val.err, err = d.Error()
298293
assert.NoError(t, err)
@@ -375,12 +370,13 @@ func TestCompleteChain(t *testing.T) {
375370
}
376371
assert.Equal(t, test.m, val.m)
377372

378-
assert.Equal(t, 0, len(*d))
373+
assert.Equal(t, 0, len(d.b))
374+
d.Return()
379375

380376
p.Reset()
381377
n := testing.AllocsPerRun(100, func() {
382378
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()
383-
d = Decoder(p.Bytes())
379+
d = GetDecoder(p.Bytes())
384380
val.err, err = d.Error()
385381
val.test, err = d.String()
386382
val.b, err = d.Bytes(val.b)
@@ -390,23 +386,24 @@ func TestCompleteChain(t *testing.T) {
390386
val.num4, err = d.Uint64()
391387
val.truth, err = d.Bool()
392388
isNil = d.Nil()
389+
d.Return()
393390
p.Reset()
394391
})
395-
assert.Equal(t, float64(4), n)
392+
assert.Equal(t, float64(3), n)
396393
}
397394

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

403-
d := Decoder(p.Bytes())
400+
d := GetDecoder(p.Bytes())
404401
j, err := d.Slice(StringKind)
405402
assert.NoError(t, err)
406403
assert.Equal(t, uint32(len(s)), j)
407404

408405
j, err = d.Slice(StringKind)
409-
assert.ErrorIs(t, err, ErrInvalidSlice)
406+
assert.ErrorIs(t, err, InvalidSlice)
410407
assert.Zero(t, j)
411408
}
412409

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

421-
d := Decoder(p.Bytes())
418+
d := GetDecoder(p.Bytes())
422419
_, err := d.String()
423-
assert.ErrorIs(t, err, ErrInvalidString)
420+
assert.ErrorIs(t, err, InvalidString)
424421

425422
val, err := d.Error()
426423
assert.NoError(t, err)
427424
assert.ErrorIs(t, val, v)
425+
426+
d.Return()
428427
}
429428

430429
func TestLen(t *testing.T) {

0 commit comments

Comments
 (0)