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
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
650
Introduction to Rake
drbrain
0
330
Lessons in Mentorship
drbrain
1
210
Open Source Maintenance — Ruby on Ales 2014
drbrain
1
120
Open Source Maintenance — RailsClub Moscow
drbrain
1
160
drbdump
drbrain
2
490
Other Decks in Programming
See All in Programming
三者三様 宣言的UI
kkagurazaka
0
290
登壇は dynamic! な営みである / speech is dynamic
da1chi
0
390
Vueのバリデーション、結局どれを選べばいい? ― 自作バリデーションの限界と、脱却までの道のり ― / Which Vue Validation Library Should We Really Use? The Limits of Self-Made Validation and How I Finally Moved On
neginasu
2
1.7k
社会人になっても趣味開発を続けたい! / traPavilion
mazrean
1
110
Pythonに漸進的に型をつける
nealle
1
140
3年ぶりにコードを書いた元CTOが Claude Codeと30分でMVPを作った話
maikokojima
0
700
CSC305 Lecture 12
javiergs
PRO
0
240
Reactive Thinking with Signals and the Resource API
manfredsteyer
PRO
0
120
SODA - FACT BOOK(JP)
sodainc
1
8.9k
Introducing RemoteCompose: break your UI out of the app sandbox.
camaelon
2
120
マイベストのシンプルなデータ基盤の話 - Googleスイートとのつき合い方 / mybest-simple-data-architecture-google-nized
snhryt
0
100
理論と実務のギャップを超える
eycjur
0
200
Featured
See All Featured
Designing for humans not robots
tammielis
254
26k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
46
2.5k
Mobile First: as difficult as doing things right
swwweet
225
10k
Fantastic passwords and where to find them - at NoRuKo
philnash
52
3.5k
We Have a Design System, Now What?
morganepeng
53
7.8k
Building a Scalable Design System with Sketch
lauravandoore
463
33k
The MySQL Ecosystem @ GitHub 2015
samlambert
251
13k
Building Adaptive Systems
keathley
44
2.8k
Improving Core Web Vitals using Speculation Rules API
sergeychernyshev
21
1.2k
Documentation Writing (for coders)
carmenintech
75
5.1k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
508
140k
Side Projects
sachag
455
43k
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)