kusano 7d535a
# LZ4 Streaming API Basics
kusano 7d535a
by *Takayuki Matsuoka*
kusano 7d535a
## LZ4 API sets
kusano 7d535a
kusano 7d535a
LZ4 has the following API sets :
kusano 7d535a
kusano 7d535a
 - "Auto Framing" API (lz4frame.h) :
kusano 7d535a
   This is most recommended API for usual application.
kusano 7d535a
   It guarantees interoperability with other LZ4 framing format compliant tools/libraries
kusano 7d535a
   such as LZ4 command line utility, node-lz4, etc.
kusano 7d535a
 - "Block" API : This is recommended for simple purpose.
kusano 7d535a
   It compress single raw memory block to LZ4 memory block and vice versa.
kusano 7d535a
 - "Streaming" API : This is designed for complex thing.
kusano 7d535a
   For example, compress huge stream data in restricted memory environment.
kusano 7d535a
kusano 7d535a
Basically, you should use "Auto Framing" API.
kusano 7d535a
But if you want to write advanced application, it's time to use Block or Streaming APIs.
kusano 7d535a
kusano 7d535a
kusano 7d535a
## What is difference between Block and Streaming API ?
kusano 7d535a
kusano 7d535a
Block API (de)compresses single contiguous memory block.
kusano 7d535a
In other words, LZ4 library find redundancy from single contiguous memory block.
kusano 7d535a
Streaming API does same thing but (de)compress multiple adjacent contiguous memory block.
kusano 7d535a
So LZ4 library could find more redundancy than Block API.
kusano 7d535a
kusano 7d535a
The following figure shows difference between API and block sizes.
kusano 7d535a
In these figures, original data is splitted to 4KiBytes contiguous chunks.
kusano 7d535a
kusano 7d535a
```
kusano 7d535a
Original Data
kusano 7d535a
    +---------------+---------------+----+----+----+
kusano 7d535a
    | 4KiB Chunk A  | 4KiB Chunk B  | C  | D  |... |
kusano 7d535a
    +---------------+---------------+----+----+----+
kusano 7d535a
kusano 7d535a
Example (1) : Block API, 4KiB Block
kusano 7d535a
    +---------------+---------------+----+----+----+
kusano 7d535a
    | 4KiB Chunk A  | 4KiB Chunk B  | C  | D  |... |
kusano 7d535a
    +---------------+---------------+----+----+----+
kusano 7d535a
    | Block #1      | Block #2      | #3 | #4 |... |
kusano 7d535a
    +---------------+---------------+----+----+----+
kusano 7d535a
    
kusano 7d535a
                    (No Dependency)
kusano 7d535a
kusano 7d535a
kusano 7d535a
Example (2) : Block API, 8KiB Block
kusano 7d535a
    +---------------+---------------+----+----+----+
kusano 7d535a
    | 4KiB Chunk A  | 4KiB Chunk B  | C  | D  |... |
kusano 7d535a
    +---------------+---------------+----+----+----+
kusano 7d535a
    |            Block #1           |Block #2 |... |
kusano 7d535a
    +--------------------+----------+-------+-+----+
kusano 7d535a
          ^              |             ^    |
kusano 7d535a
          |              |             |    |
kusano 7d535a
          +--------------+             +----+
kusano 7d535a
          Internal Dependency          Internal Dependency
kusano 7d535a
kusano 7d535a
kusano 7d535a
Example (3) : Streaming API, 4KiB Block
kusano 7d535a
    +---------------+---------------+-----+----+----+
kusano 7d535a
    | 4KiB Chunk A  | 4KiB Chunk B  | C   | D  |... |
kusano 7d535a
    +---------------+---------------+-----+----+----+
kusano 7d535a
    | Block #1      | Block #2      | #3  | #4 |... |
kusano 7d535a
    +---------------+----+----------+-+---+-+--+----+
kusano 7d535a
          ^              |   ^        | ^   |
kusano 7d535a
          |              |   |        | |   |
kusano 7d535a
          +--------------+   +--------+ +---+
kusano 7d535a
          Dependency         Dependency Dependency
kusano 7d535a
```
kusano 7d535a
kusano 7d535a
 - In example (1), there is no dependency.
kusano 7d535a
   All blocks are compressed independently.
kusano 7d535a
 - In example (2), naturally 8KiBytes block has internal dependency.
kusano 7d535a
   But still block #1 and #2 are compressed independently.
kusano 7d535a
 - In example (3), block #2 has dependency to #1,
kusano 7d535a
   also #3 has dependency to #2 and #1, #4 has #3, #2 and #1, and so on.
kusano 7d535a
kusano 7d535a
Here, we can observe difference between example (2) and (3).
kusano 7d535a
In (2), there's no dependency between chunk B and C, but (3) has dependency between B and C.
kusano 7d535a
This dependency improves compression ratio.
kusano 7d535a
kusano 7d535a
kusano 7d535a
## Restriction of Streaming API
kusano 7d535a
kusano 7d535a
For the efficiency, Streaming API doesn't keep mirror copy of dependent (de)compressed memory.
kusano 7d535a
This means users should keep these dependent (de)compressed memory explicitly.
kusano 7d535a
Usually, "Dependent memory" is previous adjacent contiguous memory up to 64KiBytes.
kusano 7d535a
LZ4 will not access further memories.