Piotr Szotkowski about "Bits of ruby"

23
Bits of Ruby

Transcript of Piotr Szotkowski about "Bits of ruby"

Page 1: Piotr Szotkowski about "Bits of ruby"

Bits of Ruby

Page 3: Piotr Szotkowski about "Bits of ruby"

Ἲ�tip to RGSoC­friendly confs♥ eurucamp ♥ JRubyConf ♥ Deccan RubyConf ♥

♥ OTSConf ♥ Madison+ Ruby ♥ BaRuCo ♥

♥ Reject.JS ♥ Strange Loop ♥ RailsClub ♥

♥ EuRuKo ♥ Øredev ♥ ROSSConf ♥

♥ GOTO Conference ♥ dotJS ♥

Page 4: Piotr Szotkowski about "Bits of ruby"

sooo… who goes where?Tate Isom Genevieve Ola Arielle

eurucamp ✓ ✓ ✓ ✓

JRubyConf ✓

ROSSConf ✓ ✓

BaRuCo ✓ ✓ ✓

EuRuKo ✓ ✓ ✓

…but how do we track who goes where?

name­keyed hash? conf­keyed hash?

how about a logical matrix?

Page 5: Piotr Szotkowski about "Bits of ruby"

oh of course! (what’s that…?)a rectangular table of Boolean values

true / false relationship representationc o n f s = % w ( e u r u c a m p J R u b y C o n f R O S S C o n f B a R u C o E u R u K o ) n a m e s = % w ( T a t e I s o m G e n e v i e v e O l a A r i e l l e ) m a t r i x = [ [ t r u e , t r u e , t r u e , f a l s e , t r u e ] , [ t r u e , f a l s e , f a l s e , f a l s e , f a l s e ] , [ f a l s e , f a l s e , f a l s e , t r u e , t r u e ] , [ f a l s e , t r u e , t r u e , f a l s e , t r u e ] , [ t r u e , f a l s e , t r u e , f a l s e , t r u e ] , ] m a t r i x [ c o n f s . i n d e x ( ' e u r u c a m p ' ) ] [ n a m e s . i n d e x ( ' T a t e ' ) ] # = > t r u e m a t r i x [ c o n f s . i n d e x ( ' R O S S C o n f ' ) ] [ n a m e s . i n d e x ( ' G e n e v i e v e ' ) ] # = > f a l s e

Page 6: Piotr Szotkowski about "Bits of ruby"

but that’s not very efficientevery conference adds a row

every participant adds a column

confs.size × (names.size Bools + 1 Array) + 1 Array

how about using zeroes and ones instead?

Page 7: Piotr Szotkowski about "Bits of ruby"

logical matrixm a t r i x = [ [ t r u e , t r u e , t r u e , f a l s e , t r u e ] , [ t r u e , f a l s e , f a l s e , f a l s e , f a l s e ] , [ f a l s e , f a l s e , f a l s e , t r u e , t r u e ] , [ f a l s e , t r u e , t r u e , f a l s e , t r u e ] , [ t r u e , f a l s e , t r u e , f a l s e , t r u e ] , ]

Page 8: Piotr Szotkowski about "Bits of ruby"

bit matrixm a t r i x = [ [ 1 , 1 , 1 , 0 , 1 ] , [ 1 , 0 , 0 , 0 , 0 ] , [ 0 , 0 , 0 , 1 , 1 ] , [ 0 , 1 , 1 , 0 , 1 ] , [ 1 , 0 , 1 , 0 , 1 ] , ]

Page 9: Piotr Szotkowski about "Bits of ruby"

bit matrixm a t r i x = [ 0 b 1 1 1 0 1 , 0 b 1 0 0 0 0 , 0 b 0 0 0 1 1 , 0 b 0 1 1 0 1 , 0 b 1 0 1 0 1 , ]

m a t r i x = [ 2 9 , 1 6 , 3 , 1 3 , 2 1 ]

Page 10: Piotr Szotkowski about "Bits of ruby"

how much better?names.size Fixnums + 1 Array   →   1 Fixnum

Fixnums go up to 2E62-1

…that’s 4611686018427387903

…that’s 4_611_686_018_427_387_903

I’m better at estimating thingsthan probably 40 or 50 billion people.

— Randy Tayler

…that’s over four quintillion

Page 11: Piotr Szotkowski about "Bits of ruby"

but how do we query it?c o n f s = % w ( e u r u c a m p J R u b y C o n f R O S S C o n f B a R u C o E u R u K o ) n a m e s = % w ( T a t e I s o m G e n e v i e v e O l a A r i e l l e ) m a t r i x = [ [ t r u e , t r u e , t r u e , f a l s e , t r u e ] , [ t r u e , f a l s e , f a l s e , f a l s e , f a l s e ] , [ f a l s e , f a l s e , f a l s e , t r u e , t r u e ] , [ f a l s e , t r u e , t r u e , f a l s e , t r u e ] , [ t r u e , f a l s e , t r u e , f a l s e , t r u e ] , ] m a t r i x [ c o n f s . i n d e x ( ' e u r u c a m p ' ) ] [ n a m e s . i n d e x ( ' T a t e ' ) ] # = > t r u e m a t r i x [ c o n f s . i n d e x ( ' R O S S C o n f ' ) ] [ n a m e s . i n d e x ( ' G e n e v i e v e ' ) ] # = > f a l s e

Page 12: Piotr Szotkowski about "Bits of ruby"

Integer#[]2 9 = = 0 b 1 1 1 0 1 # = > t r u e 2 9 [ 0 ] # = > 1 2 9 [ 1 ] # = > 0 2 9 [ 2 ] # = > 1

return the n­th least significant bitc o n f s = % w ( e u r u c a m p J R u b y C o n f R O S S C o n f B a R u C o E u R u K o ) n a m e s = % w ( T a t e I s o m G e n e v i e v e O l a A r i e l l e ) . r e v e r s e m a t r i x = [ 0 b 1 1 1 0 1 , 0 b 1 0 0 0 0 , 0 b 0 0 0 1 1 , 0 b 0 1 1 0 1 , 0 b 1 0 1 0 1 ] m a t r i x [ c o n f s . i n d e x ( ' e u r u c a m p ' ) ] [ n a m e s . i n d e x ( ' T a t e ' ) ] # = > 1 m a t r i x [ c o n f s . i n d e x ( ' R O S S C o n f ' ) ] [ n a m e s . i n d e x ( ' G e n e v i e v e ' ) ] # = > 0

Page 13: Piotr Szotkowski about "Bits of ruby"

so… how many go to each conf?e u r u c a m p = m a t r i x [ c o n f s . i n d e x ( ' e u r u c a m p ' ) ] # = > [ t r u e , t r u e , t r u e , f a l s e , t r u e ] e u r u c a m p . c o u n t ( t r u e ) # = > 4

e u r u c a m p = m a t r i x [ c o n f s . i n d e x ( ' e u r u c a m p ' ) ] # = > 0 b 1 1 1 0 1 e u r u c a m p . t o _ s ( 2 ) . c o u n t ( ' 1 ' ) # = > 4

number of ones in a binary representation

‘population count’

but to_s(2).count('1') is not very efficient

Page 14: Piotr Szotkowski about "Bits of ruby"

let’s freedom­patch Integerc l a s s I n t e g e r

d e f p o p c o u n t _ t o _ s t o _ s ( 2 ) . c o u n t ( ' 1 ' ) e n d

d e f p o p c o u n t _ c o n t _ s h i f t c o u n t = 0 n u m b e r = s e l f u n t i l n u m b e r = = 0 c o u n t + = n u m b e r & 1 n u m b e r > > = 1 e n d c o u n t e n d

e n d

Page 15: Piotr Szotkowski about "Bits of ruby"

c l a s s I n t e g e r

d e f p o p c o u n t _ b i t _ e l i m c o u n t = 0 n u m b e r = s e l f w h i l e n u m b e r ! = 0 n u m b e r & = n u m b e r - 1 c o u n t + = 1 e n d c o u n t e n d

d e f p o p c o u n t _ p r o g _ s h i f t n u m b e r = s e l f n u m b e r - = ( n u m b e r > > 1 ) & 0 x 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 n u m b e r = ( n u m b e r & 0 x 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 ) + ( ( n u m b e r > > 2 ) & 0 x 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 ) n u m b e r = ( n u m b e r + ( n u m b e r > > 4 ) ) & 0 x 0 f 0 f 0 f 0 f 0 f 0 f 0 f 0 f n u m b e r = ( n u m b e r + ( n u m b e r > > 8 ) ) & 0 x 0 0 f f 0 0 f f 0 0 f f 0 0 f f n u m b e r = ( n u m b e r + ( n u m b e r > > 1 6 ) ) & 0 x 0 0 0 0 f f f f 0 0 0 0 f f f f n u m b e r + = n u m b e r > > 3 2 ( n u m b e r + ( n u m b e r > > 6 4 ) ) & 0 x f f e n d

e n d

Page 16: Piotr Szotkowski about "Bits of ruby"

r e q u i r e ' i n l i n e ' c l a s s I n t e g e r

i n l i n e d o | b u i l d e r | b u i l d e r . c ' i n t p o p c o u n t _ b i t _ e l i m _ c ( ) { l o n g n u m b e r = N U M 2 L O N G ( s e l f ) ; i n t c o u n t ; f o r ( c o u n t = 0 ; n u m b e r ; c o u n t + + ) n u m b e r & = n u m b e r - 1 ; r e t u r n c o u n t ; } ' e n d

i n l i n e d o | b u i l d e r | b u i l d e r . c ' i n t p o p c o u n t _ b u i l t i n ( ) { r e t u r n _ _ b u i l t i n _ p o p c o u n t l ( N U M 2 L O N G ( s e l f ) ) ; } ' e n d

e n d

Page 17: Piotr Szotkowski about "Bits of ruby"

c l a s s I n t e g e r

P O P C O U N T _ C A C H E = ( 0 x 0 0 0 0 . . 0 x f f f f ) . m a p { | n u m b e r | n u m b e r . t o _ s ( 2 ) . c o u n t ( ' 1 ' ) } d e f p o p c o u n t _ c a c h e d P O P C O U N T _ C A C H E [ s e l f & 0 x f f f f ] + P O P C O U N T _ C A C H E [ s e l f > > 1 6 & 0 x f f f f ] + P O P C O U N T _ C A C H E [ s e l f > > 3 2 & 0 x f f f f ] + P O P C O U N T _ C A C H E [ s e l f > > 4 8 ] e n d

e n d

Page 18: Piotr Szotkowski about "Bits of ruby"

r e q u i r e ' b e n c h m a r k / i p s '

m e t h o d s = I n t e g e r . i n s t a n c e _ m e t h o d s . g r e p ( / ^ p o p c o u n t _ / ) n u m b e r s = A r r a y . n e w ( 1 _ 0 0 0 ) { r a n d ( 0 . . . ( 2 * * 6 2 - 1 ) ) }

f a i l ' o o p s ' u n l e s s m e t h o d s . m a p { | m e t h | n u m b e r s . m a p ( & m e t h ) } . u n i q . s i z e = = 1

B e n c h m a r k . i p s d o | b e n c h | m e t h o d s . e a c h d o | m e t h | b e n c h . r e p o r t ( m e t h [ 9 . . - 1 ] ) { n u m b e r s . m a p ( & m e t h ) } e n d b e n c h . c o m p a r e ! e n d

Page 19: Piotr Szotkowski about "Bits of ruby"

W a r m i n g u p - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - b i t _ e l i m 5 3 . 0 0 0 i / 1 0 0 m s c a c h e d 2 7 6 . 0 0 0 i / 1 0 0 m s c o n t _ s h i f t 1 7 . 0 0 0 i / 1 0 0 m s p r o g _ s h i f t 1 3 1 . 0 0 0 i / 1 0 0 m s t o _ s 6 9 . 0 0 0 i / 1 0 0 m s b i t _ e l i m _ c 1 . 0 9 8 k i / 1 0 0 m s b u i l t i n 1 . 4 5 3 k i / 1 0 0 m s C a l c u l a t i n g - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - b i t _ e l i m 5 4 1 . 1 2 7 ( ± 0 . 4 % ) i / s - 2 . 7 5 6 k c a c h e d 2 . 8 0 6 k ( ± 0 . 3 % ) i / s - 1 4 . 0 7 6 k c o n t _ s h i f t 1 8 9 . 9 2 1 ( ± 1 . 1 % ) i / s - 9 5 2 . 0 0 0 p r o g _ s h i f t 1 . 5 9 6 k ( ± 0 . 3 % ) i / s - 7 . 9 9 1 k t o _ s 6 9 5 . 5 2 8 ( ± 0 . 4 % ) i / s - 3 . 5 1 9 k b i t _ e l i m _ c 1 1 . 1 7 2 k ( ± 1 . 4 % ) i / s - 5 5 . 9 9 8 k b u i l t i n 1 4 . 6 6 1 k ( ± 1 . 1 % ) i / s - 7 4 . 1 0 3 k C o m p a r i s o n : b u i l t i n : 1 4 6 6 1 . 1 i / s b i t _ e l i m _ c : 1 1 1 7 1 . 8 i / s - 1 . 3 1 x s l o w e r c a c h e d : 2 8 0 5 . 6 i / s - 5 . 2 3 x s l o w e r p r o g _ s h i f t : 1 5 9 5 . 7 i / s - 9 . 1 9 x s l o w e r t o _ s : 6 9 5 . 5 i / s - 2 1 . 0 8 x s l o w e r b i t _ e l i m : 5 4 1 . 1 i / s - 2 7 . 0 9 x s l o w e r c o n t _ s h i f t : 1 8 9 . 9 i / s - 7 7 . 2 0 x s l o w e r

Page 20: Piotr Szotkowski about "Bits of ruby"

Tate Isom Genevieve Ola Arielle

eurucamp ✓ ✓ ✓ ✓

JRubyConf ✓

ROSSConf ✓ ✓

BaRuCo ✓ ✓ ✓

EuRuKo ✓ ✓ ✓

Page 22: Piotr Szotkowski about "Bits of ruby"

Ἲ�tip to RGSoC­friendly confs♥ eurucamp ♥ JRubyConf ♥ Deccan RubyConf ♥

♥ OTSConf ♥ Madison+ Ruby ♥ BaRuCo ♥

♥ Reject.JS ♥ Strange Loop ♥ RailsClub ♥

♥ EuRuKo ♥ Øredev ♥ ROSSConf ♥

♥ GOTO Conference ♥ dotJS ♥

Page 23: Piotr Szotkowski about "Bits of ruby"

thanks!

@chastell

talks.chastell.net