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
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
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
690
Introduction to Rake
drbrain
0
390
Lessons in Mentorship
drbrain
1
260
Open Source Maintenance — Ruby on Ales 2014
drbrain
1
140
Open Source Maintenance — RailsClub Moscow
drbrain
1
180
drbdump
drbrain
2
550
Other Decks in Programming
See All in Programming
過去最大のMCPアップデート! 2026-07-28 RC版の謎に迫る
licux
6
240
IBM Bobを活用したレガシーアプリの最新化
oniak3ibm
PRO
1
190
Datadog × OpenTelemetry 入門と実践のあいだ
kn_to_maxpno
1
150
Vite+ Unified Toolchain for the Web
naokihaba
0
240
Observability in Practice:Grafana 與 Edge Device SRE 的那些事
blueswen
0
160
jQueryをバージョンアップする前に使いたいjQuery Migrate
matsuo_atsushi
0
200
Make SRE Operations Easier with Azure SRE Agent
kkamegawa
0
5.3k
Old Dog, New Tricks: The Java 25 Reinvention - JNation
bazlur_rahman
0
150
並列実装の現場、2ヶ月間実務でAIを使い倒したAIもPCも私も限界が近い
ming_ayami
0
120
Javaの型とAI時代に型が大事な理由 / java types and type in AI era
kishida
2
120
エージェンティックRAGにAWSで入門しよう!
har1101
8
1.4k
コンテキストの使い捨てをやめる — ビジネスルール駆動開発と miko —
ioki
0
190
Featured
See All Featured
Google's AI Overviews - The New Search
badams
0
1k
What Being in a Rock Band Can Teach Us About Real World SEO
427marketing
0
250
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
37
6.5k
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
133
19k
JAMstack: Web Apps at Ludicrous Speed - All Things Open 2022
reverentgeek
1
470
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
360
30k
The Limits of Empathy - UXLibs8
cassininazir
1
350
Exploring the relationship between traditional SERPs and Gen AI search
raygrieselhuber
PRO
2
4k
Jamie Indigo - Trashchat’s Guide to Black Boxes: Technical SEO Tactics for LLMs
techseoconnect
PRO
0
160
HTML-Aware ERB: The Path to Reactive Rendering @ RubyCon 2026, Rimini, Italy
marcoroth
1
180
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
32
3.4k
Bash Introduction
62gerente
615
220k
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)