Re: [go-nuts] Need a way to check only key exist or not? without value?
On Wednesday, 29 April 2020 01:33:55 UTC+1, Shishir Verma wrote: > > I think it is kind of intuitive that empty struct takes 0 bytes > To me it wasn't intuitive, but that's because my brain instinctively read it as "interface {}" and not "struct {}". It's clear that a struct{} must occupy zero bytes. It's not clear that such a construct is even legal, but it clearly is. You can define a variable that takes zero bytes :-) You can also define a zero-sized array, which achieves the same thing. https://play.golang.org/p/16QuEbLj_89 Thanks for the trick! -- 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+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/d06c536a-8b4c-47a2-a40f-d145e6ef8218%40googlegroups.com.
Re: [go-nuts] Need a way to check only key exist or not? without value?
I think it is kind of intuitive that empty struct takes 0 bytes whereas a bool variable takes 1 byte in memory and hence a map with struct{} values would consume lesser memory than the other. I tried checking this using code and Randall's point proves: mapmem.go: package main import ( "fmt" _ "net/http/pprof" "runtime" "unsafe" ) const ( entries = 101 ) func main() { printAllocs() //Empty struct takes 0 bytes in memory whereas a boolean takes 1 s := struct{}{} b := true fmt.Printf("size of empty struct: %T, %d\n", s, unsafe.Sizeof(s)) fmt.Printf("size of a boolean: %T, %d\n", b, unsafe.Sizeof(b)) printAllocs() //Map with int keys and bool values hashset := make(map[int]bool, entries) for index := 0; index < entries-1; index++ { hashset[index] = true } fmt.Printf("Number of elements in map with bool values: %d \n", len( hashset)) printAllocs() //Map with int keys and empty struct values hashmap := make(map[int]struct{}, entries) for index := 0; index < entries-1; index++ { hashmap[index] = struct{}{} } fmt.Printf("Number of elements in map with empty struct values: %d \n", len(hashmap)) printAllocs() } func printAllocs() { var m runtime.MemStats runtime.ReadMemStats(&m) fmt.Printf("Heap size: %6d \n", m.Alloc/1e6) } And here is the output: (Please note that the GC runs and collects memory in between most of the times and hence the total heap size shows 22MB rather than 47MB) $ GODEBUG=gctrace=1 ./mapmem Heap size: 0 size of empty struct: struct {}, 0 size of a boolean: bool, 1 Heap size: 0 gc 1 @0.002s 1%: 0.002+0.25+0.019 ms clock, 0.009+0.11/0.045/0.37+0.079 ms cpu, 23->23->23 MB, 24 MB goal, 4 P Number of elements in map with bool values: 100 Heap size: 24 gc 2 @0.129s 0%: 0.003+0.25+0.018 ms clock, 0.012+0.076/0.066/0.22+0.072 ms cpu, 44->44->21 MB, 47 MB goal, 4 P Number of elements in map with empty struct values: 100 Heap size: 22 As you can see, the map with struct{} values grows the heap size to ~22 MB, whereas the map with bool values takes it close to ~24 MB for a million entries. Hence, if you are concerned about memory usage, you would rather use empty struct values. I still feel a map with bool values is easier to read though, but that's just my opinion. :) On Wednesday, 29 April 2020 00:18:12 UTC+5:30, adithya...@gmail.com wrote: > > > is it mentioned anywhere such that "map[string]struct{}" is efficeient? > > On Tuesday, April 28, 2020 at 10:23:08 AM UTC+5:30, Randall O'Reilly wrote: >> >> I think map[string]struct{} takes no storage for the value and is the >> most efficient way to do this. >> >> - Randy >> >> > On Apr 27, 2020, at 7:20 PM, Shishir Verma wrote: >> > >> > I think the idiomatic way to implement a set in golang is to use a map >> with bool values. Here is an example from effective go documentation: >> > >> > >> > attended := map[string]bool{ >> > "Ann": true, >> > "Joe": true, >> > ... >> > } >> > >> > if attended[person] { // will be false if person is not in the map >> > fmt.Println(person, "was at the meeting") >> > } >> > >> > >> > >> > On Monday, 27 April 2020 22:16:20 UTC+5:30, adithya...@gmail.com >> wrote: >> > Basically i need a slice with indexed values, so that i can check only >> existence. >> > or a map with only keys? >> > How it can be done? >> > >> > -- >> > 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 golan...@googlegroups.com. >> > To view this discussion on the web visit >> https://groups.google.com/d/msgid/golang-nuts/1201e6f3-621e-4875-9374-d7713fa7d8aa%40googlegroups.com. >> >> >> >> -- 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+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/afa734a9-edab-4358-aed9-de4a80a1c3a5%40googlegroups.com.
Re: [go-nuts] Need a way to check only key exist or not? without value?
is it mentioned anywhere such that "map[string]struct{}" is efficeient? On Tuesday, April 28, 2020 at 10:23:08 AM UTC+5:30, Randall O'Reilly wrote: > > I think map[string]struct{} takes no storage for the value and is the most > efficient way to do this. > > - Randy > > > On Apr 27, 2020, at 7:20 PM, Shishir Verma > wrote: > > > > I think the idiomatic way to implement a set in golang is to use a map > with bool values. Here is an example from effective go documentation: > > > > > > attended := map[string]bool{ > > "Ann": true, > > "Joe": true, > > ... > > } > > > > if attended[person] { // will be false if person is not in the map > > fmt.Println(person, "was at the meeting") > > } > > > > > > > > On Monday, 27 April 2020 22:16:20 UTC+5:30, adithya...@gmail.com wrote: > > Basically i need a slice with indexed values, so that i can check only > existence. > > or a map with only keys? > > How it can be done? > > > > -- > > 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 golan...@googlegroups.com . > > To view this discussion on the web visit > https://groups.google.com/d/msgid/golang-nuts/1201e6f3-621e-4875-9374-d7713fa7d8aa%40googlegroups.com. > > > > -- 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+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/267feb65-6068-444d-82fb-fa7976bfaace%40googlegroups.com.
Re: [go-nuts] Need a way to check only key exist or not? without value?
I think map[string]struct{} takes no storage for the value and is the most efficient way to do this. - Randy > On Apr 27, 2020, at 7:20 PM, Shishir Verma wrote: > > I think the idiomatic way to implement a set in golang is to use a map with > bool values. Here is an example from effective go documentation: > > > attended := map[string]bool{ > "Ann": true, > "Joe": true, > ... > } > > if attended[person] { // will be false if person is not in the map > fmt.Println(person, "was at the meeting") > } > > > > On Monday, 27 April 2020 22:16:20 UTC+5:30, adithya...@gmail.com wrote: > Basically i need a slice with indexed values, so that i can check only > existence. > or a map with only keys? > How it can be done? > > -- > 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+unsubscr...@googlegroups.com. > To view this discussion on the web visit > https://groups.google.com/d/msgid/golang-nuts/1201e6f3-621e-4875-9374-d7713fa7d8aa%40googlegroups.com. -- 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+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/6B2E6C64-4B49-48B2-95C3-FFA2852916EE%40gmail.com.
Re: [go-nuts] Need a way to check only key exist or not? without value?
On Mon, Apr 27, 2020 at 10:46 AM wrote: > > Basically i need a slice with indexed values, so that i can check only > existence. > or a map with only keys? Use a map with struct{} values: m:=make(map[keyType]struct{}) Then you can add keys by: m[key]=struct{}{} and check existence using: _,exists:=m[key] > How it can be done? > > -- > 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+unsubscr...@googlegroups.com. > To view this discussion on the web visit > https://groups.google.com/d/msgid/golang-nuts/30e31603-3a24-405a-908d-0706c3b1a851%40googlegroups.com. -- 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+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/CAMV2RqqD7RvauM23oL6mnC%2B3Gt4zvxpZ_ds4Rhdt7y41dnspaQ%40mail.gmail.com.
[go-nuts] Need a way to check only key exist or not? without value?
Basically i need a slice with indexed values, so that i can check only existence. or a map with only keys? How it can be done? -- 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+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/30e31603-3a24-405a-908d-0706c3b1a851%40googlegroups.com.