Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
empty struct
Search
tesso
November 18, 2024
60
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
empty struct
https://gijutsu-club.connpass.com/event/334645/presentation/
#2 学生と社会人LTでの発表資料
tesso
November 18, 2024
More Decks by tesso
See All by tesso
UUID
tesso57
0
3
Multi Agents with Cmux
tesso57
0
3
TC39 proposal: Type as Comments を眺める
tesso57
0
18
shift / reset
tesso57
0
86
Featured
See All Featured
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
31
10k
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
38
3.1k
Fantastic passwords and where to find them - at NoRuKo
philnash
53
4k
Six Lessons from altMBA
skipperchong
29
4.5k
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
133
20k
Into the Great Unknown - MozCon
thekraken
41
2.8k
Leo the Paperboy
mayatellez
10
2.4k
Building a Scalable Design System with Sketch
lauravandoore
464
34k
Building a A Zero-Code AI SEO Workflow
portentint
PRO
0
780
Design in an AI World
tapps
1
350
Making Projects Easy
brettharned
120
6.8k
What’s in a name? Adding method to the madness
productmarketing
PRO
24
4.2k
Transcript
@tesso57 empty struct tesso @学生と社会人LT会 2024/11/17
@tesso57 @tesso57 (てっそ) - 修士1年 - 研究 : 暗号理論 -
ナンバーナイン 開発アルバイト - go, connect-go - FPSゲーマー - valorant - deadlock - 漫画好き
@tesso57 今日のお話 func unique(list []string) []string { uniqueMap := make(map[string]bool)
for _, entry := range list { if _, value := uniqueMap[entry]; !value { uniqueMap[entry] = true } } return slices.Collect(maps.Keys(uniqueMap)) }
@tesso57 今日のお話 func unique(list []string) []string { uniqueMap := make(map[string]bool)
for _, entry := range list { if _, value := uniqueMap[entry]; !value { uniqueMap[entry] = true } } return slices.Collect(maps.Keys(uniqueMap)) } func unique(list []string) []string { uniqueMap := make(map[string]struct{}) for _, entry := range list { if _, value := uniqueMap[entry]; !value { uniqueMap[entry] = struct{}{} } } return slices.Collect(maps.Keys(uniqueMap)) }
@tesso57 struct{}について • 構造体 • フィールドを持たない構造体 • メモリを消費しない(0 byte) •
同じアドレスを参照する x := 0 y := struct{}{} fmt.Println(unsafe.Sizeof(x)) // 8 fmt.Println(unsafe.Sizeof(y)) // 0 var a, b struct{} fmt.Printf("%p\n", &a) // 0x1046f07a0 fmt.Printf("%p\n", &b) // 0x1046f07a0
@tesso57 struct{}のユースケース • bool っぽく使うことが多い ◦ uniqueをとるとき ◦ 存在確認 ◦
通知
@tesso57 struct{}のユースケース : unique // 配列のuniqueを取る関数 func unique(list []string) []string
{ uniqueMap := make(map[string]struct{}) // マップを作成 for _, entry := range list { if _, value := uniqueMap[entry]; !value { // 値が存在しない場合,マップに追加 uniqueMap[entry] = struct{}{} } } return slices.Collect(maps.Keys(uniqueMap)) // スライスに変換 }
@tesso57 struct{}のユースケース : 通知 done := make(chan struct{}) // チャンネルの作成
go func() { // 並列処理 time.Sleep(time.Second) // 1秒待つ done <- struct{}{} // チャンネルに空の構造体を送信 }() <-done // 受信を待つ fmt.Println("done")
@tesso57 struct{}のユースケース : 存在確認 type encodeState struct { bytes.Buffer ptrLevel
uint ptrSeen map[any]struct{} } func (me mapEncoder) encode(e *encodeState, v reflect.Value, opts encOpts) { if e.ptrLevel++; e.ptrLevel > startDetectingCyclesAfter { ptr := v.UnsafePointer() if _, ok := e.ptrSeen[ptr]; ok { e.error(&UnsupportedValueError{ v, fmt.Sprintf("encountered a cycle via %s", v.Type())}) } e.ptrSeen[ptr] = struct{}{} defer delete(e.ptrSeen, ptr) } https://cs.opensource.google/go/go/+/master:src/encoding/json/encode.go;l=274;bpv=0;bpt=1
@tesso57 struct{}の仕組み var zerobase uintptr … //go:linkname mallocgc func mallocgc(size
uintptr, typ *_type, needzero bool) unsafe.Pointer { … if size == 0 { return unsafe.Pointer(&zerobase) } https://go.dev/src/runtime/malloc.go
@tesso57 struct{}の仕組み • サイズ0のとき, 特別な値(zerobase)のポインタを返し続ける • それ以外のときはメモリをとる
@tesso57 まとめ • struct{} は 0bytesで便利 • 特別なポインタを返し続けることで実現 • struct{}
使ってみよう!
@tesso57 まとめ • struct{} は 0bytesで便利 • 特別なポインタを返し続けることで実現 • struct{}
使ってみよう! • 他の言語にもあるのかな?