##// END OF EJS Templates
util: implement zstd compression engine...
util: implement zstd compression engine Now that zstd is vendored and being built (in some configurations), we can implement a compression engine for zstd! The zstd engine is a little different from existing engines. Because it may not always be present, we have to defer load the module in case importing it fails. We facilitate this via a cached property that holds a reference to the module or None. The "available" method is implemented to reflect reality. The zstd engine declares its ability to handle bundles using the "zstd" human name and the "ZS" internal name. The latter was chosen because internal names are 2 characters (by only convention I think) and "ZS" seems reasonable. The engine, like others, supports specifying the compression level. However, there are no consumers of this API that yet pass in that argument. I have plans to change that, so stay tuned. Since all we need to do to support bundle generation with a new compression engine is implement and register the compression engine, bundle generation with zstd "just works!" Tests demonstrating this have been added. How does performance of zstd for bundle generation compare? On the mozilla-unified repo, `hg bundle --all -t <engine>-v2` yields the following on my i7-6700K on Linux: engine CPU time bundle size vs orig size throughput none 97.0s 4,054,405,584 100.0% 41.8 MB/s bzip2 (l=9) 393.6s 975,343,098 24.0% 10.3 MB/s gzip (l=6) 184.0s 1,140,533,074 28.1% 22.0 MB/s zstd (l=1) 108.2s 1,119,434,718 27.6% 37.5 MB/s zstd (l=2) 111.3s 1,078,328,002 26.6% 36.4 MB/s zstd (l=3) 113.7s 1,011,823,727 25.0% 35.7 MB/s zstd (l=4) 116.0s 1,008,965,888 24.9% 35.0 MB/s zstd (l=5) 121.0s 977,203,148 24.1% 33.5 MB/s zstd (l=6) 131.7s 927,360,198 22.9% 30.8 MB/s zstd (l=7) 139.0s 912,808,505 22.5% 29.2 MB/s zstd (l=12) 198.1s 854,527,714 21.1% 20.5 MB/s zstd (l=18) 681.6s 789,750,690 19.5% 5.9 MB/s On compression, zstd for bundle generation delivers: * better compression than gzip with significantly less CPU utilization * better than bzip2 compression ratios while still being significantly faster than gzip * ability to aggressively tune compression level to achieve significantly smaller bundles That last point is important. With clone bundles, a server can pre-generate a bundle file, upload it to a static file server, and redirect clients to transparently download it during clone. The server could choose to produce a zstd bundle with the highest compression settings possible. This would take a very long time - a magnitude longer than a typical zstd bundle generation - but the result would be hundreds of megabytes smaller! For the clone volume we do at Mozilla, this could translate to petabytes of bandwidth savings per year and faster clones (due to smaller transfer size). I don't have detailed numbers to report on decompression. However, zstd decompression is fast: >1 GB/s output throughput on this machine, even through the Python bindings. And it can do that regardless of the compression level of the input. By the time you have enough data to worry about overhead of decompression, you have plenty of other things to worry about performance wise. zstd is wins all around. I can't wait to implement support for it on the wire protocol and in revlogs.

File last commit:

r30435:b86a448a default
r30442:41a81067 default
Show More
make_cffi.py
110 lines | 3.3 KiB | text/x-python | PythonLexer
Gregory Szorc
zstd: vendor python-zstandard 0.5.0...
r30435 # Copyright (c) 2016-present, Gregory Szorc
# All rights reserved.
#
# This software may be modified and distributed under the terms
# of the BSD license. See the LICENSE file for details.
from __future__ import absolute_import
import cffi
import os
HERE = os.path.abspath(os.path.dirname(__file__))
SOURCES = ['zstd/%s' % p for p in (
'common/entropy_common.c',
'common/error_private.c',
'common/fse_decompress.c',
'common/xxhash.c',
'common/zstd_common.c',
'compress/fse_compress.c',
'compress/huf_compress.c',
'compress/zbuff_compress.c',
'compress/zstd_compress.c',
'decompress/huf_decompress.c',
'decompress/zbuff_decompress.c',
'decompress/zstd_decompress.c',
'dictBuilder/divsufsort.c',
'dictBuilder/zdict.c',
)]
INCLUDE_DIRS = [os.path.join(HERE, d) for d in (
'zstd',
'zstd/common',
'zstd/compress',
'zstd/decompress',
'zstd/dictBuilder',
)]
with open(os.path.join(HERE, 'zstd', 'zstd.h'), 'rb') as fh:
zstd_h = fh.read()
ffi = cffi.FFI()
ffi.set_source('_zstd_cffi', '''
/* needed for typedefs like U32 references in zstd.h */
#include "mem.h"
#define ZSTD_STATIC_LINKING_ONLY
#include "zstd.h"
''',
sources=SOURCES, include_dirs=INCLUDE_DIRS)
# Rather than define the API definitions from zstd.h inline, munge the
# source in a way that cdef() will accept.
lines = zstd_h.splitlines()
lines = [l.rstrip() for l in lines if l.strip()]
# Strip preprocessor directives - they aren't important for our needs.
lines = [l for l in lines
if not l.startswith((b'#if', b'#else', b'#endif', b'#include'))]
# Remove extern C block
lines = [l for l in lines if l not in (b'extern "C" {', b'}')]
# The version #defines don't parse and aren't necessary. Strip them.
lines = [l for l in lines if not l.startswith((
b'#define ZSTD_H_235446',
b'#define ZSTD_LIB_VERSION',
b'#define ZSTD_QUOTE',
b'#define ZSTD_EXPAND_AND_QUOTE',
b'#define ZSTD_VERSION_STRING',
b'#define ZSTD_VERSION_NUMBER'))]
# The C parser also doesn't like some constant defines referencing
# other constants.
# TODO we pick the 64-bit constants here. We should assert somewhere
# we're compiling for 64-bit.
def fix_constants(l):
if l.startswith(b'#define ZSTD_WINDOWLOG_MAX '):
return b'#define ZSTD_WINDOWLOG_MAX 27'
elif l.startswith(b'#define ZSTD_CHAINLOG_MAX '):
return b'#define ZSTD_CHAINLOG_MAX 28'
elif l.startswith(b'#define ZSTD_HASHLOG_MAX '):
return b'#define ZSTD_HASHLOG_MAX 27'
elif l.startswith(b'#define ZSTD_CHAINLOG_MAX '):
return b'#define ZSTD_CHAINLOG_MAX 28'
elif l.startswith(b'#define ZSTD_CHAINLOG_MIN '):
return b'#define ZSTD_CHAINLOG_MIN 6'
elif l.startswith(b'#define ZSTD_SEARCHLOG_MAX '):
return b'#define ZSTD_SEARCHLOG_MAX 26'
elif l.startswith(b'#define ZSTD_BLOCKSIZE_ABSOLUTEMAX '):
return b'#define ZSTD_BLOCKSIZE_ABSOLUTEMAX 131072'
else:
return l
lines = map(fix_constants, lines)
# ZSTDLIB_API isn't handled correctly. Strip it.
lines = [l for l in lines if not l.startswith(b'# define ZSTDLIB_API')]
def strip_api(l):
if l.startswith(b'ZSTDLIB_API '):
return l[len(b'ZSTDLIB_API '):]
else:
return l
lines = map(strip_api, lines)
source = b'\n'.join(lines)
ffi.cdef(source.decode('latin1'))
if __name__ == '__main__':
ffi.compile()