gpt4 book ai didi

c - zlib,放气 : How much memory to allocate?

转载 作者:IT王子 更新时间:2023-10-28 23:31:22 27 4
gpt4 key购买 nike

我正在使用 zlib压缩文本数据流。文本数据以 block 的形式出现,对于每个 block ,deflate()被调用,flush 设置为 Z_NO_FLUSH .检索到所有 block 后,deflate()调用 flush 设置为 Z_FINISH .

当然,deflate()不会在每次调用时产生压缩输出。它在内部累积数据以实现高压缩率。没关系!每次deflate()生成压缩输出,该输出被附加到数据库字段 - 一个缓慢的过程。

但是,一旦 deflate()生成压缩数据,该数据可能不适合提供的输出缓冲区,deflate_out .因此多次调用 deflate()是必须的。这就是我想要避免的:

Is there a way to make deflate_out always large enough so that deflate() can store all the compressed data in it, every times it decides to produce output?

注意事项:

  • 未压缩数据的总大小事先知道。如上所述,未压缩的数据以 block 的形式出现,压缩后的数据以 block 的形式附加到数据库字段。

  • 在包含文件中zconf.h我找到了以下评论。这可能是我正在寻找的吗? IE。是 (1 << (windowBits+2)) + (1 << (memLevel+9)) deflate() 的压缩数据的最大大小(以字节为单位)可以生产吗?

    /* The memory requirements for deflate are (in bytes):
    (1 << (windowBits+2)) + (1 << (memLevel+9))
    that is: 128K for windowBits=15 + 128K for memLevel = 8 (default values)
    plus a few kilobytes for small objects. For example, if you want to reduce
    the default memory requirements from 256K to 128K, compile with
    make CFLAGS="-O -DMAX_WBITS=14 -DMAX_MEM_LEVEL=7"
    Of course this will generally degrade compression (there's no free lunch).

    The memory requirements for inflate are (in bytes) 1 << windowBits
    that is, 32K for windowBits=15 (default value) plus a few kilobytes
    for small objects.
    */

最佳答案

deflateBound() 仅在您在一个步骤中完成所有压缩或强制 deflate 压缩当前可用的所有输入数据并为所有输入发出压缩数据时才有用。您可以使用诸如 Z_BLOCK、Z_PARTIAL_FLUSH 等刷新参数来执行此操作。

如果您想使用 Z_NO_FLUSH,那么尝试预测 deflate() 在下一次调用时可能发出的最大输出量将变得更加困难且效率低下。您不知道在发出最后一次压缩数据突发时消耗了多少输入,因此您需要假设几乎没有,缓冲区大小不必要地增长。无论您尝试估计最大输出,您都会无缘无故地执行大量不必要的 malloc 或 realloc,这是低效的。

没有必要避免调用 deflate() 以获得更多输出。如果您只是循环 deflate() 直到它没有更多输出给您,那么您可以使用分配一次的固定输出缓冲区。这就是 deflate() 和 inflate() 接口(interface)的设计用途。你可以看看http://zlib.net/zlib_how.html有关如何使用该界面的详细记录示例。

顺便说一句,在最新版本的 zlib (1.2.6) 中有一个 deflatePending() 函数可以让你知道 deflate() 有多少输出等待交付。

关于c - zlib,放气 : How much memory to allocate?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8902924/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com