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

差分計算アルゴリズムを用いた高速なUITableView描画

fumito-ito
August 31, 2018

 差分計算アルゴリズムを用いた高速なUITableView描画

UITableViewをいかに高速に描画、更新するかはiOSアプリケーションの実装において頻繁に遭遇する問題です。

本セッションでは単純な reloadData を用いた場合と差分計算アルゴリズムを用いた場合で実際にどの程度の差が生じるのか検証した結果を提示します。
また、複数の差分計算アルゴリズムとその実装を比較することで、それぞれの実装が適しているケースを提示します。

fumito-ito

August 31, 2018
Tweet

More Decks by fumito-ito

Other Decks in Technology

Transcript

  1. 1
    ࠩ෼ܭࢉΞϧΰϦζϜΛ༻͍
    ͨߴ଎ͳ UITableView ඳը

    View full-size slide

  2. 2
    ύϑΥʔϚϯε
    ؾʹͯ͠·͔͢ʁ

    View full-size slide

  3. 3
    “Most apps target a frame rate of 60 FPS,
    Equivalent to 16.67 ms per frame.”
    https://developer.apple.com/library/archive/documentation/3DDrawing/Conceptual/
    MTLBestPracticesGuide/FrameRate.html

    View full-size slide

  4. 5
    UITableView͕େྔʹฒͿ

    View full-size slide

  5. 6
    UITableViewΛ
    ଎͍ͨ͘͠

    View full-size slide

  6. 7
    UITableViewΛ଎͘͢Δʹ͸
    • CellͷϨϯμϦϯά࠷దԽ
    • ඇಉظͳσʔλϑΣονͱΩϟογϡ
    • ඇಉظͳը૾ϩʔυͱΩϟογϡ
    • ΦϑϨϯμϦϯάΛආ͚Δ
    • αΠζͷࣄલܭࢉͱΩϟογϡ

    View full-size slide

  7. 8
    ඳը͢Δߦ͕গͳ͚Ε
    ͹଎͍ͷͰ͸…

    View full-size slide

  8. 9
    performBatchUpdates

    View full-size slide

  9. 10
    ࠩ෼ߋ৽Λߦ͏ͨΊʹ͸
    • มߋલͷσʔλΛมߋޙͷσʔλʹม׵͢ΔͨΊͷ

    ฤूૢ࡞Λܭࢉ͠ͳͯ͘͸ͳΒͳ͍
    • มߋલޙͷσʔλͷ௕͞ΛͦΕͧΕM, Nͱͨ͠৔߹

    ૯౰ͨΓͰܭࢉྔ͸O(M * N)ʹͳΔ
    • ฤूૢ࡞Λܭࢉ͢Δํ๏͸͢Ͱʹଟ͘ͷΞϧΰϦζϜ͕

    ߟҊ͞Ε͓ͯΓɺ͜ΕΒΛ༻͍Δ͜ͱͰܭࢉྔΛখ͘͞

    ͢Δ͜ͱ͕Ͱ͖Δ

    View full-size slide

  10. 11
    ͬ͘͟Γฤूૢ࡞ܭࢉ
    ΞϧΰϦζϜ

    View full-size slide

  11. X
    ͬ͘͟Γฤूڑ཭ΞϧΰϦζϜ
    • จࣈྻͷฤूૢ࡞Λάϥϑʹϓ
    ϩοτ͠ɺ࠷খૢ࡞ύεʢฤू
    ڑ཭ʣΛ୳͢
    • ૯ίετΛԾఆͯ͠࠷খίετ
    Λ୳ࡧ͢Δํ๏ʢMyerʣ
    • ࡟আૢ࡞ͷ૯਺ΛԾఆͯ͠࠷খ
    ίετΛ୳ࡧ͢Δํ๏ʢWuʣ
    https://susisu.hatenablog.com/entry/2017/10/09/134032

    View full-size slide

  12. X
    ͬ͘͟ΓHeckel๏
    • ྆ํͷཁૉྻʹݱΕΔڞ௨͔ͭ

    ̍౓͔͠ग़ݱ͠ͳ͍ཁૉΛ୳͢
    • நग़ͨ͠ߦΛى఺ʹڞ௨ϒϩοΫ
    Λ୳ͯࠩ͠෼͔ΒऔΓআ͘
    Paul Heckel (Apr 1978), “A technique for isolating differences between files”

    View full-size slide

  13. 12
    ֤ΞϧΰϦζϜͷܭࢉྔ
    • Myer๏
    • ܭࢉྔɿO(N + D^2) ~ O(N * D)
    • D͸ฤूڑ཭ʢฤूૢ࡞ͷ਺ʣ
    • Wu๏
    • ܭࢉྔɿO(N + P * D) ~ O(N * P)
    • P͸࡟আૢ࡞ͷ਺ɺD͸ฤूڑ཭
    • Heckel๏
    • ܭࢉྔɿO(N)

    View full-size slide

  14. 13
    ฤूૢ࡞ܭࢉΞϧΰϦζϜͷ࣮૷
    • Dwifft
    • EditDistance
    • RxDataSources
    • DifferenceKit

    View full-size slide

  15. 14
    Dwifft
    • https://github.com/jflinter/Dwifft
    • Myer๏ͬΆ͍࣮૷ => O(N * D)
    • ࠓճऔΓ্͛ͨ΋ͷͷதͰ͸ʢཧ࿦తʹ͸ʣҰ൪஗͍

    View full-size slide

  16. 15
    EditDistance
    • https://github.com/kazuhiro4949/EditDistance
    • Wu๏ͬΆ͍࣮૷ => O(N * P)
    • WuͷΞϧΰϦζϜΛ࣮૷͍ͯ͠Δ΋ͷ͸ҙ֎ͱগͳ͍

    View full-size slide

  17. 16
    RxDataSources
    • https://github.com/RxSwiftCommunity/RxDataSources
    • Heckel๏ + α => O(N)
    • IGListKit΋ಉ༷ʹHeckel๏Λ࠾༻͍ͯ͠Δ͕ɺ

    ΑΓ࠷దԽ͞Ε͍ͯΔRxDataSourceΛ࠾༻͢Δ
    • ࠩ෼ܭࢉΞϧΰϦζϜ෦෼͸ Differentiator.framework

    ͱͯ͠෼཭͞Ε͍ͯΔͷͰଞͷϑϨʔϜϫʔΫ͔Β

    ར༻͢Δ͜ͱ͕ՄೳͰ͋Δ

    View full-size slide

  18. 18
    DifferenceKit
    • https://github.com/ra1028/DifferenceKit
    • Heckel๏ + α => O(N)
    • iOSDC1ϲ݄લʹಥ೗ͱͯ͠ݱΕͨ৽੕

    View full-size slide

  19. 20
    ύϑΥʔϚϯεൺֱ

    View full-size slide

  20. 21
    ϨΪϡϨʔγϣϯ
    • Insert 10% + Delete 10%
    • ཁૉ਺: 1,000
    • ཁૉ਺: 10,000
    • Insert 30% + Delete 30%
    • ཁૉ਺: 1,000
    • ཁૉ਺: 10,000

    View full-size slide

  21. 22
    ύϑΥʔϚϯεൺֱ(1,000ߦ)

    View full-size slide

  22. 23
    ύϑΥʔϚϯεൺֱ(10,000ߦ)

    View full-size slide

  23. 24
    ࠩ෼ߋ৽ύϑΥʔϚϯε(1,000ߦ)

    View full-size slide

  24. 25
    ࠩ෼ߋ৽ύϑΥʔϚϯε(100ߦ)

    View full-size slide

  25. 26
    ಈ͔ͤΔαϯϓϧ
    • https://github.com/fumito-ito/iOSDC-2018-Sample

    View full-size slide

  26. 27
    ύϑΥʔϚϯεҎ֎ͷൺֱ؍఺
    • ଟ࣍ݩ഑ྻʹରԠ͍ͯ͠Δ͔
    • ≒ ηΫγϣϯͷαϙʔτ
    • ॏෳͨ͠σʔλΛڐ༰͢Δ͔
    • UITableViewͰදݱՄೳͳૢ࡞Λαϙʔτ͍ͯ͠Δ͔

    View full-size slide

  27. 28
    ϥΠϒϥϦͷػೳൺֱ

    View full-size slide

  28. 29
    ೔ܦిࢠ൛ͷࠩ෼ܭࢉ
    • RxASDataSourceΛ࠾༻ͨ͠
    • RxDataSourcesͷޓ׵ϥΠϒϥϦ
    • ΋ͱ΋ͱར༻͍ͯͨ͠RxSwiftɺTextureͱ૬ੑͷ͍͍

    ϥΠϒϥϦΛ࢖͍͔ͨͬͨ

    View full-size slide

  29. 30
    ೔ܦిࢠ൛Ͱͷվળྫ
    reloadData ࠩ෼ߋ৽

    View full-size slide

  30. 31
    ࠩ෼ߋ৽
    ຊ౰ʹ΍Δ΂͖ʁ

    View full-size slide

  31. 32
    ࠩ෼ߋ৽͸ίετʹݟ߹ͬͯΔʁ
    • ཁૉ਺ʹൺྫͯ͠ܭࢉίετ͕૿େ͢Δ͸ͣ
    • ࣮ࡍʹηϧͷ਺͕େ͖͗͢Δ৔߹͸ඇৗʹ஗͘ͳΔ

    ໰୊͕ࢦఠ͞Ε͍ͯΔ
    • “I have a huge data set and -
    performBatchUpdatesAnimated: completion: is super
    slow. What do I do?”

    - IGListKit, performBatchUpdates long perform on main thread #803

    https://github.com/Instagram/IGListKit/issues/
    803#issuecomment-305276659

    View full-size slide

  32. 33
    ؾʹͳΔํ͸ͪ͜Β
    • ʮ5000ߦͷUITableViewΛࠩ෼ߋ৽͢Δʯ
    • 2018/09/01 14:20 ~ @Track A
    • https://fortee.jp/iosdc-japan-2018/proposal/
    530b6839-cc50-452c-9682-897afa9db18c

    View full-size slide

  33. 34
    ·ͱΊ
    • ࠩ෼ߋ৽Λ༻͍Δ͜ͱͰUITableViewΛߴ଎ʹ

    ඳը͢Δ͜ͱ͕Ͱ͖Δ
    • ࠩ෼ͷܭࢉʹΞϧΰϦζϜΛద༻͢Δ͜ͱͰܭࢉྔΛ

    ݮΒ͠ɺύϑΥʔϚϯεΛ޲্ͤ͞Δ͜ͱ͕Ͱ͖Δ
    • දࣔ͢Δσʔλͷྔ͕େ͖͍৔߹͸ࠩ෼ܭࢉΛ͢Δͷ͔ɺ
    reloadDataΛ͢Δͷ͔ߟ͑ͨํ͕ྑ͍

    View full-size slide

  34. 35
    RxASDataSourceͷ
    ίϯτϦϏϡʔλʔ
    ืूதͰ͢ɻ
    @fumito_ito
    fumito-ito

    View full-size slide

  35. 36
    ग़య
    • Paul Heckel (Apr 1978), “A technique for isolating
    differences between files”
    • James W. Hunt, M. Douglas Mcllory (Jun 1976), “An
    Algorithm for Differential File Comparison”
    • E. Myers (1986), “An O(ND) Difference Algorithm and
    Its Variations”

    View full-size slide