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

淺談 Gzip

Sponsored · Your Podcast. Everywhere. Effortlessly. Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
Avatar for chph chph
June 16, 2017

淺談 Gzip

Avatar for chph

chph

June 16, 2017
Tweet

More Decks by chph

Other Decks in Technology

Transcript

  1. Agenda • 動機 • Gzip 簡介 • 資料壓縮概念念 • 圖解

    LZ 演算法 (GZIP 之基礎) • Web 領域的資料壓縮實踐 • Gzip 實驗:HTML 屬性順序與 JSON 形式
  2. Gzip since 1992 • 檔案壓縮程式的簡稱 • gzip 檔案格式 (副檔名 .gz)

    • 基於 DEFLATE 演算法 • LZ77 + 霍夫曼編碼 (Huffman coding) • 無損資料壓縮
  3. Around The World 歌詞實測 • Around the world, around the

    world 
 Around the world, around the world 
 Around the world, around the world 
 Around the world, around the world • 空⾏行行 • 以上重複 18 組 = 2557 Bytes (2.5 KB) • gzip --stdout input.txt > output.txt.gz • gzip 壓縮後⼤大⼩小: 101 Bytes • 壓縮率 = 1 - (101/2557) = 96.05%
  4. Browser/Server 壓縮⽅方案協議
 (RFC 2616) • ⼀一、瀏覽器發送 Request 時,夾帶 Header 告訴伺

    服器可解讀的編碼種類 • Accept-Encoding:gzip, deflate, sdch • sdch - 由 Google 提出,僅限於 Chrome 瀏覽 Google 網站 時使⽤用 • br (Brotli) - 2015 提出的壓縮演算法,壓縮率比 Gzip/deflate 優 ~20%。
 Firefox 44+ & Chrome 49+ 開始⽀支援
  5. Browser/Server 壓縮⽅方案協議
 (RFC 2616) • ⼆二、伺服器回應 Request 時告訴瀏覽器傳輸的 編碼種類 •

    Content-Encoding: gzip • 若若伺服器未啟⽤用/未安裝壓縮模組,則不壓 縮,不回傳此 Header
  6. 使⽤用 Gzip 壓縮⽂文字資源 • Gzip 對⽂文字資料壓縮率最佳 • HTML/CSS/JS
 JSON (串串接

    API 請檢查有無遺漏) • 所有現代瀏覽器皆⽀支援 (IE6 會有 bug) • 若若使⽤用 CDN 要留留意有無啟⽤用設定
  7. HTML 屬性順序不固定
 100 組 <input> <input id="f1" class="field" name="f1" type="text">

    <input class="field" id="f2" type="text" name="f2"> <input id="f3" class="field" name="f3" type="text"> <input name="f4" type="text" class="field" id="f4"> <input type="text" id="f5" class="field" name=“f5"> … <input id="f102" class="field" name="f102" type="text"> PSJHJO#ZUFT ,# ([JQ#ZUFT ,# ᔒ༗၏NJOJGZ
  8. HTML 屬性順序固定 100 組 <input> <input class="field" id="f1" type="text" name="f1">

    <input class="field" id="f2" type="text" name="f2"> <input class="field" id="f3" type="text" name="f3"> <input class="field" id="f4" type="text" name="f4"> <input class="field" id="f5" type="text" name="f5"> … <input class="field" id="f102" type="text" name="f102"> PSJHJO#ZUFT ,# ([JQ#ZUFT ,# ᔒ༗၏NJOJGZ
  9. API 通常吐出的 JSON 形式 [ { "name": "ad6cc49dbb", "user_id": "59434f8dc0250",

    "country": "UK" }, { "name": "2d9ea3f974", "user_id": "59434f8dc04a5", "country": "USA" }, ...... ] 3PXCBTFE
  10. API 通常吐出的 JSON 形式 [ { "name": "ad6cc49dbb", "user_id": "59434f8dc0250",

    "country": "UK" }, { "name": "2d9ea3f974", "user_id": "59434f8dc04a5", "country": "USA" }, ...... ] { "name": [ "ad6cc49dbb", "2d9ea3f974" ], "user_id": [ "59434f8dc0250", "59434f8dc04a5" ], "country": [ "UK", "USA" ] } 3PXCBTFE $PMVNOCBTFE
  11. API 通常吐出的 JSON 形式 100 筆列列導向資料 原始⼤大⼩小: 10409 Bytes (~10

    KB) Minify: 6409 Bytes (6.3 KB) Minify+Gzip: 1221 Bytes (1.2 KB) 3PXCBTFE
  12. API 通常吐出的 JSON 形式 100 筆欄欄導向資料 原始⼤大⼩小: 6278 Bytes (6.1

    KB) Minify: 3524 Bytes (3.4 KB) Minify+Gzip: 1164 Bytes (1.1 KB) $PMVNOCBTFE
  13. API 通常吐出的 JSON 形式 1000 筆列列導向資料 原始⼤大⼩小: 104065 Bytes (~102

    KB) Minify: 63891 Bytes (~62 KB) Minify+Gzip: 11475 Bytes (~11 KB) 3PXCBTFE
  14. API 通常吐出的 JSON 形式 1000 筆欄欄導向資料 原始⼤大⼩小: 62108 Bytes (~61

    KB) Minify: 34989 Bytes (~34 KB) Minify+Gzip: 10754 Bytes (~11 KB) $PMVNOCBTFE
  15. References  1. 資料壓縮, http://www.csie.ntnu.edu.tw/~violet/cs92/ datacompression.htm 2. GZIP is not

    enough! | Google, https://www.youtube.com/ watch?v=whGwm0Lky2s 3. How GZIP compression works | JSConf EU 2014, https:// www.youtube.com/watch?v=wLx5OGxOYUc 4. File Compression (Zipping): How does it work?, https:// www.youtube.com/watch?v=kHkUR6sI_xw 5. https://en.wikipedia.org/wiki/Gzip