Skip to content

Latest commit

ย 

History

History
230 lines (159 loc) ยท 6.1 KB

OptimizingStorageInYourApp.md

File metadata and controls

230 lines (159 loc) ยท 6.1 KB

@ WWDC 19

Better performance and efficiency

์Šคํ† ๋ฆฌ์ง€ ์ตœ์ ํ™”๋ฅผ ํ†ตํ•ด ์–ป์„ ์ˆ˜ ์žˆ๋Š” ์ด์ ์€?

  • ๊ธด ๋ฐฐํ„ฐ๋ฆฌ ์ˆ˜๋ช…
  • ๋” ์ข‹์€ ์„ฑ๋Šฅ
  • ๋””์Šคํฌ footprint ๊ฐ์†Œ
  • ๋” ์ข‹์€ ๋””๋ฐ”์ด์Šค ์ƒํƒœ

์˜ค๋Š˜ ๋งํ•  ์ˆœ์„œ๋Š” ์ด๋ ‡๋‹ค!

  • Efficient image assets
  • File system metadata
  • Syncing to disk
  • Serialized data files
  • Core Data
  • SQLite

Efficent image assets ํšจ์œจ์ ์ธ ์ด๋ฏธ์ง€ ์—์…‹

์ด๋ฏธ์ง€ ์—์…‹์€ ํ™”๋ฉด ์‚ฌ์ด์ฆˆ์— ๋งž๊ฒŒ ํฌ๊ธฐ๊ฐ€ ์กฐ์ •๋œ๋‹ค.

HEIC๊ณผ Asset Catalog๋ฅผ ์ด์šฉํ•ด์„œ footprint๋ฅผ ์ค„์—ฌ๋ผ!

HEIC

  • JPEG๋ฅผ ๋Œ€์ฒดํ•  ์ˆ˜ ์žˆ๋Š” ๋” ํšจ์œจ์ ์ธ ํฌ๋งท
  • ๋™์ผ ํ€„๋ฆฌํ‹ฐ ๋Œ€๋น„ ~50% ๋” ์ž‘์€ ํŒŒ์ผ ํฌ๊ธฐ
  • ๋” ์ž‘์€ ๋””์Šคํฌ footprint
  • ๋” ๋น ๋ฅธ ๋„คํŠธ์›Œํฌ ๋‹ค์šด๋กœ๋“œ, ์—…๋กœ๋“œ
  • ๋” ๋น ๋ฅธ ๋””์Šคํฌ ๋กœ๋“œ, ์„ธ์ด๋ธŒ
  • auxiliary image๋ฅผ ์ €์žฅํ•œ๋‹ค. (depth, disparity ๋“ฑ์˜ ์ •๋ณด ์ €์žฅ)
  • alpha๋ฅผ ์ง€์›ํ•จ
  • ๋ฌด์†์‹ค ์••์ถ•
  • ํ•˜๋‚˜์˜ ์ปจํ…Œ์ด๋„ˆ์— ์—ฌ๋Ÿฌ ๊ฐœ์˜ ์ด๋ฏธ์ง€
  • iOS 11, macOS High Sierra๋ถ€ํ„ฐ ์ง€์›๋˜๋Š” ํฌ๋งท

Asset Catalog

  • ๊ฐ„๋‹จํ•˜๊ฒŒ ์•ฑ์˜ ๋ฆฌ์†Œ์Šค๋ฅผ ๊ด€๋ฆฌ
  • ์•ฑ ์•„์ด์ฝ˜
  • ๋‹ค์–‘ํ•œ ๋””๋ฐ”์ด์Šค, ์‚ฌ์ด์ฆˆ์— ์ ์šฉ๋˜๋Š” ์ด๋ฏธ์ง€
  • On demand resources ์‹ค์ œ ์‚ฌ์šฉํ•˜๊ณ ์ž ํ•  ๋•Œ ์‚ฌ์šฉ๋จ
  • Storage efficiency
    • On-disk foot pprint
    • App slicing (iOS) - ์•ฑ์Šคํ† ์–ด์—์„œ ๋‹ค์šด ๋ฐ›์„ ๋•Œ ๋‹ค์šด ๋ฐ›๋Š” ๊ธฐ๊ธฐ์— ํ•„์š”ํ•œ ์—์…‹๋งŒ ๋‹ค์šด๋กœ๋“œ ๋ฐ›๊ธฐ ๋•Œ๋ฌธ์— ๋” ํšจ์œจ์ ์ž„
  • Performance
    • ์ด๋ฏธ์ง€๋ฅผ ๋” ๋นจ๋ฆฌ ๋กœ๋”ฉํ•จ
    • ์•ฑ ๋Ÿฐ์น˜ ์‹œ์ ์— ๋” ํผํฌ๋จผ์Šค๊ฐ€ ์ข‹๋‹ค (up to 10% faster!)
    • GPU ๋ฒ ์ด์Šค๋ผ ๊ธฐ๋ณธ์ ์œผ๋กœ ๋ฌด์†์‹ค ์••์ถ•์„ ์ง€์›ํ•œ๋‹ค.
    • ์†์‹ค๋˜๊ฒŒ ์••์ถ•๋„ ๊ฐ€๋Šฅ
      • ์••์ถ•ํ’€ ๋•Œ ๋” ๋น ๋ฆ„
      • ๋‚ฎ์€ ๋ฉ”๋ชจ๋ฆฌ footprint

File System Metadata

  • ํŒŒ์ผ ์‹œ์Šคํ…œ์˜ ๋ฉ”ํƒ€๋ฐ์ดํ„ฐ๋ฅผ ์—…๋ฐ์ดํŠธํ•˜๋Š” ๊ฒƒ์€ ๋น„์šฉ์ด ๋“ ๋‹ค.

  • APFS๋ฅผ ์ฒดํฌํ•ด๋ณด์ž (Apple File System)

  • Efficient non-persistent file

    • ํŒŒ์ผ ์ƒ์„ฑ
    • ์˜คํ”ˆ๋œ ์ƒํƒœ๋กœ ๋‘๊ณ  ์—ฐ๊ฒฐ์€ ํ•˜์ง€ ์•Š๋Š”๋‹ค (Keep it open and unlinked)
    • fsync๋ฅผ ํ˜ธ์ถœํ•˜์ง€ ๋งˆ๋ผ

Syncing to Disk

User Space - App

Kernel - OS Cache (Logical I/O)

Hardware - Disk Cache, Permanent Storage (Physical I/O)

  • fsync()
    • Flushes cached data to disk
      • OS cache์—์„œ disk cache๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์˜ฎ๊ธด๋‹ค.
      • ๋ฐ์ดํ„ฐ๊ฐ€ permanent storage๋กœ ๋ฐ”๋กœ write ๋˜์ง€ ์•Š์„ ์ˆ˜ ์žˆ๋‹ค.
      • ์ˆœ์„œ๋ฅผ ๋ณด์žฅํ•˜์ง€ ์•Š๋Š”๋‹ค.
      • ๋น„์šฉ์ด ๋งŽ์ด ๋“ ๋‹ค.
      • OS์— ์˜ํ•ด ์ฃผ๊ธฐ์ ์œผ๋กœ ์‹คํ–‰๋œ๋‹ค.
  • F_FULLFSYNC
    • cache๋ฅผ disk๋กœ ์˜ฎ๊ธด๋‹ค.
      • disk cache์— ์žˆ๋Š” ๋ชจ๋“  ๋ฐ์ดํ„ฐ๋ฅผ ์˜ฎ๊ธด๋‹ค.
      • ๋น„์šฉ์ด ๋งŽ์ด ๋“ ๋‹ค.
      • OS์— ์˜ํ•ด ์ฃผ๊ธฐ์ ์œผ๋กœ ์‹คํ–‰๋œ๋‹ค.
  • F_BARRIERFSYNC
    • I/O ordering์„ ๋งŒ๋“ ๋‹ค.
      • fsync() with a barrier
    • F_FULLFSYNC๋ณด๋‹ค ํ›จ์”ฌ ๋น„์šฉ์ด ์ ๊ฒŒ ๋“ ๋‹ค.

์ˆœ์„œ๋ฅผ ๋ณด์žฅํ•˜๊ธฐ ์œ„ํ•ด์„œ F_BARRIERFSYNC๋ฅผ ์‚ฌ์šฉํ•ด๋ผ!

Seralized Data Files

์˜ˆ๋ฅผ ๋“ค๋ฉด, Plists, XML, JSON

  • ์žฅ์ 
    • ํŽธ๋ฆฌํ•˜๋‹ค
    • ์ž์ฃผ ์ˆ˜์ •๋˜๋Š” ๋ฐ์ดํ„ฐ๊ฐ€ ์•„๋‹ ๋•Œ ํ›Œ๋ฅญํ•˜๋‹ค
    • ๋ถ„์„ํ•˜๊ธฐ ์‰ฝ๋‹ค.
  • ๋‹จ์ 
    • ๋ชจ๋“  ๋ณ€๊ฒฝ์ ๋งˆ๋‹ค ํŒŒ์ผ ์ „์ฒด๊ฐ€ ์ƒˆ๋กœ ์ž‘์„ฑ๋œ๋‹ค.
    • ํ™•์žฅ์„ฑ์ด ์ข‹์ง€ ์•Š๋‹ค.
    • ์ž˜๋ชป ์‚ฌ์šฉํ•˜๊ธฐ ์‰ฝ๋‹ค.
    • ๋ฉ”ํƒ€๋ฐ์ดํ„ฐ์˜ ์ง‘์•ฝ์ฒด์ด๋‹ค.
  • ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋กœ์˜ ์˜๋ฏธ๊ฐ€ ์—†๋‹ค.

Core Data

  • Core Data management

    • SQLite ๊ธฐ๋ฐ˜
    • Object graph์™€ relationship์„ ๊ด€๋ฆฌํ•œ๋‹ค.
    • ๋ณ€ํ™”๋ฅผ ๊ฐ์ง€(tracking)ํ•˜๊ณ  ์•Œ๋ฆฐ๋‹ค(notify).
    • ์ž๋™์ ์œผ๋กœ ๋ฒ„์ „ ํŠธ๋ž˜ํ‚น์„ ํ•˜๊ณ  ์—ฌ๋Ÿฌ ์ž‘์„ฑ์ž๊ฐ€ ์žˆ์Œ์œผ๋กœ์จ ์ƒ๊ธฐ๋Š” ์ถฉ๋Œ์„ ํ•ด๊ฒฐํ•œ๋‹ค.
      • Automatic connection pooling
    • CloudKit๊ณผ์˜ ํ†ตํ•ฉ (iOS 13๋ถ€ํ„ฐ)
    • ์‹ค์‹œ๊ฐ„ ์ฟผ๋ฆฌ
    • Automatic memory management
    • Statement aggregation in transactions
    • Schema migrations
    • Denomalization (iOS 13๋ถ€ํ„ฐ)
    • ๊ทธ ์™ธ ๊ธฐํƒ€ ๋“ฑ๋“ฑ

SQLite

Journaling์€ ํŠธ๋žœ์žญ์…˜๊ณผ ๊ด€๋ จ์ด ์žˆ๋‹ค.

  • Connection์„ ์—ด๊ณ  ๋‹ซ๋Š” ๊ฒƒ์€ ๋น„์šฉ์ด ๋งŽ์ด ๋“œ๋Š” ์ž‘์—…์„ ์œ ๋ฐœํ•  ์ˆ˜ ์žˆ๋‹ค!
    • Consistency checking
    • Journal recovery
    • Journal checkpointing
  • Recommended usage model
    • Connection ์ƒํƒœ๋ฅผ open์ธ ์ฑ„๋กœ ๊ฐ€๋Šฅํ•œ ์˜ค๋ž˜ ์œ ์ง€ํ•ด๋ผ
    • ํ•„์š”ํ•  ๋•Œ๋งŒ connection์„ closeํ•ด๋ผ
    • Pool connections on multi-threaded processes
  • WAL Mode Journaling Reduces Writes (๋Œ€๋ถ€๋ถ„์˜ ๊ฒฝ์šฐ WAL mode ์‚ฌ์šฉ์ด ํšจ์œจ์ ์ž„)
    • Write ahead logging
    • Combines multiple writes to the same page
    • Uses fewer barriers
    • Supports multiple readers at the same time as a writer
    • Suports snapshots

Transactions

  • Transactions Help Reduce Writes

    • Multiple INSERT, UPDATE, DELETE statement์— ์“ฐ์ž„

    • Multiple statement์— ์˜ํ•œ ํŽ˜์ด์ง€ ๋ณ€๊ฒฝ์€ ํ•œ ๋ฒˆ๋งŒ write ๋œ๋‹ค.

    • Useful for aggregating changes over time!

File Size and Privacy

PRAGMA schema.secure_delete=FAST;
  • How do we securely delete sensitive data?
    • Automatically zeros deleted data
    • No cost for data within the same page as the header
    • Default behavior starting with iOS 13
  • Don't use VACUUM
    • VACUUM is a slow I/O intensive operation
    • ์ €๋„ ํŒŒ์ผ์— ์ผ๋‹ซ๊ฐ€ ๋‹ค์‹œ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ์“ฐ๋Š” ๊ณผ์ •์€ cost๊ฐ€ ๋น„์‹ธ๋‹ค.
    • All valid data gets written at least twice!
PRAGMA schema.auto_vacuum=INCREMENTAL;
PRAGMA schema.incremental_vacuum(N);
  • Incremental auto vacuum๊ณผ fast secure delete๋ฅผ ์ด์šฉํ•ด์„œ file size์™€ privacy๋ฅผ ๊ด€๋ฆฌํ•ด๋ผ.

Partial Indexes

  • Index
    • Faster ORDER BY, GROUP BY and WHERE clauses
    • Each index adds additional I/O when writing to the database
  • Partial Indexes
    • Indexes with a WHERE clause
    • Partial indexes only store data which matches the WHERE clause
    • Queries that do not match the WHERE clause cannot use the index

File Activity Instrument

  • Supports all Apple devices
  • Support for tracing single process and all processes
  • Obtain both logical and physical I/O information
  • Offers automated reasoning

Automated Reasoning

  • Excessive physical writes
  • Failed I/O related calls
  • Suboptimal caching

Summary

  • Apply these lessons
  • Profile with File Activity Instrument
  • Continue optimizing storage!