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
Eric Hodel
April 27, 2016
Programming
130
0
Share
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
680
Introduction to Rake
drbrain
0
370
Lessons in Mentorship
drbrain
1
240
Open Source Maintenance — Ruby on Ales 2014
drbrain
1
140
Open Source Maintenance — RailsClub Moscow
drbrain
1
170
drbdump
drbrain
2
530
Other Decks in Programming
See All in Programming
Xdebug と IDE による デバッグ実行の仕組みを見る / Exploring-How-Debugging-Works-with-Xdebug-and-an-IDE
shin1x1
0
370
ハンズオンで学ぶクラウドネイティブ
tatsukiminami
0
120
Codex CLIのSubagentsによる並列API実装 / Parallel API Implementation with Codex CLI Subagents
takatty
2
890
Codex CLI でつくる、Issue から merge までの開発フロー
amata1219
0
350
의존성 주입과 모듈화
fornewid
0
130
瑠璃の宝石に学ぶ技術の声の聴き方 / 【劇場版】アニメから得た学びを発表会2026 #エンジニアニメ
mazrean
0
240
Rethinking API Platform Filters
vinceamstoutz
0
11k
The Monolith Strikes Back: Why AI Agents ❤️ Rails Monoliths
serradura
0
310
Go_College_最終発表資料__外部公開用_.pdf
xe_pc23
0
200
ファインチューニングせずメインコンペを解く方法
pokutuna
0
300
PHP で mp3 プレイヤーを実装しよう
m3m0r7
PRO
0
260
3分でわかるatama plusのQA/about atama plus QA
atamaplus
0
150
Featured
See All Featured
How to audit for AI Accessibility on your Front & Back End
davetheseo
0
240
Jamie Indigo - Trashchat’s Guide to Black Boxes: Technical SEO Tactics for LLMs
techseoconnect
PRO
0
110
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
194
17k
Bridging the Design Gap: How Collaborative Modelling removes blockers to flow between stakeholders and teams @FastFlow conf
baasie
0
510
Ten Tips & Tricks for a 🌱 transition
stuffmc
0
98
Fashionably flexible responsive web design (full day workshop)
malarkey
408
66k
DBのスキルで生き残る技術 - AI時代におけるテーブル設計の勘所
soudai
PRO
64
53k
Visualization
eitanlees
150
17k
Digital Ethics as a Driver of Design Innovation
axbom
PRO
1
260
Put a Button on it: Removing Barriers to Going Fast.
kastner
60
4.2k
Statistics for Hackers
jakevdp
799
230k
How GitHub (no longer) Works
holman
316
150k
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)