Size of value in map[string]struct{}

1,008 views
Skip to first unread message

bobu...@gmail.com

unread,
May 31, 2015, 5:59:11 PM5/31/15
to golan...@googlegroups.com
Hello. I would like to use map[...]... to implement the most memory efficient implementation of set.

I am considering using 
map[string]struct{}

and

map[string]bool

I know that struct{} has 0 size, but does that hold when it is value of the map or the value's size is always min. 1 byte??


k...@golang.org

unread,
May 31, 2015, 7:38:02 PM5/31/15
to golan...@googlegroups.com, bobu...@gmail.com
Yes, struct{} uses 0 bytes even in maps.  It won't make a big difference, though, as strings are 16 bytes (on 64-bit platforms), not including their contents.  So you're talking about 6% extra space at worst to use bools.

Egon

unread,
Jun 1, 2015, 2:31:09 AM6/1/15
to golan...@googlegroups.com, bobu...@gmail.com
On Monday, 1 June 2015 00:59:11 UTC+3, bobu...@gmail.com wrote:
Hello. I would like to use map[...]... to implement the most memory efficient implementation of set.

What do you mean by "most memory efficient"?, i.e. for what purpose it's most efficient?
What are the strings being stored?
How do you need to access them?
How are they updated? Serially, randomly?
How often are they updated?
...

Without understanding those aspects, it will be hard to get to the best implementation.

+ Egon

Roberto Zanotto

unread,
Jun 1, 2015, 11:33:10 AM6/1/15
to golan...@googlegroups.com, bobu...@gmail.com
I don't think that there's a noticeable difference in terms of memory "efficiency" (I think you are prematurely optimizing here :P). Chose the one that makes more sense semantically. I would personally prefer struct{}: if a key is present, that's in the set, if not present it's not in the set. Bool may create ambiguities as a key may be not present or it may be present and true or it may be present and false.
Cheers.

Roberto Zanotto

unread,
Jun 1, 2015, 11:49:35 AM6/1/15
to golan...@googlegroups.com, bobu...@gmail.com
By the way, it holds for arrays of struct{}, I don't see why it should not hold for maps ;)
https://play.golang.org/p/k5D9G2IETb


On Sunday, May 31, 2015 at 11:59:11 PM UTC+2, bobu...@gmail.com wrote:
[...] 

Paul Borman

unread,
Jun 1, 2015, 12:11:35 PM6/1/15
to Roberto Zanotto, golang-nuts, bobu...@gmail.com
Maps have keys, array do not :-)

BTW make(chan struct{}, 1e9) will work fine, that is where I really find the space savings valuable.

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

Roberto Zanotto

unread,
Jun 1, 2015, 12:29:50 PM6/1/15
to golan...@googlegroups.com, bobu...@gmail.com, roby...@gmail.com
Well of course :P what I meant is that I expect that a map[string]struct{} doesn't use any memory to store empty structs
Reply all
Reply to author
Forward
0 new messages