127b27ec6Sopenharmony_ci# LZ4 Streaming API Basics
227b27ec6Sopenharmony_ciby *Takayuki Matsuoka*
327b27ec6Sopenharmony_ci## LZ4 API sets
427b27ec6Sopenharmony_ci
527b27ec6Sopenharmony_ciLZ4 has the following API sets :
627b27ec6Sopenharmony_ci
727b27ec6Sopenharmony_ci - "Auto Framing" API (lz4frame.h) :
827b27ec6Sopenharmony_ci   This is most recommended API for usual application.
927b27ec6Sopenharmony_ci   It guarantees interoperability with other LZ4 framing format compliant tools/libraries
1027b27ec6Sopenharmony_ci   such as LZ4 command line utility, node-lz4, etc.
1127b27ec6Sopenharmony_ci - "Block" API : This is recommended for simple purpose.
1227b27ec6Sopenharmony_ci   It compresses single raw memory block to LZ4 memory block and vice versa.
1327b27ec6Sopenharmony_ci - "Streaming" API : This is designed for complex things.
1427b27ec6Sopenharmony_ci   For example, compress huge stream data in restricted memory environment.
1527b27ec6Sopenharmony_ci
1627b27ec6Sopenharmony_ciBasically, you should use "Auto Framing" API.
1727b27ec6Sopenharmony_ciBut if you want to write advanced application, it's time to use Block or Streaming APIs.
1827b27ec6Sopenharmony_ci
1927b27ec6Sopenharmony_ci
2027b27ec6Sopenharmony_ci## What is difference between Block and Streaming API ?
2127b27ec6Sopenharmony_ci
2227b27ec6Sopenharmony_ciBlock API (de)compresses a single contiguous memory block.
2327b27ec6Sopenharmony_ciIn other words, LZ4 library finds redundancy from a single contiguous memory block.
2427b27ec6Sopenharmony_ciStreaming API does same thing but (de)compresses multiple adjacent contiguous memory blocks.
2527b27ec6Sopenharmony_ciSo Streaming API could find more redundancy than Block API.
2627b27ec6Sopenharmony_ci
2727b27ec6Sopenharmony_ciThe following figure shows difference between API and block sizes.
2827b27ec6Sopenharmony_ciIn these figures, the original data is split into 4KiBytes contiguous chunks.
2927b27ec6Sopenharmony_ci
3027b27ec6Sopenharmony_ci```
3127b27ec6Sopenharmony_ciOriginal Data
3227b27ec6Sopenharmony_ci    +---------------+---------------+----+----+----+
3327b27ec6Sopenharmony_ci    | 4KiB Chunk A  | 4KiB Chunk B  | C  | D  |... |
3427b27ec6Sopenharmony_ci    +---------------+---------------+----+----+----+
3527b27ec6Sopenharmony_ci
3627b27ec6Sopenharmony_ciExample (1) : Block API, 4KiB Block
3727b27ec6Sopenharmony_ci    +---------------+---------------+----+----+----+
3827b27ec6Sopenharmony_ci    | 4KiB Chunk A  | 4KiB Chunk B  | C  | D  |... |
3927b27ec6Sopenharmony_ci    +---------------+---------------+----+----+----+
4027b27ec6Sopenharmony_ci    | Block #1      | Block #2      | #3 | #4 |... |
4127b27ec6Sopenharmony_ci    +---------------+---------------+----+----+----+
4227b27ec6Sopenharmony_ci
4327b27ec6Sopenharmony_ci                    (No Dependency)
4427b27ec6Sopenharmony_ci
4527b27ec6Sopenharmony_ci
4627b27ec6Sopenharmony_ciExample (2) : Block API, 8KiB Block
4727b27ec6Sopenharmony_ci    +---------------+---------------+----+----+----+
4827b27ec6Sopenharmony_ci    | 4KiB Chunk A  | 4KiB Chunk B  | C  | D  |... |
4927b27ec6Sopenharmony_ci    +---------------+---------------+----+----+----+
5027b27ec6Sopenharmony_ci    |            Block #1           |Block #2 |... |
5127b27ec6Sopenharmony_ci    +--------------------+----------+-------+-+----+
5227b27ec6Sopenharmony_ci          ^              |             ^    |
5327b27ec6Sopenharmony_ci          |              |             |    |
5427b27ec6Sopenharmony_ci          +--------------+             +----+
5527b27ec6Sopenharmony_ci          Internal Dependency          Internal Dependency
5627b27ec6Sopenharmony_ci
5727b27ec6Sopenharmony_ci
5827b27ec6Sopenharmony_ciExample (3) : Streaming API, 4KiB Block
5927b27ec6Sopenharmony_ci    +---------------+---------------+-----+----+----+
6027b27ec6Sopenharmony_ci    | 4KiB Chunk A  | 4KiB Chunk B  | C   | D  |... |
6127b27ec6Sopenharmony_ci    +---------------+---------------+-----+----+----+
6227b27ec6Sopenharmony_ci    | Block #1      | Block #2      | #3  | #4 |... |
6327b27ec6Sopenharmony_ci    +---------------+----+----------+-+---+-+--+----+
6427b27ec6Sopenharmony_ci          ^              |   ^        | ^   |
6527b27ec6Sopenharmony_ci          |              |   |        | |   |
6627b27ec6Sopenharmony_ci          +--------------+   +--------+ +---+
6727b27ec6Sopenharmony_ci          Dependency         Dependency Dependency
6827b27ec6Sopenharmony_ci```
6927b27ec6Sopenharmony_ci
7027b27ec6Sopenharmony_ci - In example (1), there is no dependency.
7127b27ec6Sopenharmony_ci   All blocks are compressed independently.
7227b27ec6Sopenharmony_ci - In example (2), naturally 8KiBytes block has internal dependency.
7327b27ec6Sopenharmony_ci   But still block #1 and #2 are compressed independently.
7427b27ec6Sopenharmony_ci - In example (3), block #2 has dependency to #1,
7527b27ec6Sopenharmony_ci   also #3 has dependency to #2 and #1, #4 has #3, #2 and #1, and so on.
7627b27ec6Sopenharmony_ci
7727b27ec6Sopenharmony_ciHere, we can observe difference between example (2) and (3).
7827b27ec6Sopenharmony_ciIn (2), there's no dependency between chunk B and C, but (3) has dependency between B and C.
7927b27ec6Sopenharmony_ciThis dependency improves compression ratio.
8027b27ec6Sopenharmony_ci
8127b27ec6Sopenharmony_ci
8227b27ec6Sopenharmony_ci## Restriction of Streaming API
8327b27ec6Sopenharmony_ci
8427b27ec6Sopenharmony_ciFor efficiency, Streaming API doesn't keep a mirror copy of dependent (de)compressed memory.
8527b27ec6Sopenharmony_ciThis means users should keep these dependent (de)compressed memory explicitly.
8627b27ec6Sopenharmony_ciUsually, "Dependent memory" is previous adjacent contiguous memory up to 64KiBytes.
8727b27ec6Sopenharmony_ciLZ4 will not access further memories.
88