cosmos-sdk/tx/textual/internal/testdata/decimals.json
Emmanuel T Odeke 8c23f6f957
perf: fix: tx/textual/valuerender: use io.WriteString to skip str->byteslice + fix negative sign dropping (#12815)
Noticed in an audit that the differeent value renderers perform an
expensive and unnecessary string->byteslice in cases where the output
write implements io.StringWriter. This change instead invokes

   io.WriteString(w, formatted)

instead of:

   w.Write([]byte(formatted))

and added benchmarks that show an improvement from just the 1 line change:

```shell
$ benchstat before.txt after.txt
name                        old time/op    new time/op    delta
IntValueRendererFormat-8      4.13µs ± 3%    3.95µs ± 6%   -4.55%  (p=0.000 n=15+14)
BytesValueRendererFormat-8    5.22ms ± 3%    4.77ms ± 5%   -8.60%  (p=0.000 n=15+14)

name                        old alloc/op   new alloc/op   delta
IntValueRendererFormat-8      3.64kB ± 0%    3.31kB ± 0%   -9.01%  (p=0.000 n=15+15)
BytesValueRendererFormat-8    12.6MB ± 0%     8.4MB ± 0%  -33.22%  (p=0.000 n=15+15)

name                        old allocs/op  new allocs/op  delta
IntValueRendererFormat-8        76.0 ± 0%      67.0 ± 0%  -11.84%  (p=0.000 n=15+15)
BytesValueRendererFormat-8      27.0 ± 0%      18.0 ± 0%  -33.33%  (p=0.000 n=15+15)
```

While here, implemented negative sign preservation because previously
the code wasn't tested for negative values so passing in negative values
such as:

  "-10000000.11"

would produce:

  "10'000'000.11"

instead of the proper value with the negative sign preserved:

  "-10'000'000.11"

Fixes #12810
Fixes #12812
2022-08-04 01:27:54 -07:00

48 lines
1.6 KiB
JSON

[
["0", "0"],
["1", "1"],
["12", "12"],
["123", "123"],
["1234", "1'234"],
["0.1", "0.1"],
["0.01", "0.01"],
["0.001", "0.001"],
["0.0001", "0.0001"],
["0.00001", "0.00001"],
["0.000001", "0.000001"],
["0.0000001", "0.0000001"],
["0.00000001", "0.00000001"],
["0.000000001", "0.000000001"],
["0.0000000001", "0.0000000001"],
["0.00000000001", "0.00000000001"],
["0.000000000001", "0.000000000001"],
["0.0000000000001", "0.0000000000001"],
["0.00000000000001", "0.00000000000001"],
["0.000000000000001", "0.000000000000001"],
["0.0000000000000001", "0.0000000000000001"],
["0.00000000000000001", "0.00000000000000001"],
["0.000000000000000001", "0.000000000000000001"],
["0.100000000000000000", "0.1"],
["0.010000000000000000", "0.01"],
["0.001000000000000000", "0.001"],
["0.000100000000000000", "0.0001"],
["0.000010000000000000", "0.00001"],
["0.000001000000000000", "0.000001"],
["0.000000100000000000", "0.0000001"],
["0.000000010000000000", "0.00000001"],
["0.000000001000000000", "0.000000001"],
["0.000000000100000000", "0.0000000001"],
["0.000000000010000000", "0.00000000001"],
["0.000000000001000000", "0.000000000001"],
["0.000000000000100000", "0.0000000000001"],
["0.000000000000010000", "0.00000000000001"],
["0.000000000000001000", "0.000000000000001"],
["0.000000000000000100", "0.0000000000000001"],
["0.000000000000000010", "0.00000000000000001"],
["0.000000000000000001", "0.000000000000000001"],
["-10.0", "-10"],
["-10000", "-10'000"],
["-9999", "-9'999"],
["-999999999999", "-999'999'999'999"]
]