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
Lazy Enumeration
Search
Eric Hodel
April 27, 2016
Programming
150
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
710
Introduction to Rake
drbrain
0
410
Lessons in Mentorship
drbrain
1
270
Open Source Maintenance — Ruby on Ales 2014
drbrain
1
150
Open Source Maintenance — RailsClub Moscow
drbrain
1
190
drbdump
drbrain
2
570
Other Decks in Programming
See All in Programming
mrbgem 三角測量 開発
ogom
0
200
Starting & Sustaining Code-Based E2E Testing for Non-Coding QA Teams( #jasstniigata )
teyamagu
PRO
1
910
Simple Storage Service(S3) is not simple
iwatsukayura
0
210
Ghostty + Neovimで作る 透明でカッコ良い開発環境
j341nono
0
150
setup-vp GitLab対応の裏側
naokihaba
0
150
[ハンズオン]AIへの指示だけで「五目並べ」を作ってみよう
satoshi256kbyte
1
350
技術的負債を組織課題として解く-増えすぎたマイクロサービスとの戦い-
reimaru
1
2.8k
[Rails World 2026] Durable orchestration on Rails: from continuation to workflow
palkan
1
480
ハーネス設計入門 〜 基礎知識の整理から実務へのステップアップ 〜
kinopeee
20
21k
プロダクトコードからライブラリの境界を見つける
elmetal
PRO
0
100
GemmaをJevのように使ってみる / Use Gemma like Jev
kishida
5
700
When benchmarks go bad - what I learned from measuring performance wrong
hollycummins
0
150
Featured
See All Featured
[Rails World 2026] Durable orchestration on Rails: from continuation to workflow
palkan
1
480
How STYLIGHT went responsive
nonsquared
100
6.3k
Optimizing for Happiness
mojombo
378
71k
How to Get Subject Matter Experts Bought In and Actively Contributing to SEO & PR Initiatives.
livdayseo
0
210
How to build a perfect <img>
jonoalderson
1
6.1k
Designing Powerful Visuals for Engaging Learning
tmiket
1
590
The AI Revolution Will Not Be Monopolized: How open-source beats economies of scale, even for LLMs
inesmontani
PRO
3
3.8k
B2B Lead Gen: Tactics, Traps & Triumph
marketingsoph
0
260
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
31
3.4k
Primal Persuasion: How to Engage the Brain for Learning That Lasts
tmiket
0
490
Taking LLMs out of the black box: A practical guide to human-in-the-loop distillation
inesmontani
PRO
3
2.4k
Leo the Paperboy
mayatellez
10
2.3k
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)