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
480
testingを眺める
matumoto
1
220
sync/v2 プロポーザルの 背景と sync.Pool について
matumoto
0
820
Goトランザクション処理
matumoto
1
94
Go1.22のリリース予定の機能を見る
matumoto
0
98
GoのUnderlying typeについて
matumoto
0
250
Typed-nilについて
matumoto
0
390
GoのType Setsという概念
matumoto
0
64
GoのRateLimit処理の実装
matumoto
0
550
Other Decks in Technology
See All in Technology
予測不能な実行エージェントを安全にサクッと試せるサンドボックス環境の話
sansantech
PRO
0
120
AIは爆速なのに、私が詰まっていた話 ― 音声入力と鳴くマスコットでボトルネックを削る
yama3133
0
520
ミイダス株式会社 テックチームのご紹介 / MIIDAS Tech Team
miidas
0
160
20260930_Gemma4_Hands-on
tsho
0
230
[2026 Oracle Technical Deep Dive] AI時代のアプリケーションを支えるCloud NativeとJava EEモダナイゼーション (2026年9月17日開催)
oracle4engineer
PRO
0
120
生成AIと進める探索的データ分析(2026.10.03 第122回Tokyo.R勉強会)
tatamiya
2
1.4k
OSC2026on_the-world-is-waiting-for-your-voice.pdf
naruoga
0
350
私の推しは「聞いてから進む」AIです -AI-DLCに一人でアプリを作らせた話
yama3133
1
170
AIに攻撃される前に、AIに攻撃させる
tsuchikazu
0
150
Futexes the good, the bad, the ugly
ennael
PRO
0
120
[AWS 秋のクラウドオペレーション祭り 2026]AWS DevOps Agentで変わるリリースと運用対応 ~リリース管理機能と Directed actions のご紹介~
furuton
2
870
並行性の問題を防げ!実践トランザクション入門
occhi
0
260
Featured
See All Featured
Design of three-dimensional binary manipulators for pick-and-place task avoiding obstacles (IECON2024)
konakalab
0
610
Test your architecture with Archunit
thirion
2
2.4k
Jess Joyce - The Pitfalls of Following Frameworks
techseoconnect
PRO
1
430
The Impact of AI in SEO - AI Overviews June 2024 Edition
aleyda
6
1.2k
技術選定の審美眼(2025年版) / Understanding the Spiral of Technologies 2025 edition
twada
PRO
120
120k
Hiding What from Whom? A Critical Review of the History of Programming languages for Music
tomoyanonymous
3
1.3k
What does AI have to do with Human Rights?
axbom
PRO
1
2.4k
SERP Conf. Vienna - Web Accessibility: Optimizing for Inclusivity and SEO
sarafernandez
2
1.6k
Ruling the World: When Life Gets Gamed
codingconduct
0
380
How To Speak Unicorn (iThemes Webinar)
marktimemedia
1
590
Measuring & Analyzing Core Web Vitals
bluesmoon
9
1k
Groundhog Day: Seeking Process in Gaming for Health
codingconduct
0
390
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
ご清聴ありがとうございました!