From 8162e2e67b7b817d6b01a60df52bb67256fd2ba3 Mon Sep 17 00:00:00 2001 From: Thomas Waldmann Date: Thu, 14 Jan 2021 20:41:57 +0100 Subject: [PATCH] cached_hash is only used in archive, move it there --- src/borg/archive.py | 29 ++++++++++++++++++++++++++++- src/borg/chunker.pyx | 26 -------------------------- 2 files changed, 28 insertions(+), 27 deletions(-) diff --git a/src/borg/archive.py b/src/borg/archive.py index 90013bd29..b597a1aa6 100644 --- a/src/borg/archive.py +++ b/src/borg/archive.py @@ -19,7 +19,7 @@ logger = create_logger() from . import xattr -from .chunker import get_chunker, Chunk, cached_hash +from .chunker import get_chunker, Chunk from .cache import ChunkListEntry from .crypto.key import key_factory from .compress import Compressor, CompressionSpec @@ -41,6 +41,7 @@ from .helpers import os_open, flags_normal, flags_dir from .helpers import msgpack from .helpers import sig_int +from .lrucache import LRUCache from .patterns import PathPrefixPattern, FnmatchPattern, IECommand from .item import Item, ArchiveItem, ItemDiff from .platform import acl_get, acl_set, set_flags, get_flags, swidth, hostname @@ -1088,6 +1089,32 @@ def stat_attrs(self, st, path, fd=None): return attrs +# remember a few recently used all-zero chunk hashes in this mapping. +# (hash_func, chunk_length) -> chunk_hash +# we play safe and have the hash_func in the mapping key, in case we +# have different hash_funcs within the same borg run. +zero_chunk_ids = LRUCache(10, dispose=lambda _: None) + + +def cached_hash(chunk, id_hash): + allocation = chunk.meta['allocation'] + if allocation == CH_DATA: + data = chunk.data + chunk_id = id_hash(data) + elif allocation in (CH_HOLE, CH_ALLOC): + size = chunk.meta['size'] + assert size <= len(zeros) + data = memoryview(zeros)[:size] + try: + chunk_id = zero_chunk_ids[(id_hash, size)] + except KeyError: + chunk_id = id_hash(data) + zero_chunk_ids[(id_hash, size)] = chunk_id + else: + raise ValueError('unexpected allocation type') + return chunk_id, data + + class ChunksProcessor: # Processes an iterator of chunks for an Item diff --git a/src/borg/chunker.pyx b/src/borg/chunker.pyx index 099532308..210ea461c 100644 --- a/src/borg/chunker.pyx +++ b/src/borg/chunker.pyx @@ -7,7 +7,6 @@ import os from collections import namedtuple from .constants import CH_DATA, CH_ALLOC, CH_HOLE, MAX_DATA_SIZE, zeros -from .lrucache import LRUCache from libc.stdlib cimport free @@ -53,31 +52,6 @@ def Chunk(data, **meta): return _Chunk(meta, data) -# remember a few recently used all-zero chunk hashes in this mapping. -# (hash_func, chunk_length) -> chunk_hash -# we play safe and have the hash_func in the mapping key, in case we -# have different hash_funcs within the same borg run. -zero_chunk_ids = LRUCache(10, dispose=lambda _: None) - -def cached_hash(chunk, id_hash): - allocation = chunk.meta['allocation'] - if allocation == CH_DATA: - data = chunk.data - chunk_id = id_hash(data) - elif allocation in (CH_HOLE, CH_ALLOC): - size = chunk.meta['size'] - assert size <= len(zeros) - data = memoryview(zeros)[:size] - try: - chunk_id = zero_chunk_ids[(id_hash, size)] - except KeyError: - chunk_id = id_hash(data) - zero_chunk_ids[(id_hash, size)] = chunk_id - else: - raise ValueError('unexpected allocation type') - return chunk_id, data - - def dread(offset, size, fd=None, fh=-1): use_fh = fh >= 0 if use_fh: