Skip to content

Commit 8f69605

Browse files
authored
refactor(go): adjust go buffer uint32/64 write/read name style (#3242)
## Why? ## What does this PR do? ## Related issues #2982 ## Does this PR introduce any user-facing change? - [ ] Does this PR introduce any public API change? - [ ] Does this PR introduce any binary protocol compatibility change? ## Benchmark
1 parent 4cb1e9c commit 8f69605

31 files changed

Lines changed: 482 additions & 482 deletions

go/fory/array.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func writeArrayRefAndType(ctx *WriteContext, refMode RefMode, writeType bool, va
3030
ctx.Buffer().WriteInt8(NotNullValueFlag)
3131
}
3232
if writeType {
33-
ctx.Buffer().WriteVaruint32Small7(uint32(typeId))
33+
ctx.Buffer().WriteVarUint32Small7(uint32(typeId))
3434
}
3535
return false
3636
}
@@ -55,7 +55,7 @@ func readArrayRefAndType(ctx *ReadContext, refMode RefMode, readType bool, value
5555
}
5656
}
5757
if readType {
58-
typeID := buf.ReadVaruint32Small7(err)
58+
typeID := buf.ReadVarUint32Small7(err)
5959
if ctx.HasError() {
6060
return false
6161
}
@@ -73,7 +73,7 @@ type arraySerializer struct{}
7373
func (s arraySerializer) WriteData(ctx *WriteContext, value reflect.Value) {
7474
buf := ctx.Buffer()
7575
length := value.Len()
76-
buf.WriteVaruint32(uint32(length))
76+
buf.WriteVarUint32(uint32(length))
7777
for i := 0; i < length; i++ {
7878
elem := value.Index(i)
7979
buf.WriteInt8(NotNullValueFlag)
@@ -92,7 +92,7 @@ func (s arraySerializer) Write(ctx *WriteContext, refMode RefMode, writeType boo
9292
func (s arraySerializer) ReadData(ctx *ReadContext, value reflect.Value) {
9393
buf := ctx.Buffer()
9494
err := ctx.Err()
95-
length := int(buf.ReadVaruint32(err))
95+
length := int(buf.ReadVarUint32(err))
9696
for i := 0; i < length; i++ {
9797
_ = buf.ReadInt8(err)
9898
}
@@ -122,7 +122,7 @@ func (s *arrayConcreteValueSerializer) WriteData(ctx *WriteContext, value reflec
122122
buf := ctx.Buffer()
123123

124124
// Write length
125-
buf.WriteVaruint32(uint32(length))
125+
buf.WriteVarUint32(uint32(length))
126126
if length == 0 {
127127
return
128128
}
@@ -178,7 +178,7 @@ func (s *arrayConcreteValueSerializer) WriteData(ctx *WriteContext, value reflec
178178
if IsNamespacedType(TypeId(internalTypeID)) {
179179
ctx.TypeResolver().WriteTypeInfo(buf, elemTypeInfo, ctx.Err())
180180
} else {
181-
buf.WriteVaruint32Small7(uint32(internalTypeID))
181+
buf.WriteVarUint32Small7(uint32(internalTypeID))
182182
}
183183

184184
// Write elements
@@ -226,7 +226,7 @@ func (s *arrayConcreteValueSerializer) Write(ctx *WriteContext, refMode RefMode,
226226
func (s *arrayConcreteValueSerializer) ReadData(ctx *ReadContext, value reflect.Value) {
227227
buf := ctx.Buffer()
228228
err := ctx.Err()
229-
length := int(buf.ReadVaruint32(err))
229+
length := int(buf.ReadVarUint32(err))
230230

231231
var trackRefs bool
232232
if length > 0 {
@@ -239,7 +239,7 @@ func (s *arrayConcreteValueSerializer) ReadData(ctx *ReadContext, value reflect.
239239
// Read element type info if present
240240
if (collectFlag & CollectionIsSameType) != 0 {
241241
if (collectFlag & CollectionIsDeclElementType) == 0 {
242-
typeID := buf.ReadVaruint32(err)
242+
typeID := buf.ReadVarUint32(err)
243243
if ctx.HasError() {
244244
return
245245
}

go/fory/buffer.go

Lines changed: 51 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -129,11 +129,11 @@ func (b *ByteBuffer) WriteLength(value int) {
129129
if value >= MaxInt32 {
130130
panic(fmt.Errorf("too long: %d", value))
131131
}
132-
b.WriteVaruint32(uint32(value))
132+
b.WriteVarUint32(uint32(value))
133133
}
134134

135135
func (b *ByteBuffer) ReadLength(err *Error) int {
136-
return int(b.ReadVaruint32(err))
136+
return int(b.ReadVarUint32(err))
137137
}
138138

139139
//go:inline
@@ -393,12 +393,12 @@ func (b *ByteBuffer) Reserve(n int) {
393393
//go:inline
394394
func (b *ByteBuffer) UnsafeWriteVarint32(value int32) int8 {
395395
u := uint32((value << 1) ^ (value >> 31))
396-
return b.UnsafeWriteVaruint32(u)
396+
return b.UnsafeWriteVarUint32(u)
397397
}
398398

399-
// UnsafeWriteVaruint32 writes a varuint32 without grow check.
399+
// UnsafeWriteVarUint32 writes a VarUint32 without grow check.
400400
// Caller must have called Reserve(8) beforehand (8 for bulk uint64 write).
401-
func (b *ByteBuffer) UnsafeWriteVaruint32(value uint32) int8 {
401+
func (b *ByteBuffer) UnsafeWriteVarUint32(value uint32) int8 {
402402
if value>>7 == 0 {
403403
b.data[b.writerIndex] = byte(value)
404404
b.writerIndex++
@@ -600,13 +600,13 @@ func (b *ByteBuffer) UnsafeWriteBool(v bool) {
600600
//go:inline
601601
func (b *ByteBuffer) UnsafePutVarInt32(offset int, value int32) int {
602602
u := uint32((value << 1) ^ (value >> 31))
603-
return b.UnsafePutVaruint32(offset, u)
603+
return b.UnsafePutVarUint32(offset, u)
604604
}
605605

606-
// UnsafePutVaruint32 writes an unsigned varuint32 at the given offset without advancing writerIndex.
606+
// UnsafePutVarUint32 writes an unsigned VarUint32 at the given offset without advancing writerIndex.
607607
// Caller must have called Reserve(8) to ensure capacity (8 for bulk uint64 write).
608608
// Returns the number of bytes written (1-5).
609-
func (b *ByteBuffer) UnsafePutVaruint32(offset int, value uint32) int {
609+
func (b *ByteBuffer) UnsafePutVarUint32(offset int, value uint32) int {
610610
if value>>7 == 0 {
611611
b.data[offset] = byte(value)
612612
return 1
@@ -666,13 +666,13 @@ func (b *ByteBuffer) UnsafePutVaruint32(offset int, value uint32) int {
666666
//go:inline
667667
func (b *ByteBuffer) UnsafePutVarInt64(offset int, value int64) int {
668668
u := uint64((value << 1) ^ (value >> 63))
669-
return b.UnsafePutVaruint64(offset, u)
669+
return b.UnsafePutVarUint64(offset, u)
670670
}
671671

672-
// UnsafePutVaruint64 writes an unsigned varuint64 at the given offset without advancing writerIndex.
672+
// UnsafePutVarUint64 writes an unsigned VarUint64 at the given offset without advancing writerIndex.
673673
// Caller must have called Reserve(16) to ensure capacity (for bulk writes).
674674
// Returns the number of bytes written (1-9).
675-
func (b *ByteBuffer) UnsafePutVaruint64(offset int, value uint64) int {
675+
func (b *ByteBuffer) UnsafePutVarUint64(offset int, value uint64) int {
676676
if value>>7 == 0 {
677677
b.data[offset] = byte(value)
678678
return 1
@@ -795,13 +795,13 @@ func (b *ByteBuffer) PutInt32(index int, value int32) {
795795
binary.LittleEndian.PutUint32(b.data[index:], uint32(value))
796796
}
797797

798-
// WriteVaruint32 writes a 1-5 byte positive int (no zigzag encoding), returns the number of bytes written.
798+
// WriteVarUint32 writes a 1-5 byte positive int (no zigzag encoding), returns the number of bytes written.
799799
// Use this for lengths, type IDs, and other non-negative values.
800800
//
801801
//go:inline
802-
func (b *ByteBuffer) WriteVaruint32(value uint32) int8 {
802+
func (b *ByteBuffer) WriteVarUint32(value uint32) int8 {
803803
b.grow(8) // 8 bytes for bulk uint64 write in worst case
804-
return b.UnsafeWriteVaruint32(value)
804+
return b.UnsafeWriteVarUint32(value)
805805
}
806806

807807
type BufferObject interface {
@@ -815,11 +815,11 @@ type BufferObject interface {
815815
//go:inline
816816
func (b *ByteBuffer) WriteVarint64(value int64) {
817817
u := uint64((value << 1) ^ (value >> 63))
818-
b.WriteVaruint64(u)
818+
b.WriteVarUint64(u)
819819
}
820820

821-
// WriteVaruint64 writes to unsigned varint (up to 9 bytes)
822-
func (b *ByteBuffer) WriteVaruint64(value uint64) {
821+
// WriteVarUint64 writes to unsigned varint (up to 9 bytes)
822+
func (b *ByteBuffer) WriteVarUint64(value uint64) {
823823
b.grow(9)
824824
offset := b.writerIndex
825825
data := b.data[offset : offset+9]
@@ -942,7 +942,7 @@ func (b *ByteBuffer) readVaruint36SmallSlow(err *Error) uint64 {
942942
//
943943
//go:inline
944944
func (b *ByteBuffer) ReadVarint64(err *Error) int64 {
945-
u := b.ReadVaruint64(err)
945+
u := b.ReadVarUint64(err)
946946
v := int64(u >> 1)
947947
if u&1 != 0 {
948948
v = ^v
@@ -1053,20 +1053,20 @@ func (b *ByteBuffer) ReadTaggedUint64(err *Error) uint64 {
10531053
return value
10541054
}
10551055

1056-
// ReadVaruint64 reads unsigned varint
1056+
// ReadVarUint64 reads unsigned varint
10571057
//
10581058
//go:inline
1059-
func (b *ByteBuffer) ReadVaruint64(err *Error) uint64 {
1059+
func (b *ByteBuffer) ReadVarUint64(err *Error) uint64 {
10601060
if b.remaining() >= 9 {
1061-
return b.readVaruint64Fast()
1061+
return b.readVarUint64Fast()
10621062
}
1063-
return b.readVaruint64Slow(err)
1063+
return b.readVarUint64Slow(err)
10641064
}
10651065

10661066
// Fast path (when the remaining bytes are sufficient)
10671067
//
10681068
//go:inline
1069-
func (b *ByteBuffer) readVaruint64Fast() uint64 {
1069+
func (b *ByteBuffer) readVarUint64Fast() uint64 {
10701070
// Single instruction load using unsafe pointer cast (little-endian only)
10711071
var bulk uint64
10721072
if isLittleEndian {
@@ -1117,7 +1117,7 @@ func (b *ByteBuffer) readVaruint64Fast() uint64 {
11171117
}
11181118

11191119
// Slow path (read byte by byte)
1120-
func (b *ByteBuffer) readVaruint64Slow(err *Error) uint64 {
1120+
func (b *ByteBuffer) readVarUint64Slow(err *Error) uint64 {
11211121
var result uint64
11221122
var shift uint
11231123
for i := 0; i < 8; i++ {
@@ -1167,14 +1167,14 @@ func (b *ByteBuffer) ReadUint8(err *Error) uint8 {
11671167
//go:inline
11681168
func (b *ByteBuffer) WriteVarint32(value int32) int8 {
11691169
u := uint32((value << 1) ^ (value >> 31))
1170-
return b.WriteVaruint32(u)
1170+
return b.WriteVarUint32(u)
11711171
}
11721172

11731173
// ReadVarint32 reads a signed int32 using zigzag decoding (compatible with Java's readVarint32).
11741174
//
11751175
//go:inline
11761176
func (b *ByteBuffer) ReadVarint32(err *Error) int32 {
1177-
u := b.ReadVaruint32(err)
1177+
u := b.ReadVarUint32(err)
11781178
v := int32(u >> 1)
11791179
if u&1 != 0 {
11801180
v = ^v
@@ -1187,7 +1187,7 @@ func (b *ByteBuffer) ReadVarint32(err *Error) int32 {
11871187
//
11881188
//go:inline
11891189
func (b *ByteBuffer) UnsafeReadVarint32() int32 {
1190-
u := b.readVaruint32Fast()
1190+
u := b.readVarUint32Fast()
11911191
v := int32(u >> 1)
11921192
if u&1 != 0 {
11931193
v = ^v
@@ -1200,44 +1200,44 @@ func (b *ByteBuffer) UnsafeReadVarint32() int32 {
12001200
//
12011201
//go:inline
12021202
func (b *ByteBuffer) UnsafeReadVarint64() int64 {
1203-
u := b.readVaruint64Fast()
1203+
u := b.readVarUint64Fast()
12041204
v := int64(u >> 1)
12051205
if u&1 != 0 {
12061206
v = ^v
12071207
}
12081208
return v
12091209
}
12101210

1211-
// UnsafeReadVaruint32 reads a varuint32 without bounds checking.
1211+
// UnsafeReadVarUint32 reads a VarUint32 without bounds checking.
12121212
// Caller must ensure remaining() >= 5 before calling.
12131213
//
12141214
//go:inline
1215-
func (b *ByteBuffer) UnsafeReadVaruint32() uint32 {
1216-
return b.readVaruint32Fast()
1215+
func (b *ByteBuffer) UnsafeReadVarUint32() uint32 {
1216+
return b.readVarUint32Fast()
12171217
}
12181218

1219-
// UnsafeReadVaruint64 reads a varuint64 without bounds checking.
1219+
// UnsafeReadVarUint64 reads a VarUint64 without bounds checking.
12201220
// Caller must ensure remaining() >= 10 before calling.
12211221
//
12221222
//go:inline
1223-
func (b *ByteBuffer) UnsafeReadVaruint64() uint64 {
1224-
return b.readVaruint64Fast()
1223+
func (b *ByteBuffer) UnsafeReadVarUint64() uint64 {
1224+
return b.readVarUint64Fast()
12251225
}
12261226

1227-
// ReadVaruint32 reads a varuint32 and sets error on bounds violation
1227+
// ReadVarUint32 reads a VarUint32 and sets error on bounds violation
12281228
//
12291229
//go:inline
1230-
func (b *ByteBuffer) ReadVaruint32(err *Error) uint32 {
1230+
func (b *ByteBuffer) ReadVarUint32(err *Error) uint32 {
12311231
if b.remaining() >= 8 { // Need 8 bytes for bulk uint64 read in fast path
1232-
return b.readVaruint32Fast()
1232+
return b.readVarUint32Fast()
12331233
}
1234-
return b.readVaruint32Slow(err)
1234+
return b.readVarUint32Slow(err)
12351235
}
12361236

12371237
// Fast path reading (when the remaining bytes are sufficient)
12381238
//
12391239
//go:inline
1240-
func (b *ByteBuffer) readVaruint32Fast() uint32 {
1240+
func (b *ByteBuffer) readVarUint32Fast() uint32 {
12411241
// Single instruction load using unsafe pointer cast (little-endian only)
12421242
// On big-endian systems, use binary.LittleEndian which the compiler optimizes
12431243
var bulk uint64
@@ -1271,7 +1271,7 @@ func (b *ByteBuffer) readVaruint32Fast() uint32 {
12711271
}
12721272

12731273
// Slow path reading (processing byte by byte)
1274-
func (b *ByteBuffer) readVaruint32Slow(err *Error) uint32 {
1274+
func (b *ByteBuffer) readVarUint32Slow(err *Error) uint32 {
12751275
var result uint32
12761276
var shift uint
12771277
for {
@@ -1287,7 +1287,7 @@ func (b *ByteBuffer) readVaruint32Slow(err *Error) uint32 {
12871287
}
12881288
shift += 7
12891289
if shift >= 35 {
1290-
*err = DeserializationError("varuint32 overflow")
1290+
*err = DeserializationError("VarUint32 overflow")
12911291
return 0
12921292
}
12931293
}
@@ -1299,18 +1299,18 @@ func (b *ByteBuffer) PutUint8(writerIndex int, value uint8) {
12991299
b.data[writerIndex] = byte(value)
13001300
}
13011301

1302-
// WriteVaruint32Small7 writes a uint32 in variable-length small-7 format
1303-
func (b *ByteBuffer) WriteVaruint32Small7(value uint32) int {
1302+
// WriteVarUint32Small7 writes a uint32 in variable-length small-7 format
1303+
func (b *ByteBuffer) WriteVarUint32Small7(value uint32) int {
13041304
b.grow(8)
13051305
if value>>7 == 0 {
13061306
b.data[b.writerIndex] = byte(value)
13071307
b.writerIndex++
13081308
return 1
13091309
}
1310-
return b.continueWriteVaruint32Small7(value)
1310+
return b.continueWriteVarUint32Small7(value)
13111311
}
13121312

1313-
func (b *ByteBuffer) continueWriteVaruint32Small7(value uint32) int {
1313+
func (b *ByteBuffer) continueWriteVarUint32Small7(value uint32) int {
13141314
encoded := uint64(value & 0x7F)
13151315
encoded |= uint64((value&0x3f80)<<1) | 0x80
13161316
idx := b.writerIndex
@@ -1434,8 +1434,8 @@ func (b *ByteBuffer) UnsafePutTaggedUint64(offset int, value uint64) int {
14341434
return 9
14351435
}
14361436

1437-
// ReadVaruint32Small7 reads a varuint32 in small-7 format with error checking
1438-
func (b *ByteBuffer) ReadVaruint32Small7(err *Error) uint32 {
1437+
// ReadVarUint32Small7 reads a VarUint32 in small-7 format with error checking
1438+
func (b *ByteBuffer) ReadVarUint32Small7(err *Error) uint32 {
14391439
if b.readerIndex >= len(b.data) {
14401440
*err = BufferOutOfBoundError(b.readerIndex, 1, len(b.data))
14411441
return 0
@@ -1447,10 +1447,10 @@ func (b *ByteBuffer) ReadVaruint32Small7(err *Error) uint32 {
14471447
b.readerIndex = readIdx
14481448
return uint32(v)
14491449
}
1450-
return b.readVaruint32Small14(err)
1450+
return b.readVarUint32Small14(err)
14511451
}
14521452

1453-
func (b *ByteBuffer) readVaruint32Small14(err *Error) uint32 {
1453+
func (b *ByteBuffer) readVarUint32Small14(err *Error) uint32 {
14541454
readIdx := b.readerIndex
14551455
if len(b.data)-readIdx >= 5 {
14561456
four := binary.LittleEndian.Uint32(b.data[readIdx:])
@@ -1460,7 +1460,7 @@ func (b *ByteBuffer) readVaruint32Small14(err *Error) uint32 {
14601460
readIdx++
14611461
value |= (four >> 1) & 0x3f80
14621462
if four&0x8000 != 0 {
1463-
return b.continueReadVaruint32(readIdx, four, value)
1463+
return b.continueReadVarUint32(readIdx, four, value)
14641464
}
14651465
}
14661466
b.readerIndex = readIdx
@@ -1469,7 +1469,7 @@ func (b *ByteBuffer) readVaruint32Small14(err *Error) uint32 {
14691469
return uint32(b.readVaruint36Slow(err))
14701470
}
14711471

1472-
func (b *ByteBuffer) continueReadVaruint32(readIdx int, bulkRead, value uint32) uint32 {
1472+
func (b *ByteBuffer) continueReadVarUint32(readIdx int, bulkRead, value uint32) uint32 {
14731473
readIdx++
14741474
value |= (bulkRead >> 2) & 0x1fc000
14751475
if bulkRead&0x800000 != 0 {

0 commit comments

Comments
 (0)