zram: avoid copying spanning LZ4 objects before decompression
Subsystems (2)
cover zram: avoid copying spanning LZ4 objects before decompression
When a compressed object spans two zsmalloc pages, zram copies it into a contiguous buffer before decompression. This series lets the LZ4 and LZ4HC backends decode dictionary-free blocks directly from the two fragments.
I measured the time to swap in 1 GiB of synthetic data on arm64 with 4 KiB pages. Each page started with pseudorandom data of one of 16 lengths, followed by zeros. On this workload, 32.6% of compressed reads spanned two pages with LZ4 and 34.4% with LZ4HC.
The table shows median swap-in times over seven pairs. The last two columns summarize the percentage change within each pair.
Codec Workers Baseline (ms) Patched (ms) Median Range LZ4 1 1403.81 1344.45 -4.21% -4.64% to -1.61% LZ4 8 222.57 216.36 -2.73% -4.19% to -1.97% LZ4HC 1 1403.35 1336.99 -4.74% -5.07% to -2.90% LZ4HC 8 222.10 215.77 -3.16% -4.39% to -1.27%
These timings include both skipping the copy and switching decoders.
I used Codex to help implement this series.
Do you think the swap-in improvement justifies maintaining a second LZ4 decoder?
---
Xueyuan Chen (3):
zram: allow backends to decompress from two input buffers lib/lz4: add safe decompression from two input buffers
zram: use split-input decompression for LZ4 and LZ4HC
drivers/block/zram/backend_lz4.c | 1 + drivers/block/zram/backend_lz4.h | 19 +++++ drivers/block/zram/backend_lz4hc.c | 2 + drivers/block/zram/zcomp.c | 28 +++++++ drivers/block/zram/zcomp.h | 8 ++ include/linux/lz4.h | 18 ++++ lib/lz4/lz4_decompress.c | 163 +++++++++++++++++++++++++++++++++++++ 7 files changed, 239 insertions(+)
---
base-commit: b95f8bb86713c860ce8536d22a988b949b884d5f
Signature · 2 lines
Best regards, Xueyuan
RFC PATCH 1/3 zram: allow backends to decompress from two input buffers
Let backends decompress a spanning zsmalloc object directly from its two scatterlist entries. Keep both fragments mapped for the call.
Use the linear path if the callback is absent or returns -EOPNOTSUPP before writing output. Propagate other errors.
--- drivers/block/zram/zcomp.c | 28 ++++++++++++++++++++++++++++ drivers/block/zram/zcomp.h | 8 ++++++++ 2 files changed, 36 insertions(+)
drivers/block/zram/zcomp.c +28 -0
@@ -159,6 +159,28 @@ int zcomp_compress(struct zcomp *comp, struct zcomp_strm *zstrm,return ret; } +static int zcomp_decompress_split (struct zcomp *comp,struct zcomp_strm *zstrm, +struct scatterlist *sg,void *dst) +{ +struct scatterlist *next =sg_next (sg); +/* Each entry from the zsmalloc read path fits within one page. */ +void *first =kmap_local_page (sg_page (sg)); +void *second =kmap_local_page (sg_page (next)); +struct zcomp_req head = { + .src = first + sg->offset, + .src_len = sg->length, + .dst = dst, + .dst_len =PAGE_SIZE , + }; +int ret; + + ret = comp->ops->decompress_split (comp->params, &zstrm->ctx, &head, + second + next->offset, next->length); +kunmap_local (second); +kunmap_local (first); +return ret; +} +int zcomp_decompress (struct zcomp *comp,struct zcomp_strm *zstrm,struct scatterlist *sg,unsigned int src_len,void *dst) {
@@ -177,6 +199,12 @@ int zcomp_decompress(struct zcomp *comp, struct zcomp_strm *zstrm, src =kmap_local_page (sg_page (sg)); req.src = src + sg->offset; }else { +if (comp->ops->decompress_split) { + ret =zcomp_decompress_split (comp, zstrm, sg, dst); +if (ret != -EOPNOTSUPP ) +return ret; + } +/* the object spans two pages, linearize it into local copy */ sg_copy_to_buffer (sg,2 , zstrm->local_copy, src_len); req.src = zstrm->local_copy;
drivers/block/zram/zcomp.h +8 -0
@@ -61,6 +61,14 @@ struct zcomp_ops { struct zcomp_req *req);int (*decompress)(struct zcomp_params *params,struct zcomp_ctx *ctx,struct zcomp_req *req); +/* +* Decode one block from head->src and tail into head->dst. The input +* mappings are valid during the call. Return 0 for a complete output +* or -EOPNOTSUPP before writing output to request a linear retry. +*/ +int (*decompress_split)(struct zcomp_params *params,struct zcomp_ctx *ctx, +const struct zcomp_req *head, +const void *tail,size_t tail_len);int (*create_ctx)(struct zcomp_params *params,struct zcomp_ctx *ctx);void (*destroy_ctx)(struct zcomp_ctx *ctx);
Signature · 1 line
2.47.3
RFC PATCH 2/3 lib/lz4: add safe decompression from two input buffers
In reply to Xueyuan ChenLZ4_decompress_generic() assumes contiguous input. Its wide literal loads can cross the boundary between independently mapped fragments.
Add a dictionary-free block decoder that reads from two buffers and handles literals, lengths and offsets that cross the split.
--- include/linux/lz4.h | 18 ++++++ lib/lz4/lz4_decompress.c | 163 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 181 insertions(+)
include/linux/lz4.h +18 -0
@@ -292,6 +292,24 @@ int LZ4_decompress_fast(const char *source, char *dest, int originalSize);int LZ4_decompress_safe (const char *source,char *dest,int compressedSize,int maxDecompressedSize); +/** +* LZ4_decompress_safe_split() - Decode a single block from two buffers +* @first: address of the first input fragment +* @first_len: bytes in the first input fragment +* @second: address of the second input fragment +* @second_len: bytes in the second input fragment +* @dst: output buffer, disjoint from the input buffers +* @capacity: bytes available in the output buffer +* +* The fragments, in order, contain one dictionary-free LZ4 block. +* Their addresses need not be adjacent. An empty fragment may be NULL. +* +* Return: the decoded size, or -EINVAL on error. +*/ +int LZ4_decompress_safe_split (const u8 *first,size_t first_len, +const u8 *second,size_t second_len, +u8 *dst,int capacity); +/** * LZ4_decompress_safe_partial() - Decompress a block of size 'compressedSize' * at position 'source' into buffer 'dest'
lib/lz4/lz4_decompress.c +163 -0
175 lines not on this page · raw message
Signature · 1 line
2.47.3
Sergey Senozhatsky
maintainerRe: [PATCH RFC 2/3] lib/lz4: add safe decompression from two input buffers
Let me Cc Herbert and zswap folks on this. Herbert mentioned previously (back in the days when we landed SG-list based zsmalloc object read API) that lz4 can be extended to support spanning zsmalloc objects and, if this lands, then zswap may also be interested in adopting it (along with zram).
Quoted 226 lines · Xueyuan Chen
LZ4_decompress_generic()assumes contiguous input. Its wide literal loads can cross the boundary between independently mapped fragments.Add a dictionary-free block decoder that reads from two buffers and handles literals, lengths and offsets that cross the split.
Signed-off-by Xueyuan Chen <[email protected]>--- include/linux/lz4.h | 18 ++++++ lib/lz4/lz4_decompress.c | 163 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 181 insertions(+)include/linux/lz4.h +18
-0@@ -292,6 +292,24 @@ int LZ4_decompress_fast(const char *source, char *dest, int originalSize);int LZ4_decompress_safe (const char *source,char *dest,int compressedSize,int maxDecompressedSize); +/** +* LZ4_decompress_safe_split() - Decode a single block from two buffers +* @first: address of the first input fragment +* @first_len: bytes in the first input fragment +* @second: address of the second input fragment +* @second_len: bytes in the second input fragment +* @dst: output buffer, disjoint from the input buffers +* @capacity: bytes available in the output buffer +* +* The fragments, in order, contain one dictionary-free LZ4 block. +* Their addresses need not be adjacent. An empty fragment may be NULL. +* +* Return: the decoded size, or -EINVAL on error. +*/ +int LZ4_decompress_safe_split (const u8 *first,size_t first_len, +const u8 *second,size_t second_len, +u8 *dst,int capacity); +/** * LZ4_decompress_safe_partial() - Decompress a block of size 'compressedSize' * at position 'source' into buffer 'dest' lib/lz4/lz4_decompress.c +163
-0@@ -39,6 +39,11 @@#include <linux/kernel.h> #include <linux/unaligned.h> +#ifndef STATIC +#include <linux/errno.h> +#include <linux/limits.h> +#endif +/*-***************************** * Decompression functions *******************************/ @@ -705,6 +710,164 @@ int LZ4_decompress_fast_usingDict(const char *source, char *dest, }#ifndef STATIC +struct lz4_split_input { +const u8 *first; +const u8 *second; +size_t split; +size_t size; +size_t pos; +}; + +static __always_inline size_t +lz4_split_input_left (const struct lz4_split_input *in) +{ +return in->size - in->pos; +} + +static __always_inline int lz4_split_get_byte(struct lz4_split_input *in) +{ +size_t pos = in->pos; + +if (unlikely (pos == in->size)) +return -1 ; + in->pos++; +if (pos < in->split) +return in->first[pos]; +return in->second[pos - in->split]; +} + +/* The caller has checked that two bytes remain in the block. */ +static __always_inline int lz4_split_get_offset(struct lz4_split_input *in) +{ +size_t pos = in->pos; + + in->pos +=2 ; +if (pos >= in->split) +return get_unaligned_le16 (in->second + (pos - in->split)); +if (in->split - pos >=2 ) +return get_unaligned_le16 (in->first + pos); +return in->first[pos] | (in->second[0 ] <<8 ); +} + +static __always_inline bool +lz4_split_copy_literals (struct lz4_split_input *in,u8 *dst,size_t len) +{ +size_t pos = in->pos; + +if (unlikely (len >lz4_split_input_left (in))) +return false; +if (!len) +return true; +if (pos < in->split) { +size_t n =min (len, in->split - pos); + +memcpy (dst, in->first + pos, n); + pos += n; + dst += n; + len -= n; + } +if (len) +memcpy (dst, in->second + (pos - in->split), len); + in->pos = pos + len; +return true; +} + +/* The decoder has checked the lookbehind distance and output capacity. */ +static __always_inline void +lz4_split_copy_match (u8 *dst,size_t distance,size_t len) +{ +size_t copied =min (distance, len); + +if (distance ==1 ) { +memset (dst, dst[-1 ], len); +return ; + } + +memcpy (dst, dst - distance, copied); +/* Reuse the period already written, keeping each copy disjoint. */ +while (len - copied > copied) { +memcpy (dst + copied, dst, copied); + copied *=2 ; + } +if (copied < len) +memcpy (dst + copied, dst, len - copied); +} + +static __always_inline int +lz4_split_read_length (struct lz4_split_input *in,int length,int limit) +{ +int byte; + +if (length > limit) +return -EINVAL ; +if (length <RUN_MASK ) +return length; + +do { + byte =lz4_split_get_byte (in); +if (byte <0 || byte > limit - length) +return -EINVAL ; + length += byte; + }while (byte ==255 ); +return length; +} + +int LZ4_decompress_safe_split (const u8 *first,size_t first_len, +const u8 *second,size_t second_len, +u8 *dst,int capacity) +{ +struct lz4_split_input in = { + .first = first, + .second = second, + .split = first_len, + }; +int written =0 , min_last_literals =0 ; + +if (capacity <0 || !dst || (!first && first_len) || + (!second && second_len) || first_len >SIZE_MAX - second_len) +return -EINVAL ; + in.size = first_len + second_len; + +for (;;) { +int token, literal, distance, match; + + token =lz4_split_get_byte (&in); +if (token <0 ) +return -EINVAL ; + literal =lz4_split_read_length (&in, token >>ML_BITS , + capacity - written); +if (literal <0 || + !lz4_split_copy_literals (&in, dst + written, literal)) +return -EINVAL ; + written += literal; +if (!lz4_split_input_left (&in)) +return literal >= min_last_literals ? written : -EINVAL ; + +/* A match must leave room for the final literal sequence. */ +if (lz4_split_input_left (&in) <2 +1 +LASTLITERALS || + capacity - written <MFLIMIT ) +return -EINVAL ; + + distance =lz4_split_get_offset (&in); +if (!distance || distance > written) +return -EINVAL ; + match =lz4_split_read_length (&in, token &ML_MASK , + capacity - written -MINMATCH - +LASTLITERALS ); +if (match <0 ) +return -EINVAL ; + match +=MINMATCH ; +lz4_split_copy_match (dst + written, distance, match); + written += match; +/* +* Leave at least LASTLITERALS bytes and end at least MFLIMIT +* bytes past the start of the last match. +*/ + min_last_literals =max (LASTLITERALS ,MFLIMIT - match); + } +} +EXPORT_SYMBOL_GPL (LZ4_decompress_safe_split); +EXPORT_SYMBOL (LZ4_decompress_safe);EXPORT_SYMBOL (LZ4_decompress_safe_partial);EXPORT_SYMBOL (LZ4_decompress_fast);Signature · 2 lines
-- 2.47.3
Re: [PATCH RFC 2/3] lib/lz4: add safe decompression from two input buffers
Quoted 4 lines · Sergey Senozhatsky
Let me Cc Herbert and zswap folks on this. Herbert mentioned previously (back in the days when we landed SG-list based zsmalloc object read API) that lz4 can be extended to support spanning zsmalloc objects and, if this lands, then zswap may also be interested in adopting it (along with zram).
I tested this approach using real zram data from an Android phone, but didn't see any performance gains. :(
I'd be interested to hear if anyone has a better approach.
Quoted 229 lines · Xueyuan Chen
Quoted 226 lines
LZ4_decompress_generic()assumes contiguous input. Its wide literal loads can cross the boundary between independently mapped fragments.Add a dictionary-free block decoder that reads from two buffers and handles literals, lengths and offsets that cross the split.
Signed-off-by Xueyuan Chen <[email protected]>--- include/linux/lz4.h | 18 ++++++ lib/lz4/lz4_decompress.c | 163 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 181 insertions(+)include/linux/lz4.h +18
-0@@ -292,6 +292,24 @@ int LZ4_decompress_fast(const char *source, char *dest, int originalSize);int LZ4_decompress_safe (const char *source,char *dest,int compressedSize,int maxDecompressedSize); +/** +* LZ4_decompress_safe_split() - Decode a single block from two buffers +* @first: address of the first input fragment +* @first_len: bytes in the first input fragment +* @second: address of the second input fragment +* @second_len: bytes in the second input fragment +* @dst: output buffer, disjoint from the input buffers +* @capacity: bytes available in the output buffer +* +* The fragments, in order, contain one dictionary-free LZ4 block. +* Their addresses need not be adjacent. An empty fragment may be NULL. +* +* Return: the decoded size, or -EINVAL on error. +*/ +int LZ4_decompress_safe_split (const u8 *first,size_t first_len, +const u8 *second,size_t second_len, +u8 *dst,int capacity); +/** * LZ4_decompress_safe_partial() - Decompress a block of size 'compressedSize' * at position 'source' into buffer 'dest' lib/lz4/lz4_decompress.c +163
-0@@ -39,6 +39,11 @@#include <linux/kernel.h> #include <linux/unaligned.h> +#ifndef STATIC +#include <linux/errno.h> +#include <linux/limits.h> +#endif +/*-***************************** * Decompression functions *******************************/ @@ -705,6 +710,164 @@ int LZ4_decompress_fast_usingDict(const char *source, char *dest, }#ifndef STATIC +struct lz4_split_input { +const u8 *first; +const u8 *second; +size_t split; +size_t size; +size_t pos; +}; + +static __always_inline size_t +lz4_split_input_left (const struct lz4_split_input *in) +{ +return in->size - in->pos; +} + +static __always_inline int lz4_split_get_byte(struct lz4_split_input *in) +{ +size_t pos = in->pos; + +if (unlikely (pos == in->size)) +return -1 ; + in->pos++; +if (pos < in->split) +return in->first[pos]; +return in->second[pos - in->split]; +} + +/* The caller has checked that two bytes remain in the block. */ +static __always_inline int lz4_split_get_offset(struct lz4_split_input *in) +{ +size_t pos = in->pos; + + in->pos +=2 ; +if (pos >= in->split) +return get_unaligned_le16 (in->second + (pos - in->split)); +if (in->split - pos >=2 ) +return get_unaligned_le16 (in->first + pos); +return in->first[pos] | (in->second[0 ] <<8 ); +} + +static __always_inline bool +lz4_split_copy_literals (struct lz4_split_input *in,u8 *dst,size_t len) +{ +size_t pos = in->pos; + +if (unlikely (len >lz4_split_input_left (in))) +return false; +if (!len) +return true; +if (pos < in->split) { +size_t n =min (len, in->split - pos); + +memcpy (dst, in->first + pos, n); + pos += n; + dst += n; + len -= n; + } +if (len) +memcpy (dst, in->second + (pos - in->split), len); + in->pos = pos + len; +return true; +} + +/* The decoder has checked the lookbehind distance and output capacity. */ +static __always_inline void +lz4_split_copy_match (u8 *dst,size_t distance,size_t len) +{ +size_t copied =min (distance, len); + +if (distance ==1 ) { +memset (dst, dst[-1 ], len); +return ; + } + +memcpy (dst, dst - distance, copied); +/* Reuse the period already written, keeping each copy disjoint. */ +while (len - copied > copied) { +memcpy (dst + copied, dst, copied); + copied *=2 ; + } +if (copied < len) +memcpy (dst + copied, dst, len - copied); +} + +static __always_inline int +lz4_split_read_length (struct lz4_split_input *in,int length,int limit) +{ +int byte; + +if (length > limit) +return -EINVAL ; +if (length <RUN_MASK ) +return length; + +do { + byte =lz4_split_get_byte (in); +if (byte <0 || byte > limit - length) +return -EINVAL ; + length += byte; + }while (byte ==255 ); +return length; +} + +int LZ4_decompress_safe_split (const u8 *first,size_t first_len, +const u8 *second,size_t second_len, +u8 *dst,int capacity) +{ +struct lz4_split_input in = { + .first = first, + .second = second, + .split = first_len, + }; +int written =0 , min_last_literals =0 ; + +if (capacity <0 || !dst || (!first && first_len) || + (!second && second_len) || first_len >SIZE_MAX - second_len) +return -EINVAL ; + in.size = first_len + second_len; + +for (;;) { +int token, literal, distance, match; + + token =lz4_split_get_byte (&in); +if (token <0 ) +return -EINVAL ; + literal =lz4_split_read_length (&in, token >>ML_BITS , + capacity - written); +if (literal <0 || + !lz4_split_copy_literals (&in, dst + written, literal)) +return -EINVAL ; + written += literal; +if (!lz4_split_input_left (&in)) +return literal >= min_last_literals ? written : -EINVAL ; + +/* A match must leave room for the final literal sequence. */ +if (lz4_split_input_left (&in) <2 +1 +LASTLITERALS || + capacity - written <MFLIMIT ) +return -EINVAL ; + + distance =lz4_split_get_offset (&in); +if (!distance || distance > written) +return -EINVAL ; + match =lz4_split_read_length (&in, token &ML_MASK , + capacity - written -MINMATCH - +LASTLITERALS ); +if (match <0 ) +return -EINVAL ; + match +=MINMATCH ; +lz4_split_copy_match (dst + written, distance, match); + written += match; +/* +* Leave at least LASTLITERALS bytes and end at least MFLIMIT +* bytes past the start of the last match. +*/ + min_last_literals =max (LASTLITERALS ,MFLIMIT - match); + } +} +EXPORT_SYMBOL_GPL (LZ4_decompress_safe_split); +EXPORT_SYMBOL (LZ4_decompress_safe);EXPORT_SYMBOL (LZ4_decompress_safe_partial);EXPORT_SYMBOL (LZ4_decompress_fast);Signature · 2 lines
-- 2.47.3
RFC PATCH 3/3 zram: use split-input decompression for LZ4 and LZ4HC
In reply to Xueyuan ChenUse the split decoder for dictionary-free LZ4 and LZ4HC blocks. Keep dictionary-backed reads on the linear path and require the split decoder to produce a full page.
--- drivers/block/zram/backend_lz4.c | 1 + drivers/block/zram/backend_lz4.h | 19 +++++++++++++++++++ drivers/block/zram/backend_lz4hc.c | 2 ++ 3 files changed, 22 insertions(+)
drivers/block/zram/backend_lz4.c +1 -0
@@ -150,6 +150,7 @@ static int lz4_decompress(struct zcomp_params *params, struct zcomp_ctx *ctx,const struct zcomp_ops backend_lz4 = { .compress = lz4_compress, .decompress = lz4_decompress, + .decompress_split = lz4_decompress_split, .create_ctx = lz4_create, .destroy_ctx = lz4_destroy, .setup_params = lz4_setup_params,
drivers/block/zram/backend_lz4.h +19 -0
@@ -3,8 +3,27 @@#ifndef __BACKEND_LZ4_H__#define __BACKEND_LZ4_H__ +#include <linux/errno.h> +#include <linux/lz4.h> +#include "zcomp.h" +static inline int lz4_decompress_split (struct zcomp_params *params, +struct zcomp_ctx *ctx, +const struct zcomp_req *head, +const void *tail,size_t tail_len) +{ +int decoded; + +if (params->dict_sz) +return -EOPNOTSUPP ; + decoded =LZ4_decompress_safe_split (head->src, head->src_len, tail, + tail_len, head->dst, head->dst_len); +if (decoded <0 ) +return decoded; +return decoded == head->dst_len ?0 : -EINVAL ; +} +extern const struct zcomp_ops backend_lz4;#endif /* __BACKEND_LZ4_H__ */
drivers/block/zram/backend_lz4hc.c +2 -0
@@ -7,6 +7,7 @@#include <linux/slab.h> #include <linux/vmalloc.h> +#include "backend_lz4.h" #include "backend_lz4hc.h" struct lz4hc_ctx {
@@ -134,6 +135,7 @@ static int lz4hc_decompress(struct zcomp_params *params, struct zcomp_ctx *ctx,const struct zcomp_ops backend_lz4hc = { .compress = lz4hc_compress, .decompress = lz4hc_decompress, + .decompress_split = lz4_decompress_split, .create_ctx = lz4hc_create, .destroy_ctx = lz4hc_destroy, .setup_params = lz4hc_setup_params,
Signature · 1 line
2.47.3
Hi Xueyuan,
Quoted 14 lines · Xueyuan Chen
I measured the time to swap in 1 GiB of synthetic data on arm64 with 4 KiB pages. Each page started with pseudorandom data of one of 16 lengths, followed by zeros. On this workload, 32.6% of compressed reads spanned two pages with LZ4 and 34.4% with LZ4HC.
The table shows median swap-in times over seven pairs. The last two columns summarize the percentage change within each pair.
Codec Workers Baseline (ms) Patched (ms) Median Range LZ4 1 1403.81 1344.45 -4.21% -4.64% to -1.61% LZ4 8 222.57 216.36 -2.73% -4.19% to -1.97% LZ4HC 1 1403.35 1336.99 -4.74% -5.07% to -2.90% LZ4HC 8 222.10 215.77 -3.16% -4.39% to -1.27%
Quoted 8 lines
These timings include both skipping the copy and switching decoders.
I used Codex to help implement this series.
Do you think the swap-in improvement justifies maintaining a second LZ4 decoder?Assisted-by LLMSigned-off-by Xueyuan Chen <[email protected]>
LZ4_decompress_safe_split() isn't actually very well optimized. While it avoids the copy of the source data into a contiguous buffer, it copies every match and literal run with a variable-length memcpy(), which on most data is a very slow way to implement LZ decompression.
I suspect it looked good on your synthetic benchmark (pseudorandom data followed by 0's) only because that tended to generate 1 very long literal run and 1 very long match. Whereas normal data would have lots of interleaved literal runs and matches of much shorter lengths where an external memcpy() call does very poorly; that is why the existing LZ4 decompression code does not do it that way.
To some extent the literal run and match copy performance is fixable with a better implementation. However, having to handle two different source buffers still introduces overhead that cannot go away.
So I feel this approach is probably not worth it. If you'd like to continue to explore it, it would need to be tested on different data and would need to be properly optimized.
- Eric
Quoted 42 lines · Eric Biggers
Hi Xueyuan,
Quoted 14 lines · Xueyuan Chen
I measured the time to swap in 1 GiB of synthetic data on arm64 with 4 KiB pages. Each page started with pseudorandom data of one of 16 lengths, followed by zeros. On this workload, 32.6% of compressed reads spanned two pages with LZ4 and 34.4% with LZ4HC.
The table shows median swap-in times over seven pairs. The last two columns summarize the percentage change within each pair.
Codec Workers Baseline (ms) Patched (ms) Median Range LZ4 1 1403.81 1344.45 -4.21% -4.64% to -1.61% LZ4 8 222.57 216.36 -2.73% -4.19% to -1.97% LZ4HC 1 1403.35 1336.99 -4.74% -5.07% to -2.90% LZ4HC 8 222.10 215.77 -3.16% -4.39% to -1.27%Quoted 8 lines
These timings include both skipping the copy and switching decoders.
I used Codex to help implement this series.
Do you think the swap-in improvement justifies maintaining a second LZ4 decoder?Assisted-by LLMSigned-off-by Xueyuan Chen <[email protected]>
LZ4_decompress_safe_split()isn't actually very well optimized. While it avoids the copy of the source data into a contiguous buffer, it copies every match and literal run with a variable-lengthmemcpy(), which on most data is a very slow way to implement LZ decompression.I suspect it looked good on your synthetic benchmark (pseudorandom data followed by 0's) only because that tended to generate 1 very long literal run and 1 very long match. Whereas normal data would have lots of interleaved literal runs and matches of much shorter lengths where an external
memcpy()call does very poorly; that is why the existing LZ4 decompression code does not do it that way.To some extent the literal run and match copy performance is fixable with a better implementation. However, having to handle two different source buffers still introduces overhead that cannot go away.
Quoted 7 lines
So I feel this approach is probably not worth it. If you'd like to continue to explore it, it would need to be tested on different data and would need to be properly optimized.
- Eric
Hi Eric,
Thanks for taking a look. I was also unsure how representative the synthetic workload was, so I was hoping to get some feedback from experienced kernel developers.
I'll collect 1 GiB of data from zram on an Android phone and rerun the swap-in benchmark with it.
Quoted 42 lines · Eric Biggers
Hi Xueyuan,
Quoted 14 lines · Xueyuan Chen
I measured the time to swap in 1 GiB of synthetic data on arm64 with 4 KiB pages. Each page started with pseudorandom data of one of 16 lengths, followed by zeros. On this workload, 32.6% of compressed reads spanned two pages with LZ4 and 34.4% with LZ4HC.
The table shows median swap-in times over seven pairs. The last two columns summarize the percentage change within each pair.
Codec Workers Baseline (ms) Patched (ms) Median Range LZ4 1 1403.81 1344.45 -4.21% -4.64% to -1.61% LZ4 8 222.57 216.36 -2.73% -4.19% to -1.97% LZ4HC 1 1403.35 1336.99 -4.74% -5.07% to -2.90% LZ4HC 8 222.10 215.77 -3.16% -4.39% to -1.27%Quoted 8 lines
These timings include both skipping the copy and switching decoders.
I used Codex to help implement this series.
Do you think the swap-in improvement justifies maintaining a second LZ4 decoder?Assisted-by LLMSigned-off-by Xueyuan Chen <[email protected]>
LZ4_decompress_safe_split()isn't actually very well optimized. While it avoids the copy of the source data into a contiguous buffer, it copies every match and literal run with a variable-lengthmemcpy(), which on most data is a very slow way to implement LZ decompression.I suspect it looked good on your synthetic benchmark (pseudorandom data followed by 0's) only because that tended to generate 1 very long literal run and 1 very long match. Whereas normal data would have lots of interleaved literal runs and matches of much shorter lengths where an external
memcpy()call does very poorly; that is why the existing LZ4 decompression code does not do it that way.To some extent the literal run and match copy performance is fixable with a better implementation. However, having to handle two different source buffers still introduces overhead that cannot go away.
Quoted 6 lines
So I feel this approach is probably not worth it. If you'd like to continue to explore it, it would need to be tested on different data and would need to be properly optimized.
- Eric
Hi Eric,
I reran the benchmark on arm64 with pages from an Android phone's zram export. Optimizing short match copies cut cycles per spanning read by 22–27% compared with the RFC decoder.
I also tried restoring the LZ4 short-sequence fast path within each input fragment. That version was still slower than the existing copy-and-decompress path in all 28 paired runs. Swap-in took about 6.6–6.8% longer with one worker and 3.8% longer with eight workers.
I'll drop this RFC. Thanks for pointing me toward the short-copy overhead and the workload issue.
Sergey Senozhatsky
maintainerQuoted 28 lines · Xueyuan Chen
Quoted 16 lines
LZ4_decompress_safe_split()isn't actually very well optimized. While it avoids the copy of the source data into a contiguous buffer, it copies every match and literal run with a variable-lengthmemcpy(), which on most data is a very slow way to implement LZ decompression.I suspect it looked good on your synthetic benchmark (pseudorandom data followed by 0's) only because that tended to generate 1 very long literal run and 1 very long match. Whereas normal data would have lots of interleaved literal runs and matches of much shorter lengths where an external
memcpy()call does very poorly; that is why the existing LZ4 decompression code does not do it that way.To some extent the literal run and match copy performance is fixable with a better implementation. However, having to handle two different source buffers still introduces overhead that cannot go away.
Quoted 6 lines
So I feel this approach is probably not worth it. If you'd like to continue to explore it, it would need to be tested on different data and would need to be properly optimized.
- Eric
Hi Eric,
I reran the benchmark on arm64 with pages from an Android phone's zram export. Optimizing short match copies cut cycles per spanning read by 22–27% compared with the RFC decoder.
Quoted 7 lines
I also tried restoring the LZ4 short-sequence fast path within each input fragment. That version was still slower than the existing copy-and-decompress path in all 28 paired runs. Swap-in took about 6.6–6.8% longer with one worker and 3.8% longer with eight workers.
I'll drop this RFC. Thanks for pointing me toward the short-copy overhead and the workload issue.
Oh, I see, thanks for the update.