Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.
Sign upGitHub is where the world builds software
Millions of developers and companies build, ship, and maintain their software on GitHub — the largest and most advanced development platform in the world.
compiler/natives/src/strconv: Use js string conversion utilities to improve performance #786
Conversation
// Bounds checking | ||
floatval := jsValue.Float() | ||
if floatval > maxInt32 { | ||
return 1<<31 - 1, rangeError(fnAtoi, s) |
flimzy
Apr 4, 2018
•
Member
Does it make sense to replace 1<<31 - 1
with the maxInt32
const here? And similar for minInt32
below?
if len(s) == 0 { | ||
return 0, syntaxError(fnAtoi, s) | ||
} | ||
jsValue := js.Global.Call("Number", s, 10) |
flimzy
Apr 4, 2018
Member
I believe JS's Number
constructor is far more forgiving than Go's Atoi
implementation. Is it therefore reasonable to validate that s
contains only Go-acceptable numbers?
Some examples of values that Number()
accepts but Atoi()
should not:
- "0x10"
- "10.2"
- "0b11"
- "0o10"
lologarithm
Apr 4, 2018
Author
Contributor
Probably should do some input validation first - if the resulting code is slower than just using Atoi directly I will probably remove this piece of code from the PR
lologarithm
Apr 4, 2018
Author
Contributor
Validate doesn't seem to have hurt perf at all -- pushing up the changes.
flimzy
Apr 4, 2018
Member
Looks like a good improvement. And glad to hear it doesn't have a noticeable impact on performance!
@shurcooL another optimization PR for you :) |
Conflicts: compiler/natives/fs_vfsdata.go
Added native implemntations for Atoi and Itoa. This generates smaller and faster code.
Before (using go stdlib)
After (using new js native)
I also wrote a small Itoa benchmark since there wasn't one in std lib. This uses 123456789 as the test number