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
Sponsored
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
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
520
testingを眺める
matumoto
1
230
sync/v2 プロポーザルの 背景と sync.Pool について
matumoto
0
840
Goトランザクション処理
matumoto
1
97
Go1.22のリリース予定の機能を見る
matumoto
0
100
GoのUnderlying typeについて
matumoto
0
250
Typed-nilについて
matumoto
0
400
GoのType Setsという概念
matumoto
0
66
GoのRateLimit処理の実装
matumoto
0
550
Other Decks in Technology
See All in Technology
外国人エンジニアがVueプロトタイプで仕様を合わせている話 / Vue Fes Japan 2026 LT
eluwing
0
170
「作れる」と「リリースできる」の間にあったもの── PdMを外して見えた、PdMがやるべき仕事/Buildable ≠ Shippable: What Removing the PM Taught Me About the PM’s Job
yappli_developers
0
280
How we choose session proposals
kawaguti
PRO
0
170
Vue.jsのGitHubから学ぶ意思決定
yut0naga1_fa
1
280
プロダクトが成長すると、 プロダクトマネージャーの仕事はどう変わるのか - 「まず作る」から「小さく学び、確かめながら進む」へ
sansantech
PRO
1
370
Claude Code on Bedrock で統制をかける小ネタ集/claude-code-bedrock-governance-tips
emiki
1
13k
【データ横丁主催】AI エージェント時代のTranslytical Data Platform
shisyu_gaku
0
300
「Learning Outcome」を書きたくない私が、Learning Outcomeと折り合いをつけるまで
mhlyc
0
240
導入から定着へ ~コーディングエージェントを日常業務に組み込む実践知~
yukisakai1225
0
240
What Is JetBrains Air
x5gtrn
PRO
0
170
ビジネス部門からPOを迎えての半年間の進歩と課題
zozotech
PRO
0
150
自宅サーバと脆弱性
masakiokuda
1
120
Featured
See All Featured
Money Talks: Using Revenue to Get Sh*t Done
nikkihalliwell
0
540
How to audit for AI Accessibility on your Front & Back End
davetheseo
0
610
Six Lessons from altMBA
skipperchong
29
4.5k
How to Think Like a Performance Engineer
csswizardry
28
2.9k
The SEO identity crisis: Don't let AI make you average
varn
0
610
Highjacked: Video Game Concept Design
rkendrick25
PRO
1
490
The browser strikes back
jonoalderson
0
1.9k
Dominate Local Search Results - an insider guide to GBP, reviews, and Local SEO
greggifford
PRO
0
380
A Soul's Torment
seathinner
8
3.8k
AI Search: Where Are We & What Can We Do About It?
aleyda
0
8.1k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
49
3.6k
Git: the NoSQL Database
bkeepers
PRO
433
67k
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
ご清聴ありがとうございました!