Slide 1

Slide 1 text

Array#+ ͱ Array#concatͷ ϝϞϦͷ࢖͍ํͷҧ͍ @morihirok

Slide 2

Slide 2 text

ഁյతϝιουͱඇഁյతϝ ιου • ඇഁյతϝιου͸Ϩγʔόͷঢ়ଶʹӨڹΛٴ΅ ͢ • Array#+ ͸ඇഁյతϝιου • ഁյతϝιου͸Ϩγʔόͷঢ়ଶʹӨڹΛٴ΅͢ • Array#concat͸ഁյతϝιου

Slide 3

Slide 3 text

#+ ͱ #concat ͷҧ͍ ary1 = ['hoge', 'fuga'] ary2 = ['foo', ‘bar'] ary3 = ary1 + ary2 p ary3 => ["hoge", "fuga", "foo", "bar"] p ary1 => ["hoge", “fuga"] ary1.concat(ary2) p ary1 => ["hoge", "fuga", "foo", "bar"]

Slide 4

Slide 4 text

ඇഁյతϝιου͸ϝϞϦޮ཰ ͕ѱ͍ͷͰ͸ʁ ary1 = ['hoge', 'fuga'] ary2 = ['foo', ‘bar'] ary3 = ary1 + ary2 p ary3 => ["hoge", "fuga", "foo", "bar"] p ary1 => ["hoge", “fuga"] ary1.concat(ary2) p ary1 => ["hoge", "fuga", "foo", "bar"] ৽ͨͳ഑ྻΛੜ੒͍ͯ͠Δ

Slide 5

Slide 5 text

ͱ͍͏Θ͚Ͱௐࠪ ary1 = ['hoge', 'fuga'] ary2 = ['foo', 'bar'] ary1.object_id => 180 ary1.map(&:object_id) => [200, 220] ary2.object_id => 240 ary2.map(&:object_id) => [260, 280]

Slide 6

Slide 6 text

Array#concat ͷ৔߹ p ary1.object_id => 180 p ary1.map(&:object_id) => [200, 220] p ary2.map(&:object_id) => [260, 280] ary1.concat(ary2) p ary1.object_id => 180 p ary1.map(&:object_id) => [200, 220, 260, 280] ಉҰͷΦϒδΣΫτΛอ࣋͢Δ ഑ྻ͕׬੒

Slide 7

Slide 7 text

Array#+ ͷ৔߹ p ary1.map(&:object_id) => [200, 220] p ary2.map(&:object_id) => [260, 280] ary3 = ary1 + ary2 ary3.object_id => 300 ary3.map(&:object_id) => [200, 220, 260, 280] ಉҰͷΦϒδΣΫτΛอ࣋͢Δ ഑ྻ͕׬੒ ৽͍͠ΦϒδΣΫτ͕ੜ੒͞Ε ͦΕΛอ࣋͢Δ഑ྻ͕Ͱ͖Δͱ ࢥ͍ͬͯͨʜ

Slide 8

Slide 8 text

Arrayʹ͍ͭͯ צҧ͍͍ͯ͠Δ͔΋͠Εͳ͍

Slide 9

Slide 9 text

Arrayͷߏ଄ମʹ͍ͭͯௐ΂Δ struct RArray { struct RBasic basic; union { struct { long len; union { long capa; #if defined(__clang__) /* <- clang++ is sane */ || \ !defined(__cplusplus) /* <- C99 is sane */ || \ (__cplusplus > 199711L) /* <- C++11 is sane */ const #endif VALUE shared_root; } aux; const VALUE *ptr; } heap; const VALUE ary[RARRAY_EMBED_LEN_MAX]; } as; };

Slide 10

Slide 10 text

Arrayͷߏ଄ମʹ͍ͭͯௐ΂Δ struct RArray { struct RBasic basic; union { struct { long len; union { long capa; #if defined(__clang__) /* <- clang++ is sane */ || \ !defined(__cplusplus) /* <- C99 is sane */ || \ (__cplusplus > 199711L) /* <- C++11 is sane */ const #endif VALUE shared_root; } aux; const VALUE *ptr; } heap; const VALUE ary[RARRAY_EMBED_LEN_MAX]; } as; }; ϫʔΫΞϥ΢ϯυͷΑ͏ͳͷͰ ಡΈਐΊ͍ͯ͘ʹ͋ͨͬͯ Ұ୴ແࢹͯ͠ΈΔ

Slide 11

Slide 11 text

Arrayͷߏ଄ମʹ͍ͭͯௐ΂Δ struct RArray { struct RBasic basic; union { struct { long len; union { long capa; const VALUE shared_root; } aux; const VALUE *ptr; } heap; const VALUE ary[RARRAY_EMBED_LEN_MAX]; } as; };

Slide 12

Slide 12 text

Arrayͷߏ଄ମʹ͍ͭͯௐ΂Δ struct RArray { struct RBasic basic; union { struct { long len; union { long capa; const VALUE shared_root; } aux; const VALUE *ptr; } heap; const VALUE ary[RARRAY_EMBED_LEN_MAX]; } as; }; ࣗ਎ͷܕ΍ॴଐΫϥε΍ϑϥά શͯͷΦϒδΣΫτߏ଄ମ͕࣋ͭ ࣗ਎ͷ഑ྻͷ਺ ഑ྻͷશମαΠζ ڞ༗ϑϥάͬΆ͍

Slide 13

Slide 13 text

Arrayͷߏ଄ମʹ͍ͭͯௐ΂Δ struct RArray { struct RBasic basic; union { struct { long len; union { long capa; const VALUE shared_root; } aux; const VALUE *ptr; } heap; const VALUE ary[RARRAY_EMBED_LEN_MAX]; } as; }; ࣮ମϝϞϦ΁ͷϙΠϯλ ཁૉ਺͕3"33":@&.#&%@-&.@."9ҎԼͩͱ 3"SSBZߏ଄ମͷதʹ഑ྻΛ࣋ͭΒ͍͠

Slide 14

Slide 14 text

VALUEͱ͸ • ߏ଄ମ΁ͷϙΠϯλΛද͢ • ͭ·ΓRubyͷArrayͷ࣮ମϝϞϦ͸ unsigned longͷ഑ྻ ruby.h :102 typedef unsigned long VALUE;

Slide 15

Slide 15 text

Array#+ͷ࣮૷ VALUE rb_ary_plus(VALUE x, VALUE y) { VALUE z; long len, xlen, ylen; y = to_ary(y); xlen = RARRAY_LEN(x); ylen = RARRAY_LEN(y); len = xlen + ylen; z = rb_ary_new2(len); ary_memcpy(z, 0, xlen, RARRAY_CONST_PTR_TRANSIENT(x)); ary_memcpy(z, xlen, ylen, RARRAY_CONST_PTR_TRANSIENT(y)); ARY_SET_LEN(z, len); return z; }

Slide 16

Slide 16 text

Array#+ͷ࣮૷ VALUE rb_ary_plus(VALUE x, VALUE y) { VALUE z; long len, xlen, ylen; y = to_ary(y); xlen = RARRAY_LEN(x); ylen = RARRAY_LEN(y); len = xlen + ylen; z = rb_ary_new2(len); ary_memcpy(z, 0, xlen, RARRAY_CONST_PTR_TRANSIENT(x)); ary_memcpy(z, xlen, ylen, RARRAY_CONST_PTR_TRANSIENT(y)); ARY_SET_LEN(z, len); return z; } ৽͍͠3"SSBZߏ଄ମΛ࡞Δ 7"-6&ΛNFNDQZ͢Δ ֤ΦϒδΣΫτߏ଄ମΛίϐʔ͢ΔΘ͚Ͱ͸ͳ͍

Slide 17

Slide 17 text

Array#concatͷ࣮૷ • RArrayߏ଄ମͷheap.aux.capaΛ֬ೝ • concatޙͷαΠζΑΓখ͔ͬͨ͞Βmalloc͠ ͨΓreallocͨ͠Γͯ͠ϝϞϦΛ֬อ͢Δ໛༷ • Ξϩέʔτ͞ΕͨϝϞϦʹVALUEΛmemcpy

Slide 18

Slide 18 text

·ͱΊ • ArrayͷඇഁյతϝιουͰੜ੒͞ΕΔͷ͸ unsigned intͷ഑ྻͳͷͰΊͬͪΌ௕͍Array ͱ͔Ͱͳ͚Ε͹ϝϞϦޮ཰͸ؾʹ͠ͳͯ͘Α ͦ͞͏ • Rubyͷ࣮૷ΛͪΐͬͱཧղͰ͖ͯΑ͔ͬͨ