Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #39 from tdakkota/feat/reduce-float-allocation
feat: reduce float decoding allocations by using unsafe
- Loading branch information
Showing
6 changed files
with
91 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
//go:build appengine || purego | ||
|
||
package jx | ||
|
||
import ( | ||
"strconv" | ||
|
||
"github.com/go-faster/errors" | ||
) | ||
|
||
func (d *Decoder) floatSlow(size int) (float64, error) { | ||
var buf [32]byte | ||
|
||
str, err := d.numberAppend(buf[:0]) | ||
if err != nil { | ||
return 0, errors.Wrap(err, "number") | ||
} | ||
if err := validateFloat(str); err != nil { | ||
return 0, errors.Wrap(err, "invalid") | ||
} | ||
|
||
val, err := strconv.ParseFloat(string(str), size) | ||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
return val, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
//go:build !appengine && !purego | ||
|
||
package jx | ||
|
||
import ( | ||
"strconv" | ||
"unsafe" | ||
|
||
"github.com/go-faster/errors" | ||
) | ||
|
||
func (d *Decoder) floatSlow(size int) (float64, error) { | ||
var buf [32]byte | ||
|
||
str, err := d.numberAppend(buf[:0]) | ||
if err != nil { | ||
return 0, errors.Wrap(err, "number") | ||
} | ||
if err := validateFloat(str); err != nil { | ||
return 0, errors.Wrap(err, "invalid") | ||
} | ||
|
||
slice := *(*sliceType)(unsafe.Pointer(&str)) // #nosec G103 | ||
s := strType{ | ||
Ptr: noescape(slice.Ptr), | ||
Len: slice.Len, | ||
} | ||
val, err := strconv.ParseFloat(*(*string)(unsafe.Pointer(&s)), size) // #nosec G103 | ||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
return val, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
//go:build !appengine && !purego | ||
|
||
package jx | ||
|
||
import "unsafe" | ||
|
||
type sliceType struct { | ||
Ptr unsafe.Pointer | ||
Len uintptr | ||
Cap uintptr | ||
} | ||
|
||
type strType struct { | ||
Ptr unsafe.Pointer | ||
Len uintptr | ||
} | ||
|
||
//go:linkname noescape runtime.noescape | ||
func noescape(unsafe.Pointer) unsafe.Pointer |