What's the best way to reuse large map

591 views
Skip to first unread message

Zhang,Lei(Ecom)

unread,
Sep 11, 2014, 11:05:30 PM9/11/14
to golan...@googlegroups.com

I need use a very large map in a function and the function will be repeatedly called many times.

At the beginning of the function, the map need be empty.

One way to do it is to use/reuse just one permanent map object and clear it at the beginning of the function.

Another way is to create a new map object in the function.

Since the map is very large, I'd like to save some memory and keep the burden on GC low.

So I guess the first way is preferred. But I am not sure whether clearing a map and reusing it is really memory-efficient

compared to creating a new map object in each call. It depends on how map is implemented internally.

 

Any ideas and suggestions ?

 

Thanks !

 

Zhang Lei

Baidu, Inc.

 

 

 

Andrew Gerrand

unread,
Sep 11, 2014, 11:14:14 PM9/11/14
to Zhang,Lei(Ecom), golan...@googlegroups.com
The best approach for you depends on many factors. The best way to find out is to try re-allocating vs re-using a map and profiling the performance. 


Andrew

--
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.

Caleb Spare

unread,
Sep 11, 2014, 11:14:15 PM9/11/14
to Zhang,Lei(Ecom), golan...@googlegroups.com
Note that "clearing" a map means iterating through it and deleting the
entries one-by-one.

Also note that you can give a map a size hint: make(map[X]Y, 10000).
That way the map may be pre-sized and will hopefully do fewer
allocations/copying as it grows.

You could use the previous map size to know how large to hint in the
next iteration, for example.

If I were you, I would compare these two approaches:

1. Clear the map between each iteration
2. Throw away the map each iteration but use its size as a hint for
making the new map

Writing a benchmark is the best way to see what's going to happen in
your particular situation. I would expect that #2 is the fastest.

-Caleb

Daniel Mal

unread,
Sep 11, 2014, 11:17:00 PM9/11/14
to golan...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages