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
いまいちどスライスの 挙動を見直してみる
Search
matumoto
July 23, 2024
Technology
430
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
いまいちどスライスの 挙動を見直してみる
DMM.go#8で登壇した際の発表資料です
connpass
-
https://dmm.connpass.com/event/322113/
matumoto
July 23, 2024
More Decks by matumoto
See All by matumoto
Go標準パッケージのI/O処理をながめる
matumoto
0
470
testingを眺める
matumoto
1
220
sync/v2 プロポーザルの 背景と sync.Pool について
matumoto
0
820
Goトランザクション処理
matumoto
1
93
Go1.22のリリース予定の機能を見る
matumoto
0
93
GoのUnderlying typeについて
matumoto
0
250
Typed-nilについて
matumoto
0
390
GoのType Setsという概念
matumoto
0
62
GoのRateLimit処理の実装
matumoto
0
510
Other Decks in Technology
See All in Technology
今話題のAI「Jev」って何? 宇宙最速で学ぶ会
minorun365
PRO
21
13k
10分で知る最近のOmarchy
komagata
0
320
Omarchy Quattro の日本語設定周り
simosako
2
180
Screen Lens - 今見てる画面を翻訳する
komagata
0
300
株式会社シーエーシー エンジニア向け会社紹介資料
cac
0
57k
Issue 駆動でスペシャリストの意図を届ける、AI 実装のアクセシビリティ向上
thkt
0
110
【技術的負債conf】事業成長に伴う技術的負債の説明責任とAIによるモニタリング、認知的負債について
i35_267
2
1.6k
データ界隈LT祭 第1回LT登壇
taromatsui_cccmkhd
1
1.3k
The Agent Builder Loop from Daily Work to OSS
minorun365
PRO
3
200
すぐできる衛星通信対応 あとは山奥に行くだけ
tatetate55
0
130
GoにおけるFFIのこれまでとこれから
goccy
5
3.4k
アクセスキーこわい やめかたと漏らさない工夫
sassssan68
1
240
Featured
See All Featured
jQuery: Nuts, Bolts and Bling
dougneiner
66
8.6k
Fashionably flexible responsive web design (full day workshop)
malarkey
409
67k
世界の人気アプリ100個を分析して見えたペイウォール設計の心得
akihiro_kokubo
PRO
74
42k
Kristin Tynski - Automating Marketing Tasks With AI
techseoconnect
PRO
0
520
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
47
8.3k
Winning Ecommerce Organic Search in an AI Era - #searchnstuff2025
aleyda
2
2.1k
A brief & incomplete history of UX Design for the World Wide Web: 1989–2019
jct
2
510
Heart Work Chapter 1 - Part 1
lfama
PRO
10
36k
Connecting the Dots Between Site Speed, User Experience & Your Business [WebExpo 2025]
tammyeverts
11
1k
First, design no harm
axbom
PRO
2
1.3k
The Pragmatic Product Professional
lauravandoore
37
7.4k
The Hidden Cost of Media on the Web [PixelPalooza 2025]
tammyeverts
2
500
Transcript
© DMM CONFIDENTIAL いまいちどスライスの 挙動を見直してみる DMM.go #8 24新卒 松本響輝(matumoto) 2024-07-23
発表の目的 • スライスがどう作られているのか把握する • 気をつけたい罠とその対策を理解する 2 スライスに関して改めて 詳しくなってもらいたい!
目次 • 基本操作のおさらい • スライスの実体 • 気をつけること • Tips 3
基本操作のおさらい 4
スライスの宣言 • []T で型 T のスライスになる 5
スライスのリテラル • []T{x1, x2, … , xn} で型 T の要素をn個もつスライスになる
6
スライス式 • a[low:high] でスライスを構築できる 7
スライスの実体 8
スライスがどう実装されているか • 配列へのポインタ、長さ、容量 を持った構造体 9 引用:https://github.com/golang/go/blob/master/src/runtime/slice.go
配列を参照している 10 3 1 4 1 5 x := []int{3,1,4,1,5}
y := x[1:3] ptr len:5 cap:5 ptr len:2 cap:4 [5]int []int []int
ポインタの値をコピー • y := x はポインタの値がコピーされる 11 3 1 4
1 5 x := []int{3,1,4,1,5} y := x ptr len:5 cap:5 ptr len:5 cap:5 [5]int []int []int
容量 (capacity) はどう増えるか • append関数で要素を追加するとき、長さや容量が増えていく 12 x := []int{3,1,4,1,5} ptr
len:5 cap:5 x = append(x, 10, 11) len:7 cap:10 ptr
容量 (capacity) はどう増えるか • 元が256より小さい場合: 2倍ずつ増える • 元が256以上の場合: 1.25倍+192ずつ増える •
192足すのは256付近でもスムーズに増やすため 13 例. capacityを256→350 にしたい +192なし:2回 • 256 * 1.25 = 320 • 320 * 1.25 = 400 +192あり:1回 • 256 * 1.25 + 192 = 512 詳細:https://github.com/golang/go/blob/master/src/runtime/slice.go#L289
気をつけること 14
今回は2つ紹介 15 要素の変更 a[0] = 10 メモリ管理 return a[:3]
1.要素の変更 16
「1.要素の変更」を正しく使っている例 17 引用元:https://github.com/golang/go/blob/master/src/slices/zsortordered.go slices 標準パッケージでのソート処理
「1.要素の変更」の使いどころ 18 • 副作用のある関数で使う • パフォーマンス的に都合が良い場合 • sort, swap •
※ただし、一般的なWebアプリケーション開発ではDB処理などの方が重くなりが ちなので、変数のコピーコストなどは無視することが多いです
「1.要素の変更」の対策 19 • linterで検知する • go-reassignでトップレベル変数への再代入の検知(golangci-lintあり) • goboundcheckで境界チェック • なお、関数が持つ仮引数への再代入検知を行う、というようなlinterは見つけられ
ませんでした(作ることはできそうです) • レビューで気づく • a[i] = 0 みたいな文があったら注意です
2.余分なメモリ参照 • スライスが参照している限り、配列を保持し続ける 20 0 0 0 0 0 x
:= make([]int, 0, 1000) x = x[:3:3] ptr len:3 cap:3 [1000]int []int … 1000要素分メモリが確保されたまま
「2.余分なメモリ参照」のわるい例 • 読み取ったファイル全体は保持されたまま 一部改変して引用:https://go.dev/blog/slices-intro 21
「2.余分なメモリ参照」を改善した例 • 必要な分だけメモリを再確保している 一部改変して引用:https://go.dev/blog/slices-intro 22
「2.余分なメモリ参照」の対策 • 静的解析での検知は難しい • 実行時の挙動であるため厳密に検知できない • レビューで気づく • スライスを受け取って部分スライスを返している関数などに注意 •
regexpパッケージの結果をそのまま返している部分など • 例. return digitRegexp.Find(a) 23
Tips スライスに関するTips 24
sync.Poolによる使い回し • リクエストごとにスライスを作る...というのですら高速化したい • sync.Poolを用いてメモリ領域を使い回すのがよくある手法 • これをラップしたbytebufferpoolというライブラリがある • fasthttpなどで使用されている 25
fasthttpでの例 • fasthttpではレスポンスボディを使い回している 26 引用:https://github.com/valyala/fasthttp/blob/master/http.go
まとめ 27
まとめ • スライスは配列を参照するポインタを持っている • 要素の変更や余分なメモリ参照は気をつけよう • sync.Poolでメモリを使いまわせる 28
ご清聴ありがとうございました!