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

High-performance Jackson #渋谷Java

High-performance Jackson #渋谷Java

第17回 #渋谷Java の発表資料です
http://shibuya-java.connpass.com/event/38674/

KOMIYA Atsushi

October 01, 2016
Tweet

More Decks by KOMIYA Atsushi

Other Decks in Programming

Transcript

  1. “Jackson is a suite of data- processing tools for Java”

    https://github.com/FasterXML/jackson/blob/master/README.md
  2. Jackson • JSON processor • Streaming API • Data binding

    (POJO to and from JSON) • Other formats • XML, YAML • Avro, CBOR, CSV, Smile, Protobuf
  3. Popular JSON Libraries • 4,180 usages: Jackson Databind • 2,820

    usages: Gson • 1,238 usages: Json (org.json) • 516 usages: JSON.simple • 494 usages: Jersey Json ※mvnrepository.com ௐ΂ɺ2016-10-01 ࣌఺
  4. Jackson as JSON processor • Pros • γϦΞϥΠζɾσγϦΞϥΠζͱ΋ʹ଎͍ • ࠷଎Ͱ͸ͳͦ͞͏͚ͩͲ…

    • ػೳ͕๛෋Ͱɺᙱ͍ͱ͜Ζʹख͕ಧ͖΍͍͢ • ৼΔ෣͍ͷΧελϚΠζ͸·͋·͋͠΍͍͢ • ͍͍ҙຯͰރΕ͍ͯΔ ※ݸਓͷײ૝Ͱ͋Γɺੑೳɾੑ࣭Λอূ͢Δ΋ͷͰ͸͋Γ·ͤΜ
  5. Jackson as JSON processor • Cons • ମܥతʹهड़͞Εͨྑ࣭ͳ೔ຊޠυΩϡϝϯτ͸ͳ͍ • ػೳ͕๛෋ա͗Δ͕Ώ͑ʹɺ࢖͍͜ͳ͢ͷ͸೉͍͠

    • ʮ͜͏͍͏͜ͱ͸Ͱ͖Δͷ͔ͳʁʯ
 ʮͲ͏΍ͬͯઃఆ͢Ε͹͍͍Μͩʁʯ • ࣮૷͸៉ྷ͚ͩͲɺΫϥε਺͕ଟͯ͘ॳݟࡴ͠ײ͕͋ Δ ※ݸਓͷײ૝Ͱ͋Γɺੑೳɾੑ࣭Λอূ͢Δ΋ͷͰ͸͋Γ·ͤΜ
  6. Shortest path to high-performance • Presentation: Jackson Performance • https://github.com/FasterXML/jackson-docs/

    wiki/Presentation:-Jackson-Performance • Jackson Best Practices: Performance • http://wiki.fasterxml.com/ JacksonBestPracticesPerformance
  7. ࠓճର৅ͱ͍ͯ͠ΔλεΫ • 1 ߦ͝ͱʹ JSON ͕ه࿥͞ΕͨϑΝΠϧ
 (= Line delimited JSON)

    ͕༩͑ΒΕ͍ͯΔ • JSON 1 ݅͝ͱʹಛఆͷॲཧΛ͍ͨ͠ • 㱺 JSON ͷσγϦΞϥΠζΛ࠷଎ʹ❗
  8. ۩ମతͳαϯϓϧλεΫ • Twitter Streaming API Ͱ 1,000 ݅ͷ tweet Λऩूͯ͠ɺ̍ͭ

    ͷϑΝΠϧʹ·ͱΊ͓ͯ͘ ǖ DVSMHFUIUUQTTUSFBNUXJUUFSDPNTUBUVTFTTBNQMFKTPOǘ • શ෦Ͱ໿ 4MB = JSON 1 ͭ͋ͨΓ 4KB ͙Β͍ • ্هϑΝΠϧΛಡΈࠐΈɺ೔ຊޠ tweet ͷ݅਺Λ਺্͑͛Δ • ೔ຊޠ͔൱͔͸ “lang” ଐੑͷ஋Ͱ൑ఆ͢Δ • ͜ͷλεΫͷεϧʔϓοτ (ops/s) Λ JMH Ͱଌఆ͢Δ
  9. Baseline implementation • Best practice ʹࢥ͍͖ͬΓ൓͢Δɺ͋͑ͯ
 ඇޮ཰ͳ࣮૷͔Β࢝ΊΔ • ϑΝΠϧ͔Β 1

    ߦͣͭจࣈྻͱͯ͠ಡΈࠐΉ • Data-binding Ͱ JSON → POJO ʹσγϦΞϥ Πζ͢Δ • ͜ͷॲཧͷ౎౓ɺObjectMapper Λੜ੒͢Δ
  10. 1. ObjectMapper Λ࠶ར༻͢Δ • POJO from JSON ͷ data-binding Λ͢Δͷʹ

    ຖճੜ੒͍ͯͨ͠ ObjectMapper Λ࢖͍·Θ͢ • ObjectMapper ͷΦϒδΣΫτੜ੒͸ಛʹॏ ͍ॲཧ • ObjectMapper ͸εϨουηʔϑ ☺
  11. 3. ObjectReader
 #readValues(InputStream) • 1 ߦͣͭจࣈྻͱͯ͠ಡΈࠐΉͷΛ΍Ίɺ InputStream ͷ··Ͱ Jackson ʹ༩͑Δ

    • Jackson ͸ byte[] ΍ InputSteam ͳͲͷ
 ೖྗιʔεʹରͯ͠࠷దԽ͍ͯ͠Δ • String ͷೖྗιʔεʹ͸ͦΜͳʹ࠷దԽ͍ͯ͠ ΔΘ͚Ͱ͸ͳ͍
  12. 4. Afterburner ϞδϡʔϧΛ࢖͏ • POJO to and from JSON ͷ

    data-binding Λߴ଎Խ͢ ΔϞδϡʔϧ • POJO ͷੜ੒΍ getter / setter ͷݺͼग़͠Λɺ
 ϦϑϨΫγϣϯͳ͠ʹόΠτίʔυੜ੒ͨ͠΋ͷͰ ࣮ݱ͢Δ • primitive ͳ஋ͷ boxing ΋ආ͚Δ͜ͱ͕Ͱ͖Δ • طଘͷίʔυ͸΄ͱΜͲมߋෆཁͰಋೖָ͕
  13. 5. Streaming API Λར༻͢Δ • POJO to and from JSON

    ͸ɺͲ͏଍ૡ͍ͯ΋ॏ͍ ॲཧͰ͋Δ͜ͱʹ͸มΘΒͳ͍ • POJO ͷΦϒδΣΫτ͸ੜ੒͠ͳ͘ͱ΋ɺ Streaming API Λ࢖͑͹ JSON ͸ύʔεͰ͖Δ • Streaming API Ͱ໨తͷॲཧΛ࣮૷͢Δͷ͸େม ͕ͩɺ଎౓໘ʹ͓͍ͯ͸Ұ൪ޮՌత❗
  14. Conclusion • Jackson Ͱ POJO to and from JSON ͷγϦΞϥΠζɾσγϦΞ

    ϥΠζΛߴ଎ʹॲཧ͍ͨ͠ͷͰ͋Ε͹: • Jackson ͷΦϒδΣΫτ (ObjectMapper, ObjectReader ͳͲ) ͸ੵۃతʹ࠶ར༻͠Α͏ • ͳΔ΂͘ InputStream ΦϒδΣΫτΛ Jackson ʹ༩͑ΔΑ͏ ʹ͠Α͏ • Afterburner ΋࢖͏͜ͱΛݕ౼͠Α͏ • ΋ͬͱ଎౓ΛՔ͍͗ͨͷͰ͋Ε͹ɺStreaming API Λར༻͠Α͏