Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
range over funcの使い道と非同期N+1リゾルバーの夢 / about a ran...
Search
mackee
December 13, 2024
Programming
0
830
range over funcの使い道と非同期N+1リゾルバーの夢 / about a range over func
kamakura.go #7
mackee
December 13, 2024
Tweet
Share
More Decks by mackee
See All by mackee
ワンバイナリWebサービスのススメ
mackee
10
8k
tanukistack ライブコーディング / tanukistack live-coding
mackee
0
120
perl for shell, awk and sed programmers
mackee
2
2.4k
今更GoのWebフレームワークを作ろうとしているワケ / Why am I trying to create a Go web framework now?
mackee
1
800
database/sqlでNullを扱う歴史とsql.Null[T]の登場 / sql.Null[T] history
mackee
0
690
マイクロサービス化を利用した Goへの移行事例
mackee
0
820
PerlでつくるフルスクラッチWebAuthn/パスキー認証 / Demonstration of full-scratch WebAuthn/Passkey Authentication written in Perl
mackee
3
5k
SRE定例やその辺の取り組みをアプリケーションエンジニア目線で語る / "Observe" about SRE Meeting by Application Engineer
mackee
0
2k
TinyGoで使えるORM sqllaの 紹介とTinyGoで使えるようにするための工夫
mackee
0
1.5k
Other Decks in Programming
See All in Programming
WebAssemblyインタプリタを書く ~Component Modelを添えて~
ruccho
1
910
State of CSS 2025
benjaminkott
1
120
技術的負債で信頼性が限界だったWordPress運用をShifterで完全復活させた話
rvirus0817
1
2.1k
オープンセミナー2025@広島「君はどこで動かすか?」アンケート結果
satoshi256kbyte
0
200
Introduction to Git & GitHub
latte72
0
120
私の後悔をAWS DMSで解決した話
hiramax
4
130
decksh - a little language for decks
ajstarks
4
21k
Constant integer division faster than compiler-generated code
herumi
2
690
自作OSでDOOMを動かしてみた
zakki0925224
1
1.4k
AIエージェント開発、DevOps and LLMOps
ymd65536
1
340
コーディングは技術者(エンジニア)の嗜みでして / Learning the System Development Mindset from Rock Lady
mackey0225
2
570
AIレビュアーをスケールさせるには / Scaling AI Reviewers
technuma
2
230
Featured
See All Featured
Optimizing for Happiness
mojombo
379
70k
RailsConf 2023
tenderlove
30
1.2k
GraphQLとの向き合い方2022年版
quramy
49
14k
Into the Great Unknown - MozCon
thekraken
40
2k
Unsuck your backbone
ammeep
671
58k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
33
2.4k
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
29
2.8k
The Art of Programming - Codeland 2020
erikaheidi
55
13k
Learning to Love Humans: Emotional Interface Design
aarron
273
40k
Building a Modern Day E-commerce SEO Strategy
aleyda
43
7.5k
Stop Working from a Prison Cell
hatefulcrawdad
271
21k
What's in a price? How to price your products and services
michaelherold
246
12k
Transcript
range over funcͷ͍ಓͱ ඇಉظN+1Ϧκϧόʔͷເ @mackee_w
Δਓ • X: @mackee_w • GitHub: @mackee • ໘ന๏ਓΧϠοΫ •
όοΫΤϯυ & SRE • ࠷ۙRAGύΠϓϥΠϯͳͲΛࢼ࡞ ͍ͯ͠·͢
range over func͓͞Β͍ + ͍ಓ
Go 1.23 Release NotesΑΓ
ͬ͘͟Γ range over ** Go language specΑΓ
ͬ͘͟Γrange over func • for range … ͷ … ʹೖΕΒΕΔͭͰɺҙͷڍಈΛ͢Δ͕ͭ࡞ΕΔΑ͏
ʹͳΓ·ͨ͠Α • ݴޠઃܭଆͷҙਤ: ෮ॲཧͷҰൠԽ͞Εͨॲཧύλʔϯɺͭ·ΓΠςϨʔ λʔΛݴޠػೳͱͯ͠ΈࠐΜͩ • *database/sql.Rows *bu fi o.ScannerͳͲΠςϨʔλʔΛಠࣗʹ࣮͠ ͍ͯΔ(ͦͯͦ͠ΕͧΕΠϯλʔϑΣΠε͕ҧ͏) • ͜ΕΒΛେ౷Ұ͢Δҙਤ͕͋ΔͷͰͳ͍͔(ཁग़య)
ͬ͘͟Γ͍ํ
iter.Seqࣗ࡞ͯ͠ྑ͍ • ҙͷྻจࣈྻΛϧʔϓ͝ͱʹ ੜ͠ଓ͚Δ܅࡞ΕΔ • ଞͷrange over **ͱҧ͍ɺແݶʹ Λు͖ग़͢ͷ࡞ΕΔ •
👉ͷྻΛՃ͍ͯͯ͠͠Δ
օ͞Μ͕͙͢ʹ͑Δศརؔ slices.Chunk
iter.Seqผʹfor rangeͰΘͳͯ͘ྑ͍ • iter.Seq slices.Collect ͰεϥΠεʹมͰ͖Δ • ແݶʹΛుͭ͘ऴΘΒͳ͍ͷͰҙ •
εϥΠεͷΑ͏ͳσʔλྻΛදݱ͢ΔσʔλΛ͞ΒʹநԽͨ͠ܕͱݴ͑Δ • github.com/BooleanCat/go-functional/v2/it ʹศརϢʔςΟϦςΟ͕ؔ͋Δ • ͍ΘΏΔ github.com/samber/lo ͷ iter.Seq/iter.Seq2൛ • it.Map/it.Map2, it.Filter/it.Filter2 ͳͲ • x/exp/xiter ύοέʔδʹಉछͷ͕ؔೖΔ͜ͱ͕ٞ͞Ε͍ͯΔ
ࠓ·Ͱͷ + ͬͱதͷ͕ҎԼͷεϥΠυ ʹ͋Δ • Go Conference 2024: ΠςϨʔλʹΑͬͯGoͲ͏มΘΔͷ͔ •
https://gocon.jp/2024/sessions/2/
TIPS: օ͞Μ͕͙͢ʹ͑Δศརؔ slices.Chunk • sliceΛׂͯ͠ෳݸͷsliceΛฦ͢iter.Seq[[]E]Λ࡞Δ • Ͳ͏͍͏໘ʹศར͔ʁ => όϧΫͰॲཧ͍͕ͨ͠ɺҰؾʹΓ͗͢ Δͷྑ͘ͳ͍࣌
• SQLͰIN۟Λ༻͍ͨόϧΫऔಘ • Ұʹ͛ΒΕΔID͕nݸʹ੍ݶ͞Ε͍ͯΔόϧΫऔಘAPI
ඇಉظN+1Ϧκϧόʔͷເ
N+1 • ιγϟήͷྫ: users ςʔϒϧʹ user_weapons(උث) ςʔϒϧ͕ has-many Ͱ ͍͍ͭͯΔɻҰཡͰuserͱඋثΛηοτͰग़͍ͨ͠
• φΠʔϒͳ࣮: SELECT * FROM users WHERE … • ্هͰҾͬு͖ͬͯͨ݁ՌΛߦ͝ͱʹSELECT * FROM user_weapons WHERE user_id = ? • usersͷߦ͝ͱʹSQL͕ൃߦ͞Εͯ͠·͏ͨΊɺRDBMS·Ͱͷ௨৴Φʔόʔϔο υɺSQLύʔείετɺͳͲͳͲ͕ੵΈॏͳΓϨεϙϯελΠϜ͕ྼԽ͢Δ
GraphQLN+1͕ຊ࣭తʹى͜Γ͕ͪ • GraphQLͰɺ͋Δϊʔυʹඥͮ͘ࢠڙͷϊʔυΛऔΔΑ͏ͳΫΤϦΛΫϥΠ Ξϯτ͕ಈతʹΈཱͯͯൃߦ͢Δ͜ͱ͕Ͱ͖Δ • ී௨ʹφΠʔϒʹ࣮͢ΔͱN+1͕ൃੜ͢Δʂ • ͦ͜ͰGraphQLքͰdataloaderͱ͍͏ςΫ͕·͍ͬͯΔ • GoͷdataloaderͰɺϊʔυͷղܾΛඇಉظʹ͢Δ
+ छྨ͝ͱͷϊʔυͷղ ܾϦΫΤετΛόοϑΝϦϯάͯ͠όϧΫͰॲཧ͢Δ • dataloaderʹ͍ͬͯΕ΄ͱΜͲͷN+1͍͍ײ͡ʹղܾ͞ΕΔ
ҰํRESTfulͷN+1ղܾํ๏ • ϨεϙϯεͷΈཱͯํϑϨʔϜϫʔΫͰͳ͘ΞϓϦέʔγϣϯ ίʔυͷ • => dataloaderͷΑ͏ʹఆܕԽͰ͖ͳ͍ • ΄ͱΜͲͷέʔεͰεϥΠεΛฦ͢ͱ͖ʹಉظతʹεϥΠεͷதΛ ॱ൪ʹΈཱͯͯߦ͍ͬͯΔͷͰdataloaderͷద༻Ͱ͖ͳ͍
• => εϥΠεͷղܾΛඇಉظʹ͢Εྑ͍ͷͰʁ
github.com/mackee/iterutils/async.Map • go-functional/v2/it.Mapͷඇಉظ൛ • async.Map2͋ΔΑʂ • ͜ΕͰεϥΠεͷࢠͷϊʔυͷղܾΛdataloaderʹͤΔ͜ͱ͕Ͱ ͖ΔͷͰͳ͍͔
͍ํ
sync.WaitGroupͰࣄΓΔͷͰʁ • ҧ͍ͱͯ͠ɺॏͶ͕͚͕Մೳ • async.MapiterΛड͚ೖΕΔ ͭ·Γit.Mapasync.MapΛෳճ ֻ͚߹ΘͤΔ͜ͱ͕Ͱ͖Δ • ॲཧͷ࣮ߦࣗମɺslices.Collect ͞ΕΔ·ͰԆ͞ΕΔ
• ͪͳΈʹॱ൪ೖΕͨiter.Seqͷॱ൪ʹྻͯ͠ฦ͢Α͏ʹͳͬͯ ͍·͢
async.Map2ͷ͍ํ • errgroup.Groupʹ૬ • iter.Seq2[V, error] Λฦ͢ • it.TryCollectiter.Seq2[V, error]Λ৯ͯͲΕ͔ҰͭͰ
error͕͋Εଧͪͬͯ errorΛฦ͢
github.com/vikstrous/dataloadgen • GoͰdataloaderΛߦ͏ͭ • GenericsΛ͍ͬͯΔͷ Ͱੜ͠ͳͯ͘ศར(ର vektah/dataloaden)
࣮ࡍૣ͘ͳΔ͔ݕূͯ͠ΈΔ • ӈਤͷςʔϒϧΛ༻ҙ • fi xtureΛೖΕͨ users: 10000, items: 10000
user_items: 100000 • ϥϯμϜͰ500݅IDࢦఆͨ͠users ΛऔΓɺ͞Βʹ͍࣋ͬͯΔitemΛ ͱΔૢ࡞ • FkΠϯσοΫεష͍ͬͯͳ͍
N+1͕͋Δঢ়ଶͷίʔυ
Dataloaderʹͨ͠ͱ͖ͷಉ༷ͷίʔυ
Dataloaderʹͨ͠ͱ͖ͷಉ༷ͷίʔυ
ϕϯν݁Ռ 100ഒۙ͘ͷ͕͍ࠩͭͨ
ͦͷଞͷߟ • ࣮ISUCONͷաڈͰͬͯείΞ͕ͦΜͳʹ্͕Βͳ͍ • ਪଌ: CPU/ϝϞϦ੍͕ݶ͞Ε͍ͯͨΓ͢Δ͔Βʁ • ͔ͳΓgoroutineΛىಈ͢ΔͨΊʁ • ͔࣮͠͠ࡍͷݱͰgqlgen/dataloader͕ػೳ͍ͯ͠ΔҎ্ɺ͜ͷ
ख๏༗ޮͦ͏ͱݴ͑Δ • (͕͜͜λΠτϧͷʮເʯཁૉͰ͢)
·ͱΊ • GraphQLͰͳ͍໘ͰdataloaderΛ͏߹ʹɺasync.Map/ async.Map2͕༻Ͱ͖Δ͜ͱΛࣔͨ͠ • ͦͯ͋͠ΔఔޮՌతͰ͋Δ͜ͱ͕Θ͔ͬͨ • async.Map/dataloaderΛ͏ͱطଘͷσʔλΈཱͯͷߏ͔Β͋ ·Γม͑ͳͯ͘ྑ͍ͷͰϦϑΝΫλʹศར •
AΛݺͿͷʹBͷmap͕ඞཁΈ͍ͨͳґଘؔݮΒͤΔ
Ҏ্Ͱ͢ʂ • ͦͷଞ͓Βͤ: gitHub.com/mackee/iterutils ʹطଘͷΠςϨʔλͬ Ά͍interfaceΛiter.Seqʹม͢Δ͕ؔ͋ΔͷͰݟͯ͘Εʂ