Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(330)

Issue 5570044: code review 5570044: math/big: slight improvement to algorithm used for inte...

Can't Edit
Can't Publish+Mail
Start Review
Created:
13 years, 5 months ago by dga
Modified:
13 years, 5 months ago
Reviewers:
brtzsnr, gri
CC:
golang-dev, dave_cheney.net, eds2, gri
Visibility:
Public.

Description

math/big: slight improvement to algorithm used for internal bitLen function The bitLen function currently shifts out blocks of 8 bits at a time. This change replaces this sorta-linear algorithm with a log(N) one (shift out 16 bits, then 8, then 4, then 2, then 1). I left the start of it linear at 16 bits at a time so that the function continues to work with 32 or 64 bit values without any funkiness. The algorithm is similar to several of the nlz ("number of leading zeros") algorithms from "Hacker's Delight" or the "bit twiddling hacks" pages. Doesn't make a big difference to the existing benchmarks, but I'm using the code in a different context that calls bitLen much more often, so it seemed worthwhile making the existing codebase faster so that it's a better building block. Microbenchmark results on a 64-bit Macbook Pro using 6g from weekly.2012-01-20: benchmark old ns/op new ns/op delta big.BenchmarkBitLen0 4 6 +50.12% big.BenchmarkBitLen1 4 6 +33.91% big.BenchmarkBitLen2 6 6 +3.05% big.BenchmarkBitLen3 7 6 -19.05% big.BenchmarkBitLen4 9 6 -30.19% big.BenchmarkBitLen5 11 6 -42.23% big.BenchmarkBitLen8 16 6 -61.78% big.BenchmarkBitLen9 5 6 +18.29% big.BenchmarkBitLen16 18 7 -60.99% big.BenchmarkBitLen17 7 6 -4.64% big.BenchmarkBitLen31 19 7 -62.49% On an ARM machine (with the previous weekly): benchmark old ns/op new ns/op delta big.BenchmarkBitLen0 37 50 +36.56% big.BenchmarkBitLen1 59 51 -13.69% big.BenchmarkBitLen2 74 59 -20.40% big.BenchmarkBitLen3 92 60 -34.89% big.BenchmarkBitLen4 110 59 -46.09% big.BenchmarkBitLen5 127 60 -52.68% big.BenchmarkBitLen8 181 59 -67.24% big.BenchmarkBitLen9 78 60 -23.05% big.BenchmarkBitLen16 199 69 -65.13% big.BenchmarkBitLen17 91 70 -23.17% big.BenchmarkBitLen31 210 95 -54.43%

Patch Set 1 #

Patch Set 2 : diff -r 9f2be4fbbf69 https://go.googlecode.com/hg/ #

Patch Set 3 : diff -r 63a6abde14b1 https://go.googlecode.com/hg/ #

Patch Set 4 : diff -r 63a6abde14b1 https://go.googlecode.com/hg/ #

Total comments: 2

Patch Set 5 : diff -r 63a6abde14b1 https://go.googlecode.com/hg/ #

Unified diffs Side-by-side diffs Delta from patch set Stats (+36 lines, -2 lines) Patch
M src/pkg/math/big/arith.go View 1 1 chunk +14 lines, -2 lines 0 comments Download
M src/pkg/math/big/arith_test.go View 1 2 3 4 1 chunk +22 lines, -0 lines 0 comments Download

Messages

Total messages: 18
dga
Hello golang-dev@googlegroups.com (cc: golang-dev@googlegroups.com), I'd like you to review this change to https://go.googlecode.com/hg/
13 years, 5 months ago (2012-01-23 06:32:23 UTC) #1
dga
Must be close to bedtime. In the microbenchmark results, Bitl2 is the *old* implementation, obviously.
13 years, 5 months ago (2012-01-23 06:35:57 UTC) #2
dave_cheney.net
Excellent, very pleased to see the results on arm are also improved.
13 years, 5 months ago (2012-01-23 07:37:59 UTC) #3
eds2
If this makes a big difference for you, you could implement an i386/amd64 assembly version ...
13 years, 5 months ago (2012-01-23 09:33:23 UTC) #4
gri
This looks good, but I remember having done exactly the same when I tweaked the ...
13 years, 5 months ago (2012-01-23 17:20:51 UTC) #5
dga
What range of numbers do you mean for "small?" The benchmark results I posted above ...
13 years, 5 months ago (2012-01-23 18:25:44 UTC) #6
gri
On Mon, Jan 23, 2012 at 10:25 AM, <dave.andersen@gmail.com> wrote: > What range of numbers ...
13 years, 5 months ago (2012-01-23 18:53:33 UTC) #7
dga
Before I add another CL, is this roughly the benchmark you hoped for? [Results from ...
13 years, 5 months ago (2012-01-23 19:24:31 UTC) #8
gri
Yes, this looks good. You probably don't need that many different ones. Given your new ...
13 years, 5 months ago (2012-01-23 19:30:11 UTC) #9
dga
Hello golang-dev@googlegroups.com, dave@cheney.net, edsrzf@gmail.com, gri@golang.org (cc: golang-dev@googlegroups.com), Please take another look.
13 years, 5 months ago (2012-01-23 20:03:46 UTC) #10
gri
Looks good. A couple of minor suggestions. Please also update the CL description by running ...
13 years, 5 months ago (2012-01-23 20:37:03 UTC) #11
dga
On 2012/01/23 20:37:03, gri wrote: > Looks good. A couple of minor suggestions. > > ...
13 years, 5 months ago (2012-01-23 20:47:29 UTC) #12
dga
Hello golang-dev@googlegroups.com, dave@cheney.net, edsrzf@gmail.com, gri@golang.org (cc: golang-dev@googlegroups.com), Please take another look.
13 years, 5 months ago (2012-01-23 21:00:07 UTC) #13
gri
LGTM. Thanks for bearing with me. - gri
13 years, 5 months ago (2012-01-23 21:43:51 UTC) #14
gri
*** Submitted as 408491a4bde1 *** math/big: slight improvement to algorithm used for internal bitLen function ...
13 years, 5 months ago (2012-01-23 21:46:30 UTC) #15
dave_cheney.net
That's a great win for arm, thanks a lot. Sent from my iPhone On 24/01/2012, ...
13 years, 5 months ago (2012-01-23 22:26:03 UTC) #16
brtzsnr
Sorry for replying late, but if have already you check the algorithms from http://graphics.stanford.edu/~seander/bithacks.html#IntegerLogLookup On ...
13 years, 5 months ago (2012-01-24 21:05:35 UTC) #17
gri
13 years, 5 months ago (2012-01-24 21:58:54 UTC) #18
It's not clear that a lookup table approach is much faster; also, in
the meantime we have an assembly version CL to be reviewed. At the end
of the day, measurement is the only way to find out.

Thanks for pointing this out.
- gri

On Tue, Jan 24, 2012 at 1:05 PM,  <brtzsnr@gmail.com> wrote:
> Sorry for replying late, but if have already you check the algorithms
> from
> http://graphics.stanford.edu/~seander/bithacks.html#IntegerLogLookup
>
>
> On 2012/01/23 22:26:03, dfc wrote:
>>
>> That's a great win for arm, thanks a lot.
>
>
>> Sent from my iPhone
>
>
>> On 24/01/2012, at 8:46, mailto:gri@golang.org wrote:
>
>
>> > *** Submitted as 408491a4bde1 ***
>> >
>> > math/big: slight improvement to algorithm used for internal bitLen
>> > function
>> >
>> > The bitLen function currently shifts out blocks of 8 bits at a time.
>> > This change replaces this sorta-linear algorithm with a log(N)
>> > one (shift out 16 bits, then 8, then 4, then 2, then 1).
>> > I left the start of it linear at 16 bits at a time so that
>> > the function continues to work with 32 or 64 bit values
>> > without any funkiness.
>> > The algorithm is similar to several of the nlz ("number of
>> > leading zeros") algorithms from "Hacker's Delight" or the
>> > "bit twiddling hacks" pages.
>> >
>> > Doesn't make a big difference to the existing benchmarks, but
>> > I'm using the code in a different context that calls bitLen
>> > much more often, so it seemed worthwhile making the existing
>> > codebase faster so that it's a better building block.
>> >
>> > Microbenchmark results on a 64-bit Macbook Pro using 6g from
>> > weekly.2012-01-20:
>> >
>> > benchmark                old ns/op    new ns/op    delta
>> > big.BenchmarkBitLen0             4            6  +50.12%
>> > big.BenchmarkBitLen1             4            6  +33.91%
>> > big.BenchmarkBitLen2             6            6   +3.05%
>> > big.BenchmarkBitLen3             7            6  -19.05%
>> > big.BenchmarkBitLen4             9            6  -30.19%
>> > big.BenchmarkBitLen5            11            6  -42.23%
>> > big.BenchmarkBitLen8            16            6  -61.78%
>> > big.BenchmarkBitLen9             5            6  +18.29%
>> > big.BenchmarkBitLen16           18            7  -60.99%
>> > big.BenchmarkBitLen17            7            6   -4.64%
>> > big.BenchmarkBitLen31           19            7  -62.49%
>> >
>> > On an ARM machine (with the previous weekly):
>> >
>> > benchmark                old ns/op    new ns/op    delta
>> > big.BenchmarkBitLen0            37           50  +36.56%
>> > big.BenchmarkBitLen1            59           51  -13.69%
>> > big.BenchmarkBitLen2            74           59  -20.40%
>> > big.BenchmarkBitLen3            92           60  -34.89%
>> > big.BenchmarkBitLen4           110           59  -46.09%
>> > big.BenchmarkBitLen5           127           60  -52.68%
>> > big.BenchmarkBitLen8           181           59  -67.24%
>> > big.BenchmarkBitLen9            78           60  -23.05%
>> > big.BenchmarkBitLen16          199           69  -65.13%
>> > big.BenchmarkBitLen17           91           70  -23.17%
>> > big.BenchmarkBitLen31          210           95  -54.43%
>> >
>> > R=golang-dev, dave, edsrzf, gri
>> > CC=golang-dev
>> > http://codereview.appspot.com/5570044
>> >
>> > Committer: Robert Griesemer <mailto:gri@golang.org>
>> >
>> >
>> > http://codereview.appspot.com/5570044/
>
>
>
>
> http://codereview.appspot.com/5570044/
Sign in to reply to this message.

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b