what's the fastest way for clearing a map

9,079 views
Skip to first unread message

Daniel Mal

unread,
Aug 3, 2014, 9:17:09 AM8/3/14
to golan...@googlegroups.com
my app made use of maps frequently, it's like

1. put some k,v into map
2. process the map
3. loop delete each key in the map, and goto step 1

what is the fastest way for clearing a map, and also i don't want to make(map) each time to give GC too much pressure.

James Wendel

unread,
Aug 3, 2014, 9:38:46 AM8/3/14
to golan...@googlegroups.com
There's no simple and easy way to clear a map that I'm aware of. It is normally just easier to create a new one. Alternatively, are you able to solve your problem using arrays/slices?

Sameer Ajmani

unread,
Aug 3, 2014, 10:00:09 AM8/3/14
to James Wendel, golang-nuts

If your goal is to create an empty map that can store the same number of keys as the map used in the previous iteration, pass the desired size for the new map as the second parameter to make.  That will reduce or eliminate the number of times the map is resized each round.

But if you have relatively few key collisions each round, it might be more efficient to append your items to a slice then sort them at the end to identify duplicates.  Then just reslice down to zero at the start of each round to reuse the underlying array.

Profile your code and see.

On Aug 3, 2014 9:38 AM, "James Wendel" <jmwe...@gmail.com> wrote:
There's no simple and easy way to clear a map that I'm aware of. It is normally just easier to create a new one. Alternatively, are you able to solve your problem using arrays/slices?

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

k...@golang.org

unread,
Aug 3, 2014, 11:36:59 PM8/3/14
to golan...@googlegroups.com, jmwe...@gmail.com
for k := range m { delete(m, k) }

should work fine.  It will probably be faster to create a new (correctly sized, if you know it) map, but reusing can put less pressure on the garbage collector.

Rob Pike

unread,
Aug 4, 2014, 10:26:55 AM8/4/14
to k...@golang.org, golan...@googlegroups.com, jmwe...@gmail.com
Does it always put significantly less pressure on the collector to run
the loop? The amount certainly depends on the types of the key and
value, since a large map of complex types can contain a lot of
pointers and such. The overhead of the buckets could be anything from
dominant to minor as I understand things.

For a simple map, like map[string]int, it's likely that the bucket
overhead is relatively large.

-rob

dja...@gmail.com

unread,
Aug 4, 2014, 11:14:33 AM8/4/14
to golan...@googlegroups.com, jmwe...@gmail.com

if i have server that process N (big value) maps per second,  to reuse or make new map ?

Sameer Ajmani

unread,
Aug 4, 2014, 11:26:02 AM8/4/14
to dja...@gmail.com, James Wendel, golang-nuts

Perhaps use a sync.Pool to recycle maps in your server.

--

Dave Cheney

unread,
Aug 4, 2014, 8:29:04 PM8/4/14
to golan...@googlegroups.com, jmwe...@gmail.com, dja...@gmail.com
make() a new one is faster than emptying the old one. Both result in the same amount of garbage, but the former does less paperwork beforehand.

Brad Fitzpatrick

unread,
Aug 4, 2014, 8:37:23 PM8/4/14
to Dave Cheney, golang-nuts, jmwe...@gmail.com, dja...@gmail.com
On Mon, Aug 4, 2014 at 5:29 PM, Dave Cheney <da...@cheney.net> wrote:
make() a new one is faster than emptying the old one. Both result in the same amount of garbage, but the former does less paperwork beforehand.

Why does emptying the old one make any garbage?
 

Dave Cheney

unread,
Aug 4, 2014, 8:53:29 PM8/4/14
to Brad Fitzpatrick, jmwe...@gmail.com, golang-nuts, dja...@gmail.com

I had assumed that the OP was talking about a map with indirect keys/values, in that case I believe those keys/values will become garbage after deletion. At least in 1.4 when there is no more free method inside the runtime.

Reply all
Reply to author
Forward
0 new messages