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

new_urlparser.pdf

 new_urlparser.pdf

Yosuke Furukawa

May 09, 2023
Tweet

More Decks by Yosuke Furukawa

Other Decks in Programming

Transcript

  1. Node.js v20 Ͱ௥Ճ͞Ε
    ͨ URL Parser Ada ʹͭ
    ͍ͯ
    2023/05/09 @ ؔ੢NodeֶԂ

    View full-size slide

  2. Twitter: @yosuke_furukawa


    Github: yosuke-furukawa

    View full-size slide

  3. URL Parser ͳʹͦΕ͓͍͍͠
    ͷʁ
    • URL Parser ͦ΋ͦ΋Կʁ
    > new URL("https://example.com");
    URL {
    href: 'https://example.com/',
    origin: 'https://example.com',
    protocol: 'https:',
    username: '',
    password: '',
    host: 'example.com',
    hostname: 'example.com',
    port: '',
    pathname: '/',
    search: '',
    searchParams: URLSearchParams {},
    hash: ''
    }

    View full-size slide

  4. URL Parser ͳʹͦΕ͓͍͍͠
    ͷʁ
    • URL Parser ͦ΋ͦ΋Կʁ
    > new URL("https://example.com");
    URL {
    href: 'https://example.com/',
    origin: 'https://example.com',
    protocol: 'https:',
    username: '',
    password: '',
    host: 'example.com',
    hostname: 'example.com',
    port: '',
    pathname: '/',
    search: '',
    searchParams: URLSearchParams {},
    hash: ''
    }
    ͜͏͍͏΍ͭ

    View full-size slide

  5. URLͬͯͨ͘͞ΜͷύʔπͰͰ
    ͖ͯΜͶΜɻ
    • ͦΕͧΕ෦෼͝ͱʹ෼ׂ͓ͯ͘͠ͱ৭ʑྑ͍͜ͱ͕͋Δɻ


    • ྫ͑͹ϧʔςΟϯά


    • ྫ͑͹ϦΫΤετ࣮ߦ


    • Node.js ͩͬͯ ESM ͷϩʔμʔ಺ͰϞδϡʔϧऔಘ͢Δͱ͖࢖ͬ
    ͯͨΓ͢Δ

    View full-size slide

  6. URL Parser ͕ʁͲ͏ͳͬͨΜʁ
    • ؆୯ʹݴ͏ͱ଎͘ͳͬͨɻ


    • Node.js v20 ͔Β v19 ͷൺֱ

    View full-size slide

  7. URL Parser ͕ʁͲ͏ͳͬͨΜʁ
    • ͞ΒʹDeno v1.32.1 ΍Bun v0.5.8ͱͷൺֱ

    View full-size slide

  8. ԿΛͨ͠Β଎͘ͳͬͨͷ͔

    View full-size slide

  9. Legacy URL Parser
    • const url = require("url");


    • Ͱݺͼग़͢΍ͭ


    • JavaScript ੡


    • Legacy URL Parser ͱͯ͠ղઆ͞Ε͍ͯΔ


    • طʹࠓ͸ deprecated ѻ͍

    View full-size slide

  10. Ұݸલͷ URL Parser
    • new URL() Ͱݺͼग़͢΍ͭ


    • JavaScript Ͱ binding ͞ΕͯΔ͕ɺ parser ຊମ
    ͸ C++ (not ada)


    • φΠʔϒͳ࣮૷


    • ଎͍͜ͱΑΓ΋ spec ʹ஧࣮ͳ͜ͱΛ໨ࢦͯͨ͠

    View full-size slide

  11. JavaScript vs C++

    View full-size slide

  12. JavaScript vs C++
    • Αٞ͘࿦ʹͳΔ໰୊


    • JavaScript ͰίʔυΛॻ͘ͱॳظίετ͸஗͍͕ɺJITͷӨڹͰߴ଎ԽͰ
    ͖Δɻ


    • C++ ͰίʔυΛॻ͘ͱ଎͍͕ɺJITͷԸܙ͸ड͚ΒΕͳ͍ɻ


    • C++ Ͱશ෦ॻ͍ͨΒ͡Ό͋ྑ͍ͷ͔ʁͱ͍͏ͱɺͦ͏Ͱ΋ͳ͍ɻ։ൃί
    ετ΍ϝϞϦ΁ͷέΞ͕ॏཁʹͳΔɻ


    • ͜ͷ࣌఺Ͱ͸ WHATWG URL vs Legacy URL ͸ͲͪΒ΋࢒͍ͯ͘͠ํ਑
    Ͱ։ൃ͞Ε͍ͯͯɺύϑΥʔϚϯεൺֱ͸ͦΜͳʹଟ͘͞Ε͍ͯͳ͍ɻ

    View full-size slide

  13. JavaScript and C++

    View full-size slide

  14. JavaScript and C++
    • ݁ہ C++ Ͱॻ͘ͱͯ͠΋ JavaScript ͷΠϯλϑΣʔεͰఏڙ͞
    Ε͍ͯΔͷͰɺJS <=> C++ ؒͷ৘ใͷड͚౉͕͠ඞཁʹͳΔɻ


    • ݴ͍׵͑Δͱ JS ͷੈքͱ C++ ͷੈք͕ Node.js ʹ͸ଘࡏ͢Δɻ


    • JS ͔Β C++ ʹ৘ใͷड͚౉͠Λ͢Δ࣌ʹߦ͍ͬͯΔͷ͕ String
    ͷ serialize ͱ͍͏ॲཧɻ


    • Node.jsͰBuffer Λ௚઀࢖ͬͨ͜ͱ͕͋Δਓ͸ serialize ͞Εͨจ
    ࣈྻΛ௚઀࢖͍ͬͯΔɻ

    View full-size slide

  15. Serialize ॲཧΛ೗Կʹߴ଎ʹ
    ͢Δ͔͕ϙΠϯτ

    View full-size slide

  16. ݱࡏͷ URL Parser
    • new URL() Ͱݺͼग़͢΍ͭ


    • Ada ͱݺ͹ΕΔύʔαʔ(C++)Ͱ࣮૷͞Εͯ
    Δɻ


    • JSͱC++ͷόΠϯσΟϯά΋༻ҙ͞Ε͍ͯΔɻ


    • String ͷ serialize ΛۃྗলྗԽ͍ͯ͠Δɻ

    View full-size slide

  17. ࡞ઓ1:


    จࣈྻΛύʔε͢Δ࣌ʹશ෦
    Λจࣈʹม׵͠ͳ͍࡞ઓ

    View full-size slide

  18. શ෦Λจࣈʹม׵͠ͳ͍࡞ઓ
    • URL ͷͲ͔͜ΒͲ͜·Ͱ͕Կͷ৘ใͳͷ͔ͱ
    ͍͏ offset ஋͚͓ͩ࣋ͬͯ͘ɻ

    View full-size slide

  19. શ෦Λจࣈʹม׵͠ͳ͍࡞ઓ
    • URL ͷͲ͔͜ΒͲ͜·Ͱ͕Կͷ৘ใͳͷ͔ͱ
    ͍͏ offset ஋͚͓ͩ࣋ͬͯ͘ɻ
    จࣈྻͷTFSJBMJ[F͸Ұճ͚ͩʂͲ͜ͷจࣈ͔ΒͲ͜ͷจࣈ·Ͱ͕Կͳͷ
    ͔Λ΋͓͖ͬͯɺͦΕ͕௚઀+4ʹόΠϯυ͞ΕΔ࢓૊ΈΛ࡞ͬͯΔ

    View full-size slide

  20. ࡞ઓ2:


    C++͔ΒJSʹ௚઀όΠϯσΟ
    ϯά͢Δ࡞ઓ

    View full-size slide

  21. C++͔ΒJSʹ௚઀όΠϯσΟϯ
    ά͢Δ࡞ઓ
    • AliasedUint32Arrayͱݺ͹ΕΔಛघͳ
    TypedArray Λ࢖ͬͯ C++ ࣮૷͔Β௚઀JSʹ
    ૹΔɻ


    • Serialize Λͱ͜ͱΜߴ଎Խ͢Δɻ

    View full-size slide

  22. ࡞ઓ3:


    ஗ԆಡΈࠐΈ

    View full-size slide

  23. ஗ԆಡΈࠐΈ
    • ඞཁʹͳΔΪϦΪϦ·Ͱ࣮ߦ͠ͳ͍࡞ઓ


    • ΞΫηε͢Δͱ͖ͷඞཁͳϓϩύςΟ͕Θ
    ͔ͬͨΒͦ͜·ͰύʔεΛߦ͏ɻ

    View full-size slide

  24. ͜͏ͯ͠ߴ଎Խ͞Εͨ


    Ada Λ࢖ͬͨURL Parser͕


    ஀ੜͨ͠

    View full-size slide

  25. Enjoy Node.js v20!!

    View full-size slide