Download - Ruby performance distilled, com Hanneli Tavante

Transcript

Ruby performance distilled

Hi! I’m Hanneli

No advanced stuff

Fast snippets

You can agree or not

Why speed?

( °͡ ʖ͜ °͡) ( °͡ ʖ͜ °͡) ( °͡ ʖ͜ °͡) ( °͡ ʖ͜ °͡) ( °͡ ʖ͜ °͡) ( °͡ ʖ͜ °͡) ( °͡ ʖ͜ °͡) ( °͡ ʖ͜ °͡) ( °͡ ʖ͜ °͡) ( °͡ ʖ͜ °͡) ( °͡ ʖ͜ °͡) ( °͡ ʖ͜ °͡) ( °͡ ʖ͜ °͡) ( °͡ ʖ͜ °͡) ( °͡ ʖ͜ °͡) ( °͡ ʖ͜ °͡) ( °͡ ʖ͜ °͡) ( °͡ ʖ͜ °͡) ( °͡ ʖ͜ °͡) ( °͡ ʖ͜ °͡) ( °͡ ʖ͜ °͡) ( °͡ ʖ͜ °͡) ( °͡ ʖ͜ °͡) ( °͡ ʖ͜ °͡) ( °͡ ʖ͜ °͡)

Optimize makes my code uglier

( °͡ ʖ͜ °͡) ( °͡ ʖ͜ °͡) ( °͡ ʖ͜ °͡) ( °͡ ʖ͜ °͡) ( °͡ ʖ͜ °͡) ( °͡ ʖ͜ °͡) ( °͡ ʖ͜ °͡) ( °͡ ʖ͜ °͡) ( °͡ ʖ͜ °͡) ( °͡ ʖ͜ °͡) ( °͡ ʖ͜ °͡) ( °͡ ʖ͜ °͡) ( °͡ ʖ͜ °͡) ( °͡ ʖ͜ °͡) ( °͡ ʖ͜ °͡) ( °͡ ʖ͜ °͡) ( °͡ ʖ͜ °͡) ( °͡ ʖ͜ °͡) ( °͡ ʖ͜ °͡) ( °͡ ʖ͜ °͡) ( °͡ ʖ͜ °͡) ( °͡ ʖ͜ °͡) ( °͡ ʖ͜ °͡) ( °͡ ʖ͜ °͡) ( °͡ ʖ͜ °͡)

Optimize makes my code uglier

IoT

128GB RAM at Amazon 1GB RAM at Raspberry

There are several layers to optimise

Source code optimisation{Search timeSorting timeComplexityData Structures

Example #1 - countingLoops are very common for Hardware

(1…65536).map { |i| i.to_s } (1…65536).map { &:to_s }

Example #2 - mergeHashes happen a lot in Hardware

merge! merge

Example #3 - sub/gsubSubstitutions (that occur only once) happen in Hardware

“AD1”.gsub(“AD”, “DA") “AD1”.sub(“AD”, “DA")

Example #4 - permutePermutation happens in Hardware

array.permutation home made solution

Example #4 - permutePermutation happens in Hardware

else { /* this is the general case */ volatile VALUE t0; long *p = (long*)ALLOCV(t0, r*sizeof(long)+n*sizeof(char)); char *used = (char*)(p + r); VALUE ary0 = ary_make_shared_copy(ary); /* private defensive copy of ary */ RBASIC_CLEAR_CLASS(ary0);

MEMZERO(used, char, n); /* initialize array */

permute0(n, r, p, used, ary0); /* compute and yield permutations */ ALLOCV_END(t0); RBASIC_SET_CLASS_RAW(ary0, rb_cArray); } return ary;

RECURSIVE STUFF AAAAAAA

Example #5 - reversereverse happens A LOT in Hardware

reverse reverse! other

Example #5 - reversereverse happens A LOT in Hardware static VALUErb_ary_reverse_m(VALUE ary){ long len = RARRAY_LEN(ary); VALUE dup = rb_ary_new2(len);

if (len > 0) { const VALUE *p1 = RARRAY_CONST_PTR(ary); VALUE *p2 = (VALUE *)RARRAY_CONST_PTR(dup) + len - 1; do *p2-- = *p1++; while (--len > 0); } ARY_SET_LEN(dup, RARRAY_LEN(ary)); return dup;}

༼ つ ◕_◕ ༽つ༼ つ ◕_◕ ༽つ

References

• https://vimeo.com/114848080

• http://chriscontinanza.com/2010/10/29/Array.html

• http://www.cse.dmu.ac.uk/~hgs/ruby/performance/

• https://www.youtube.com/watch?v=fGFM_UrSp70