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

Issue 7722046: code review 7722046: runtime: allocate first bucket table lazily (Closed)

Can't Edit
Can't Publish+Mail
Start Review
Created:
11 years, 1 month ago by bradfitz
Modified:
11 years, 1 month ago
Reviewers:
mtj1
CC:
khr, golang-dev, r
Visibility:
Public.

Description

runtime: allocate maps' first bucket table lazily Motivated by garbage profiling in HTTP benchmarks. This changes means new empty maps are just one small allocation (the HMap) instead the HMap + the relatively larger h->buckets allocation. This helps maps which remain empty throughout their life. benchmark old ns/op new ns/op delta BenchmarkNewEmptyMap 196 107 -45.41% benchmark old allocs new allocs delta BenchmarkNewEmptyMap 2 1 -50.00% benchmark old bytes new bytes delta BenchmarkNewEmptyMap 195 50 -74.36%

Patch Set 1 #

Patch Set 2 : diff -r 05653fa6bcc4 https://go.googlecode.com/hg/ #

Patch Set 3 : diff -r 05653fa6bcc4 https://go.googlecode.com/hg/ #

Patch Set 4 : diff -r 3d477c8de1c2 https://go.googlecode.com/hg/ #

Unified diffs Side-by-side diffs Delta from patch set Stats (+33 lines, -5 lines) Patch
M src/pkg/runtime/hashmap.c View 1 6 chunks +26 lines, -5 lines 0 comments Download
M src/pkg/runtime/map_test.go View 1 1 chunk +7 lines, -0 lines 0 comments Download

Messages

Total messages: 10
bradfitz
Hello khr@golang.org, golang-dev@googlegroups.com (cc: golang-dev@googlegroups.com), I'd like you to review this change to https://go.googlecode.com/hg/
11 years, 1 month ago (2013-03-27 22:46:48 UTC) #1
r
why?
11 years, 1 month ago (2013-03-27 22:47:33 UTC) #2
bradfitz
To make empty maps cheaper. I noticed this during garbage profiling of the HTTP server. ...
11 years, 1 month ago (2013-03-27 22:53:45 UTC) #3
khr
LGTM
11 years, 1 month ago (2013-03-27 22:55:52 UTC) #4
bradfitz
*** Submitted as https://code.google.com/p/go/source/detail?r=6efdf4f2924b *** runtime: allocate maps' first bucket table lazily Motivated by garbage ...
11 years, 1 month ago (2013-03-27 23:28:56 UTC) #5
mtj1
Brad, I have a question about your analysis. Do you have any sense how commonly ...
11 years, 1 month ago (2013-03-28 14:17:44 UTC) #6
mtj1
[...grrr...] if a, present := map[index]; if present { // or !present .... map[index] = ...
11 years, 1 month ago (2013-03-28 14:20:18 UTC) #7
bradfitz
On Thu, Mar 28, 2013 at 7:19 AM, Michael Jones <mtj@google.com> wrote: > > if ...
11 years, 1 month ago (2013-03-28 15:08:32 UTC) #8
mtj1
Cool, thanks. If we had this then such loops might be more than twice as ...
11 years, 1 month ago (2013-03-28 15:23:40 UTC) #9
bradfitz
11 years, 1 month ago (2013-03-29 17:10:58 UTC) #10
I've prototyped this in https://codereview.appspot.com/8160043 if you want
to play.  The patch isn't very clean yet.  I'm not proposing it for Go 1.1.

On Thu, Mar 28, 2013 at 8:23 AM, Michael Jones <mtj@google.com> wrote:

> Cool, thanks. If we had this then such loops might be more than twice as
> fast--you'd know the hash value and could know the exact point to do the
> insertion (when the bucket was not full). The second map call would largely
> be a pointer copy and an index update.
>
>
> On Thu, Mar 28, 2013 at 8:08 AM, Brad Fitzpatrick <bradfitz@golang.org>wrote:
>
>> On Thu, Mar 28, 2013 at 7:19 AM, Michael Jones <mtj@google.com> wrote:
>>
>>>
>>> if a, present := map[index]; if present { // or !present
>>>     ....
>>>     map[index] = newValue
>>> }
>>>
>>> It seems that I'm doing this maybe half of the time and there is the
>>> opportunity to do just one key value computation in such cases. (with an
>>> appropriate way of saying so or caching the result and comparing keys.)
>>>
>>> Just wondering of your analysis shows this to be frequent.
>>>
>>
>> I didn't specifically look for that, but I agree that such a pattern is
>> common.  I do that all the time as well.
>>
>> That's an interesting optimization idea (not for Go 1.1).  It would
>> likely enlarge Hmap, so it would really need to be worth it to justify
>> that, though.  The cost of caching the last key's hash value in Hmap could
>> be avoided in most cases since the pattern you describe above is known
>> statically.  The compiler could pass a hint to the runtime map functions
>> when the pattern above is detected.  Actually, you probably wouldn't even
>> need to enlarge and put it in Hmap--- the cached hash value could be saved
>> in the P.
>>
>> I've filed https://code.google.com/p/go/issues/detail?id=5147
>>
>
>
>
> --
> Michael T. Jones | Chief Technology Advocate  | mtj@google.com |  +1
> 650-335-5765
>
Sign in to reply to this message.

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