Skip to content

Commit

Permalink
Special case Comma(math.MinInt64).
Browse files Browse the repository at this point in the history
The lowest value of an int64 is itself when negated, so the rest of
the code kind of breaks down.  Short circuit it to avoid all the problems.

https://play.golang.org/p/AuAGwhJ69d

Closes: #45
  • Loading branch information
dustin committed Jan 6, 2017
1 parent ef638b6 commit 904a494
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
7 changes: 7 additions & 0 deletions comma.go
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package humanize


import ( import (
"bytes" "bytes"
"math"
"math/big" "math/big"
"strconv" "strconv"
"strings" "strings"
Expand All @@ -13,6 +14,12 @@ import (
// e.g. Comma(834142) -> 834,142 // e.g. Comma(834142) -> 834,142
func Comma(v int64) string { func Comma(v int64) string {
sign := "" sign := ""

// minin64 can't be negated to a usable value, so it has to be special cased.
if v == math.MinInt64 {
return "-9,223,372,036,854,775,808"
}

This comment has been minimized.

Copy link
@dmitshur

dmitshur Jan 6, 2017

Collaborator

As an observation, this special case could've been placed above the declaration of sign variable since it's unused.

There's also a typo in "minin64".

This comment has been minimized.

Copy link
@dustin

dustin via email Jan 6, 2017

Author Owner

This comment has been minimized.

Copy link
@dmitshur

dmitshur Jan 6, 2017

Collaborator

Moving the declaration doesn't change the compiled code,

Looks like you're right. I thought it might cause an extra unnecessary allocation, but a quick benchmark
with -benchmem says there are 0 allocs/op.

This comment has been minimized.

Copy link
@dustin

dustin via email Jan 6, 2017

Author Owner

This comment has been minimized.

Copy link
@mvdan

mvdan Jan 7, 2017

@shurcooL what's your logic there? why would an empty string ever cause an alloc?

This comment has been minimized.

Copy link
@dmitshur

dmitshur Jan 7, 2017

Collaborator

@mvdan I'm not very well-versed with allocations. I thought there was a chance that declaring a variable could cause it to be allocated (on the stack, in this case).

This comment has been minimized.

Copy link
@mvdan

mvdan Jan 8, 2017

Oh. In general, by alloc people always mean the heap. That's what incurs the most runtime penalty and the GC work. -benchmem and all other alloc stuff from Go's tooling is always the heap, since it's about those two. Otherwise it would be "stack size" or something similar, which affects the compiled function.

This change is valid as far as code correctness goes, but I wouldn't fix it for performance reasons unless a benchmark showed so. And even then, it would probably be because the compiler doesn't do all the magic it could yet.

This comment has been minimized.

Copy link
@dmitshur

dmitshur Nov 11, 2017

Collaborator

There's also a typo in "minin64".

This has been fixed in #64.


if v < 0 { if v < 0 {
sign = "-" sign = "-"
v = 0 - v v = 0 - v
Expand Down
2 changes: 2 additions & 0 deletions comma_test.go
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ func TestCommas(t *testing.T) {
{"10,001,000", Comma(10001000), "10,001,000"}, {"10,001,000", Comma(10001000), "10,001,000"},
{"123,456,789", Comma(123456789), "123,456,789"}, {"123,456,789", Comma(123456789), "123,456,789"},
{"maxint", Comma(9.223372e+18), "9,223,372,000,000,000,000"}, {"maxint", Comma(9.223372e+18), "9,223,372,000,000,000,000"},
{"math.maxint", Comma(math.MaxInt64), "9,223,372,036,854,775,807"},
{"math.minint", Comma(math.MinInt64), "-9,223,372,036,854,775,808"},
{"minint", Comma(-9.223372e+18), "-9,223,372,000,000,000,000"}, {"minint", Comma(-9.223372e+18), "-9,223,372,000,000,000,000"},
{"-123,456,789", Comma(-123456789), "-123,456,789"}, {"-123,456,789", Comma(-123456789), "-123,456,789"},
{"-10,100,000", Comma(-10100000), "-10,100,000"}, {"-10,100,000", Comma(-10100000), "-10,100,000"},
Expand Down

0 comments on commit 904a494

Please sign in to comment.