Is map[string]bool optimized to map[string]struct{}{}?

1,515 views
Skip to first unread message

Peng Gao

unread,
Nov 9, 2015, 6:13:32 AM11/9/15
to golang-nuts
Is map[string]bool optimized to map[string]struct{}{}? I think map[string]struct{}{} need to contain a filed to indicate if a string in the map,I guess map[string]bool could be optimized to map[string]bool, is it like that in Go?

Naoki INADA

unread,
Nov 9, 2015, 6:22:10 AM11/9/15
to golang-nuts
false and nothing is different.


How could map[string]bool can be optimized?

Jan Mercl

unread,
Nov 9, 2015, 6:23:19 AM11/9/15
to golang-nuts
> On Mon, Nov 9, 2015 at 12:13 PM Peng Gao <peng.g...@gmail.com> wrote:
>
> Is map[string]bool optimized to map[string]struct{}{}? I think map[string]struct{}{} need to contain a filed to indicate if a string in the map,I guess map[string]bool could be optimized to map[string]bool, is it like that in Go?

map[T]bool cannot be optimized into map[T]struct{} without a change in semantics. The two valued index operation[0] on a map[T]bool can produce 3 distinct tuples, the same on map[T]struct{} only 2.


[0]: https://golang.org/ref/spec#Index_expressions

--

-j

Peng Gao

unread,
Nov 9, 2015, 6:24:07 AM11/9/15
to golang-nuts
Yes you are right.

chris dollin

unread,
Nov 9, 2015, 6:29:47 AM11/9/15
to Peng Gao, golang-nuts
On 9 November 2015 at 11:13, Peng Gao <peng.g...@gmail.com> wrote:
> Is map[string]bool optimized to map[string]struct{}{}?

No. That would be incorrect. Bools have two values but
struct{} has only one, so a struct{} map member cannot
represent a bool.

If the value associated with some string in the map is
always true if that string is a key of the map, and false
if it is not [1], /then/ you can choose to use struct{} and
not bool as the value type. But it's not an automatic
optimisation.

Chris

--
Chris "allusive" Dollin

Russ Cox

unread,
Nov 9, 2015, 11:57:05 AM11/9/15
to chris dollin, Peng Gao, golang-nuts
Yes. Also, the difference between the two is not as large as it might seem. Each individual map bucket holds an 8-byte header (partial hash codes for 8 objects), then 8 keys, then 8 values, then a pointer to an overflow bucket. On a 32-bit system, a map[string]struct{} bucket is 76 bytes and a map[string]bool bucket is 84 bytes. On a 64-bit system, a map[string]struct{} bucket is 144 bytes and a map[string]bool bucket is 152 bytes. So on a 32-bit system you'd save 9.5% of the bucket footprint, and on a 64-bit system you'd save 5.3%. It's not a lot, especially compared to the fact that the implementation tries to keep the map buckets at most 81.25% full. The savings from bool->struct{} are mostly noise.

Russ

Matt Harden

unread,
Nov 9, 2015, 10:24:13 PM11/9/15
to Russ Cox, chris dollin, Peng Gao, golang-nuts
To me the best reason to use map[string]struct{} is that you don't have to answer the question "what does it mean if the value is false?" like you do with map[string]bool. Nothing to do with performance or memory usage, just cognitive load on the programmer.

--
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.
Reply all
Reply to author
Forward
0 new messages