Upgrade to Pro — share decks privately, control downloads, hide ads and more …

PHPerKaigi 2019

memory
March 29, 2019

PHPerKaigi 2019

memory

March 29, 2019
Tweet

More Decks by memory

Other Decks in Programming

Transcript

  1. ༧උ஌ࣝฤͰ࿩͢͜ͱ • 16ਐ਺ͷܭࢉํ๏ʹ͍ͭͯ • signed / unsgined ͳܕʹ͍ͭͯ • double

    ΍ float ͳͲͷුಈখ਺఺ͷܭࢉํ๏ʹ͍ͭͯ • ΤϯσΟΞϯʹ͍ͭͯ
  2. 16ਐ਺ͷܭࢉํ๏ • 10ਐ਺͔Β16ਐ਺ͷܭࢉํ๏͸Լهͷࣜʹ౰ͯ͸ΊΒΕ·͢ 0xFF = (16 - 1) * 16^1

    + (16 - 1) * 16^0 = 255(10) 1ܻ໨͔Βܻ਺͕૿͑Δͨͼʹ1৐,2৐…N৐ͱ૿͍͖͑ͯ·͢ɻͭ·Γɺ0xFFFFͷ ৔߹͸ԼهͷΑ͏ʹͳΓ·͢ɻ 0xFFFF = (16 - 1) * 16^3 +(16 - 1) * 16^2 + (16 - 1) * 16^1 + (16 - 1) * 16^0 = 65535(10) 16͔Β1ΛҾ͍͍ͯΔͷ͸ɺ։࢝͢Δ਺ࣈ͕0͔Β࢝·ΔͨΊͰ͢ɻA͸10, B͸ 11, … F͸15ͱ֮͑ͯ͠·͍·͠ΐ͏ɻ
  3. 16ਐ਺ͷܭࢉํ๏ • PHPͰܭࢉ͢Δʹ͸ʁ ͜Μͳܭࢉਖ਼௚΍Γͨ͘ͳ͍ɻϓϩάϥϚςΟοΫʹղܾͰ͖ΔͳΒղܾ͢Δʹӽ ͨ͜͠ͱ͸ͳ͍Ͱ͢ɻ • 10ਐ਺͔Β16ਐ਺ʹ͢Δ <?= dechex(255) ?>

    • 16ਐ਺͔Β10ਐ਺ʹ͢Δ <?= hexdec(‘0xFF’) ?> <?= base_convert(255, 10, 16) ?> <?= base_convert(‘0xFF’, 16, 10) ?>
  4. PHP͔Βݟͯ૿͑ͨܕ • boolean • int (4 byte) • string •

    float • ࣮࣭ float ͷ double • byte (1 byte) • short (2 byte) • long (8 byte) • Ψν float (4 byte, ୯ਫ਼౓ුಈখ਺఺਺) • Ψν double (8 byte, ഒਫ਼౓ුಈখ਺఺਺)
  5. ֤ܕʹ͍ͭͯ - byte • signed byte (ූ߸෇͖ byte) • -128(10)

    ʙ 127(10) ͕ ූ߸෇͖ byte ͷൣғͱͳΓ·͢ɻ • 16ਐ਺Ͱ͸ 0x00 ʙ 0xFF ·ͰͰ͢ɻ • ਖ਼ͷ਺͸ 0x00ʙ0x7F, ෛͷ਺͸ 0x80 ʙ 0xFF ͱͳΓ·͢ɻ • unsigned byte (ූ߸ͳ͠ byte) • 0(10) ʙ 255(10) ͕ූ߸ͳ͠ byte ͷൣғͱͳΓ·͢ɻ • 16ਐ਺Ͱ͸ 0x00 ʙ 0xFF ·ͰͰ͢ • ਖ਼ͷ਺ͷΈͰ͢ɻ
  6. ֤ܕʹ͍ͭͯ - short • signed short (ූ߸෇͖ short) • -32768(10)

    ʙ 32767(10) ͕ ූ߸෇͖ short ͷൣғͱͳΓ·͢ɻ • 16ਐ਺Ͱ͸ 0x0000 ʙ 0xFFFF ·ͰͰ͢ɻ • ਖ਼ͷ਺͸ 0x0000ʙ0x7FFF, ෛͷ਺͸ 0x8000 ʙ 0xFFFF ͱͳΓ·͢ɻ • unsigned short (ූ߸ͳ͠ short) • 0(10) ʙ 65535(10) ͕ූ߸ͳ͠ short ͷൣғͱͳΓ·͢ɻ • 16ਐ਺Ͱ͸ 0x0000 ʙ 0xFFFF ·ͰͰ͢ • ਖ਼ͷ਺ͷΈͰ͢ɻ
  7. ֤ܕʹ͍ͭͯ - int • signed int (ූ߸෇͖ int) • -2147483648(10)

    ʙ 2147483647(10) ͕ ූ߸෇͖ int ͷൣғͱͳΓ· ͢ɻ • 16ਐ਺Ͱ͸ 0x000000 ʙ 0xFFFFFF ·ͰͰ͢ɻ • ਖ਼ͷ਺͸ 0x000000ʙ0x7FFFFF, ෛͷ਺͸ 0x800000 ʙ 0xFFFFFF ͱͳ Γ·͢ɻ • unsigned int (ූ߸ͳ͠ int) • 0(10) ʙ 4294967295(10) ͕ූ߸ͳ͠ int ͷൣғͱͳΓ·͢ɻ • 16ਐ਺Ͱ͸ 0x000000 ʙ 0xFFFFFF ·ͰͰ͢ • ਖ਼ͷ਺ͷΈͰ͢ɻ
  8. ֤ܕʹ͍ͭͯ - long • signed long (ූ߸෇͖ long) • -9223372036854775808(10)

    ʙ 9223372036854775807(10) ͕ ූ߸෇͖ long ͷൣғͱͳΓ·͢ɻ • 16ਐ਺Ͱ͸ 0x00000000 ʙ 0xFFFFFFFF ·ͰͰ͢ɻ • ਖ਼ͷ਺͸ 0x00000000ʙ0x7FFFFFFF, ෛͷ਺͸ 0x80000000 ʙ 0xFFFFFFFF ͱͳΓ·͢ɻ • unsigned long (ූ߸ͳ͠ long) • 0(10) ʙ 18446744073709551615(10) ͕ූ߸ͳ͠ long ͷൣғͱͳΓ· ͢ɻ • 16ਐ਺Ͱ͸ 0x00000000 ʙ 0xFFFFFFFF ·ͰͰ͢ • ਖ਼ͷ਺ͷΈͰ͢ɻ
  9. PHP ͰܭࢉͰ͖ͳ͍஋Λܭࢉ͢Δ • PHP Ͱ͸ unsigned long ͷ஋ΛܭࢉͰ͖ͳ͍͕ɺ gmpؔ਺ Ͱܭࢉͤ͞Δ͜ͱ͸Մೳɻྫ͑͹

    <?= gmp_strval(gmp_add(PHP_INT_MAX, PHP_INT_MAX));" ?> • ݁Ռ͸ 18446744073709551614 ͱͳΓ·͢
  10. ුಈখ਺఺ͷܭࢉํ๏ • ුಈখ਺఺ͷܭࢉํ๏Ͱ༗໊ॴ͸Լه͔ͱࢥ͍·͢ɻ • ୯ਫ਼౓ුಈখ਺఺਺ • ഒਫ਼౓ුಈখ਺఺਺ • ͪͳΈʹ͜ΕΒ͸ IEEE754

    Ͱఆٛ͞Ε͍ͯΔͷͰৄ͘͠஌Γ ͍ͨํ͸ޚཡ͍ͩ͘͞ɻ • ୯ਫ਼౓ුಈখ਺఺਺͸ 32 Ϗοτ (4 byte) ഒਫ਼౓ුಈখ਺఺਺ ͸ 64 Ϗοτ (8 byte) ͷදݱ෯ͷҧ͍͕͋Γ·͢ɻ ࢀরɿhttps://ja.wikipedia.org/wiki/IEEE_754 ը૾ʹ͍ͭͯɿhttps://creativecommons.org/licenses/by-sa/3.0/
  11. ුಈখ਺఺ͷܭࢉํ๏ • ୯ਫ਼౓ුಈখ਺఺਺ (float) ͷܭࢉํ๏ • sign ͕ූ߸ͱͳΓ·͢ɻ (sͱஔ͖·͢) •

    exponent ͕ࢦ਺෦ͱͳΓ·͢ɻ (e ͱஔ͖·͢) • fraction ͕Ծ਺෦ͱͳΓ·͢ɻ (f ͱஔ͖·͢ɻ) ࢀরɿhttps://ja.wikipedia.org/wiki/IEEE_754 ը૾ʹ͍ͭͯɿhttps://creativecommons.org/licenses/by-sa/3.0/ ୯ਫ਼౓ුಈখ਺఺਺ͷܭࢉࣜ͸ = s * f * 2^(e - 127)
  12. ුಈখ਺఺ͷܭࢉํ๏ • ഒਫ਼౓ුಈখ਺఺਺ (double) ͷܭࢉํ๏ • جຊతʹ͸୯ਫ਼౓ුಈখ਺఺਺ͱมΘΓ·ͤΜɻ • sign ͕ූ߸ͱͳΓ·͢ɻ

    (sͱஔ͖·͢) • exponent ͕ࢦ਺෦ͱͳΓ·͢ɻ (e ͱஔ͖·͢) • fraction ͕Ծ਺෦ͱͳΓ·͢ɻ (f ͱஔ͖·͢ɻ) ࢀরɿhttps://ja.wikipedia.org/wiki/IEEE_754 ը૾ʹ͍ͭͯɿhttps://creativecommons.org/licenses/by-sa/3.0/ ഒਫ਼౓ුಈখ਺఺਺ͷܭࢉࣜ͸ = s * f * 2^(e - 1023)
  13. ͓͞Β͍ •gmp_* ؔ਺Λ֮͑ͯؼΖ͏ •dechex / hexdec / base_convert ؔ਺Λ֮͑ͯ ؼΖ͏

    •signed, unsigned ͳ஋ʹ͍֮ͭͯ͑ͯؼΖ͏ •ුಈখ਺఺ͷܭࢉํ๏Λ֮͑ͯؼΖ͏ •ΤϯσΟΞϯʹ͍֮ͭͯ͑ͯؼΖ͏
  14. PHP Ͱ JVM ʹೖ໳͢Δ - ຊฤ - ຊ ฤ PHPerKaigi

    2019 2019/03/29 Ί΋Γʔ(@m3m0r7)
  15. PHP Ͱ JVM ΛಡΜͰԿָ͕͍͠ͷ͔ • ελοΫϚγϯ͕ͲͷΑ͏ʹಈ͍ͯΔ͔ཧղ͢Δ͜ͱ͕Ͱ͖ ͨؾ࣋ͪʹͳΔ • ͨͩ͠ JVM

    ͸ϩʔΧϧม਺ͷετΞ΍ϩʔυ͕ແݶେʹͰ ͖ΔͨΊ७ਮͳελοΫϚγϯͰ͸ͳ͍ • php ͷதؒίʔυ͕ཧղ͠΍͘͢ͳΔ • vld extension ͷؾ࣋ͪʹͳΕΔ • 30 ԯͷσόΠεͰ૸Δ͜ͱ͕Ͱ͖Δ • ͳΜͳΒ PHP ͕Քಇ͍ͯ͠Δαʔόʔ୆਺෼΋ؚΊΔͱ Java ΑΓ΋͍͢͝ͷͰ͸
  16. όΠφϦΛಡΉʹ͸ • PHPͷϑΝΠϧؔ਺Λ࢖͏ • fopen ؔ਺ • fread ؔ਺ •

    fseek ؔ਺ • int ΍ short ͳͲͷ஋Λܭࢉ͢Δ • base_convert ؔ਺ • pack / unpack ؔ਺ (ࠓճ͸͜ΕΛ࢖͍·͢) • ord / chr ؔ਺ • ؾ߹
  17. N ͸ unsigned int Ͱ ϏοάΤϯσΟΞϯ όΠτΦʔμʔ n ͸ unsigned

    short Ͱ ϏοάΤϯσΟΞϯ όΠτΦʔμʔ cafebabe 0 52 (JDK 8) 29 ͜͜Ͱ͍͏ int (4byte) ΍ short (2byte) ͷαΠζ͸ JVM ্Ͱͷ࿩ʹͳΓ·͢ɻ
  18. ॲཧͷํ๏͕ҟͳΔ • ࠓճ͸ Methodref (0x0A), Fieldref (0x09), String (0x08), Class

    (0x07), Utf8 (0x01), NameAndType(0x0C) ͷ6ͭͷΈ ରԠɻ Constant Kind Tag Read bytes CONSTANT_Methodref 0x0A 2 byte + 2 byte ( unsigned short + unsigned short ) CONSTANT_Fieldref 0x09 2 byte + 2 byte ( unsigned short + unsigned short ) CONSTANT_String 0x08 2 byte ( unsigned short ) CONSTANT_Class 0x07 2 byte ( unsigned short ) CONSTANT_Utf8 0x01 ( length = 2 byte ) + length bytes ( unsigned short + length bytes ) CONSTANT_NameAndType 0x0C 2 byte + 2 byte ( unsigned byte + unsigned byte )
  19. C ͸ unsigned byte constant pool count ͸ྺ࢙తͳ ཧ༝ʹΑΓ -1

    ͢Δ CONSTANT_Methodref CONSTANT_Fieldref CONSTANT_String CONSTANT_Class
  20. unsigned short access flag unsigned short this class unsigned short

    super class unsigned short interface count unsigned short fields count unsigned short methods count
  21. unsigned short unsigned short unsigned short unsigned short ϝιουʹ͸ Code

    Attribute ؚ͕·ΕΔ ʢͦΕҎ֎΋ؚ·Ε·͕͢লུ͠·͢ʣ
  22. unsigned short length byte unsigned int Code Attribute ʹ͸ LineNumberTable

    Attribute ؚ͕·Ε·͢ ʢͦΕҎ֎΋ؚ·Ε·͕͢লུ͠·͢ʣ
  23. unsigned short attribute count unsigned short attribute name index unsigned

    int attribute length unsigned short sourcefile index
  24. PHP Ͱ JVM Λಈ͔͢ χʔϞχοΫͱ͸ Operation Code ͱ͸ iload_0 ͬͯͳΜͩ…

    tableswitch ͱ lookupswitch ͷҧ͍ Φϖϥϯυ ϩʔΧϧετϨʔδ invokespecial ͱ invokevirtual ಥવͷ <clinit> ͳͥ PHP Ͱ΍ΔΜͩ…