Skip to content

Commit 5a55047

Browse files
authored
Fix: un-optimized codec (#21)
1 parent c585529 commit 5a55047

File tree

1 file changed

+16
-16
lines changed

1 file changed

+16
-16
lines changed

codec.go

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -312,30 +312,30 @@ func (d Decimal) MarshalBinary() ([]byte, error) {
312312

313313
func (d Decimal) marshalBinaryU128() ([]byte, error) {
314314
coef := d.coef.u128
315-
totalBytes := 19
316315

317-
if coef.hi == 0 {
318-
totalBytes = 11
319-
}
316+
var (
317+
buf []byte
318+
neg uint8
319+
)
320320

321-
buf := make([]byte, totalBytes)
322-
var neg int
323321
if d.neg {
324322
neg = 1
325323
}
326324

327-
// overflow + neg with overflow = false (always 0)
328-
buf[0] = byte(neg)
329-
buf[1] = byte(d.prec)
330-
buf[2] = byte(totalBytes)
331-
332-
if coef.hi != 0 {
325+
if coef.hi == 0 {
326+
buf = make([]byte, 11)
327+
buf[2] = 11
328+
copyUint64ToBytes(buf[3:], coef.lo)
329+
} else {
330+
buf = make([]byte, 19)
331+
buf[2] = 19
333332
copyUint64ToBytes(buf[3:], coef.hi)
334333
copyUint64ToBytes(buf[11:], coef.lo)
335-
} else {
336-
copyUint64ToBytes(buf[3:], coef.lo)
337334
}
338335

336+
buf[0] = neg
337+
buf[1] = d.prec
338+
339339
return buf, nil
340340
}
341341

@@ -369,7 +369,7 @@ func (d *Decimal) unmarshalBinaryU128(data []byte) error {
369369
coef := u128{}
370370
switch totalBytes {
371371
case 11:
372-
coef = u128{lo: binary.BigEndian.Uint64(data[3:])}
372+
coef.lo = binary.BigEndian.Uint64(data[3:])
373373
case 19:
374374
coef.hi = binary.BigEndian.Uint64(data[3:])
375375
coef.lo = binary.BigEndian.Uint64(data[11:])
@@ -467,7 +467,7 @@ func (d *NullDecimal) Scan(src any) error {
467467
var err error
468468
switch v := src.(type) {
469469
case []byte:
470-
d.Decimal, err = Parse(string(v))
470+
d.Decimal, err = parseBytes(v)
471471
case string:
472472
d.Decimal, err = Parse(v)
473473
case uint64:

0 commit comments

Comments
 (0)