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
Lazy Enumeration
Search
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
Eric Hodel
April 27, 2016
Programming
140
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Lazy Enumeration
An introduction to lazy enumeration in ruby
Eric Hodel
April 27, 2016
More Decks by Eric Hodel
See All by Eric Hodel
Building maintainable command-line tools with MRuby
drbrain
0
700
Introduction to Rake
drbrain
0
400
Lessons in Mentorship
drbrain
1
260
Open Source Maintenance — Ruby on Ales 2014
drbrain
1
150
Open Source Maintenance — RailsClub Moscow
drbrain
1
180
drbdump
drbrain
2
560
Other Decks in Programming
See All in Programming
yield再入門 #phpcon
o0h
PRO
0
930
仕様駆動開発の消費期限
watany
7
2k
人間の目はかわらない、だからJPEGは30年もつ
yuzneri
12
18k
Google Apps Script で Ruby を動かす
kawahara
0
130
メールのエイリアス機能を履き違えない
isshinfunada
0
220
「寝てても仕事が進む」Claude Codeで組む第二の脳
tomoyafujita2016
0
280
複数の Claude Code が"放置"されてしまう問題をCLI ダッシュボードを自作して解決した話
sumihiro3
1
650
ルールを書いて終わらせないハーネスエンジニアリング
yug1224
4
1.8k
「人を評価する AI」の設計と実装
ryoyanara
0
150
React本体のコードリーディング
high_g_engineer
1
130
<title><a id="</title>君はこのHTMLをパースできるか"></a></title> #雑LT_study
pizzacat83
0
130
Lean は証明の正しさを確認するためだけのツールって思ってませんか?
inoueasei
1
140
Featured
See All Featured
Introduction to Domain-Driven Design and Collaborative software design
baasie
1
920
Utilizing Notion as your number one productivity tool
mfonobong
4
500
Unsuck your backbone
ammeep
672
58k
10 Git Anti Patterns You Should be Aware of
lemiorhan
PRO
659
62k
WCS-LA-2024
lcolladotor
0
790
Why Your Marketing Sucks and What You Can Do About It - Sophie Logan
marketingsoph
0
350
Raft: Consensus for Rubyists
vanstee
141
7.6k
Kristin Tynski - Automating Marketing Tasks With AI
techseoconnect
PRO
0
440
The AI Search Optimization Roadmap by Aleyda Solis
aleyda
1
6k
RailsConf 2023
tenderlove
30
1.5k
Optimizing for Happiness
mojombo
378
71k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
508
140k
Transcript
Lazy Enumera,on Eric Hodel —
[email protected]
Loop values = [1, 2, 3, 4] doubles = []
index = 0 while index < values.length do doubles << values[index] * 2 index += 1 end
Enumera,ng Where am I? values[index] Am I done? index <
values.length What’s next? index += 1
Enumerator API Where am I? next #=> nil or exception
if done Am I done? nil, StopIteration What’s next? handled for you
External Enumerator result = db_conn.exec ‘SELECT * FROM orders’ while
order = result.next do # … end
Internal Enumerator result = db_conn.exec ‘SELECT * FROM orders’ result.each_row
do |order| # … end
External vs Internal You write loop Impera,ve C, Ruby Loop
built-in Func,onal Scheme, Ruby
Eager Enumera,on orders = db_conn.exec ‘SELECT total FROM orders’ total_order_value
= orders.map { |order| # 10,000 values order[‘total’] }.reduce { |sum, order_total| sum + order_total }
Eager Enumera,on orders = db_conn.exec ‘SELECT total FROM orders’ total_order_value
= orders.map { |order| # 100,000,000 values order[‘total’] }.reduce { |sum, order_total| sum + order_total }
100,000,000 Objects >> a = Array.new 100_000_000 >> ObjectSpace.memsize_of a
=> 800000040 800MB 400ms
Lazy Enumera,on orders = db_conn.exec ‘SELECT total FROM orders’ total_order_value
= orders.lazy.map { |order| order[‘total’] }.reduce { |sum, order_total| sum + order_total } 10MB similar ,me
Eager Processing 100M 100M .map .map 100M .map 100M
Lazy Processing 1 1 .map .map 1 .map 1 2
2 2 2 3 3 3 3 … … … … 100M 100M 100M 100M
How does lazy work? Fibers!
Fiber? •Story line for a program •One Fiber runs at
a ,me •Scheduled by author •“Corou,ne”
Hierarchy Process ↳Thread ↳Fiber OS scheduled Manually scheduled
Scheduling Fibers resume(input) #=> output Run a specific Fiber Fiber.yield(value)
Return output to #resume
Ac,ve Fiber 1 1 .map .map 1 .map 1 2
2 2 2 3 3 3 3 … … … … 100M 100M 100M 100M Fiber Fiber Fiber Fiber
Example of Fiber ⃠
Lazy Enumera,on •Reduces memory used •Great for huge data sets
•Processes one at a ,me •Uses Fiber (corou,ne)