Skip to content

Improve marshal speed #47

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

Merged
merged 5 commits into from
Nov 24, 2021
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
98 changes: 98 additions & 0 deletions appendfloat_f.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// Modified for 'f' format with inlined variables.

package simdjson

import "math"

func appendFloatF(dst []byte, val float64) []byte {
var prec int
var bits uint64
bits = math.Float64bits(val)
//var float64info = floatInfo{mantbits: 52, expbits: 11, bias: -1023}
const mantbits = 52
const expbits = 11
const bias = -1023
//flt = &float64info

neg := bits>>(expbits+mantbits) != 0
exp := int(bits>>mantbits) & (1<<expbits - 1)
mant := bits & (uint64(1)<<mantbits - 1)

switch exp {
case 0:
// denormalized
exp++

default:
// add implicit top bit
mant |= uint64(1) << mantbits
}
exp += bias

var digs decimalSlice
// Use Ryu algorithm.
var buf [32]byte
digs.d = buf[:]
ryuFtoaShortest(&digs, mant, exp-mantbits)
// Precision for shortest representation mode.

prec = max(digs.nd-digs.dp, 0)
return fmtF(dst, neg, digs, prec)
}

type decimalSlice struct {
d []byte
nd, dp int
neg bool
}

// %f: -ddddddd.ddddd
func fmtF(dst []byte, neg bool, d decimalSlice, prec int) []byte {
// sign
if neg {
dst = append(dst, '-')
}

// integer, padded with zeros as needed.
if d.dp > 0 {
m := min(d.nd, d.dp)
dst = append(dst, d.d[:m]...)
for ; m < d.dp; m++ {
dst = append(dst, '0')
}
} else {
dst = append(dst, '0')
}

// fraction
if prec > 0 {
dst = append(dst, '.')
for i := 0; i < prec; i++ {
ch := byte('0')
if j := d.dp + i; 0 <= j && j < d.nd {
ch = d.d[j]
}
dst = append(dst, ch)
}
}

return dst
}

func min(a, b int) int {
if a < b {
return a
}
return b
}

func max(a, b int) int {
if a > b {
return a
}
return b
}
Loading