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
0
120
Lazy Enumeration
An introduction to lazy enumeration in ruby
Eric Hodel
April 27, 2016
Tweet
Share
More Decks by Eric Hodel
See All by Eric Hodel
Building maintainable command-line tools with MRuby
drbrain
0
660
Introduction to Rake
drbrain
0
350
Lessons in Mentorship
drbrain
1
230
Open Source Maintenance — Ruby on Ales 2014
drbrain
1
130
Open Source Maintenance — RailsClub Moscow
drbrain
1
170
drbdump
drbrain
2
510
Other Decks in Programming
See All in Programming
Oxlint JS plugins
kazupon
1
810
例外処理とどう使い分ける?Result型を使ったエラー設計 #burikaigi
kajitack
16
6k
AI によるインシデント初動調査の自動化を行う AI インシデントコマンダーを作った話
azukiazusa1
1
700
Rust 製のコードエディタ “Zed” を使ってみた
nearme_tech
PRO
0
150
副作用をどこに置くか問題:オブジェクト指向で整理する設計判断ツリー
koxya
1
600
dchart: charts from deck markup
ajstarks
3
990
CSC307 Lecture 03
javiergs
PRO
1
490
AIによるイベントストーミング図からのコード生成 / AI-powered code generation from Event Storming diagrams
nrslib
2
1.8k
Package Management Learnings from Homebrew
mikemcquaid
0
210
Fluid Templating in TYPO3 14
s2b
0
130
Patterns of Patterns
denyspoltorak
0
1.4k
CSC307 Lecture 07
javiergs
PRO
0
550
Featured
See All Featured
Noah Learner - AI + Me: how we built a GSC Bulk Export data pipeline
techseoconnect
PRO
0
100
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
141
34k
Mobile First: as difficult as doing things right
swwweet
225
10k
Tell your own story through comics
letsgokoyo
1
810
The Mindset for Success: Future Career Progression
greggifford
PRO
0
230
Building AI with AI
inesmontani
PRO
1
680
What Being in a Rock Band Can Teach Us About Real World SEO
427marketing
0
170
The untapped power of vector embeddings
frankvandijk
1
1.6k
B2B Lead Gen: Tactics, Traps & Triumph
marketingsoph
0
53
30 Presentation Tips
portentint
PRO
1
210
Mind Mapping
helmedeiros
PRO
0
78
Typedesign – Prime Four
hannesfritz
42
2.9k
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)