utils/md5_file: don't iterate line-by-line
PATCH 1/2 utils/md5_file: don't iterate line-by-line
Opening a file in binary mode and iterating it seems like the simple solution but will still break on newlines, which for binary files isn't really useful as the size of the chunks could be huge or tiny.
Instead, let's be a bit more clever: we'll be MD5ing lots of files, but we don't want to fill up memory: use mmap() to open the file and read the file in 8k blocks.
--- bitbake/lib/bb/utils.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-)
bitbake/lib/bb/utils.py +9 -4
@@ -524,12 +524,17 @@ def md5_file(filename): """ Return the hex string representation of the MD5 checksum of filename. """ - import hashlib - m = hashlib.md5() + import hashlib, mmap with open(filename, "rb") as f: - for line in f: - m.update(line) + m = hashlib.md5() + try: + with mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ) as mm: + for chunk in iter(lambda: mm.read(8192), b''): + m.update(chunk) + except ValueError: + # You can't mmap() an empty file so silence this exception + pass return m.hexdigest() def sha256_file(filename):
Signature · 1 line
2.11.0
PATCH 2/2 checksum: sanity check path when recursively checksumming
In case something goes tragically wrong, catch a request to checksum / and refuse.
--- bitbake/lib/bb/checksum.py | 2 ++ 1 file changed, 2 insertions(+)
bitbake/lib/bb/checksum.py +2 -0
@@ -97,6 +97,8 @@ class FileChecksumCache(MultiProcessCache): def checksum_dir(pth): # Handle directories recursively + if pth == "/": + bb.fatal("Refusing to checksum /") dirchecksums = [] for root, dirs, files in os.walk(pth): for name in files:
Signature · 1 line
2.11.0
Quoted 29 lines · Ross Burton
Opening a file in binary mode and iterating it seems like the simple solution but will still break on newlines, which for binary files isn't really useful as the size of the chunks could be huge or tiny.
Instead, let's be a bit more clever: we'll be MD5ing lots of files, but we don't want to fill up memory: use
mmap()to open the file and read the file in 8k blocks.Signed-off-by Ross Burton <[email protected]>--- bitbake/lib/bb/utils.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-)bitbake/lib/bb/utils.py +2
-4@@ -524,12 +524,17 @@ def md5_file(filename): """ Return the hex string representation of the MD5 checksum of filename. """ - import hashlib - m = hashlib.md5() + import hashlib, mmap with open(filename, "rb") as f: - for line in f: - m.update(line) + m = hashlib.md5()
bitbake/lib/bb/utils.py def md5_file(filename): · line 540
Quoted 10 lines
bitbake/lib/bb/utils.py +7
-0+ try: + with mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ) as mm: + for chunk in iter(lambda: mm.read(8192), b''): + m.update(chunk) + except ValueError: + # You can't mmap() an empty file so silence this exception + pass return m.hexdigest() def sha256_file(filename):
Fixes the problem I was having. thanks
Quoted 3 lines · Ross Burton
Opening a file in binary mode and iterating it seems like the simple solution but will still break on newlines, which for binary files isn't really useful as the size of the chunks could be huge or tiny.
And what exactly was wrong with
http://lists.openembedded.org/pipermail/bitbake-devel/2018-July/009407.html
http://lists.openembedded.org/pipermail/bitbake-devel/2018-July/009409.html
? I have lots of patches and ideas for performance improvements in bitbake, pseudo and other components, but I'm not getting any response at all, which is rather frustrating.
Rasmus
Nothing whatsoever, I don't read bitbake-devel often so didn't spot those. The mmap approach is slightly faster though in my benchmarks.
If you have other patches which are not getting merged or commented on, please ping the thread to remind the maintainers.
Ross
Quoted 15 lines · Rasmus Villemoes
Quoted 3 lines · Ross Burton
Opening a file in binary mode and iterating it seems like the simple solution but will still break on newlines, which for binary files isn't really useful as the size of the chunks could be huge or tiny.
And what exactly was wrong with
http://lists.openembedded.org/pipermail/bitbake-devel/2018-July/009407.html
http://lists.openembedded.org/pipermail/bitbake-devel/2018-July/009409.html? I have lots of patches and ideas for performance improvements in bitbake, pseudo and other components, but I'm not getting any response at all, which is rather frustrating.
Rasmus
Quoted 14 lines · Rasmus Villemoes
Quoted 5 lines · Ross Burton
Opening a file in binary mode and iterating it seems like the simple solution
but will still break on newlines, which for binary files isn't really useful as
the size of the chunks could be huge or tiny.And what exactly was wrong with
http://lists.openembedded.org/pipermail/bitbake-devel/2018-July/00940
7.html
http://lists.openembedded.org/pipermail/bitbake-devel/2018-July/00940
9.html
Quoted 4 lines
? I have lots of patches and ideas for performance improvements in bitbake, pseudo and other components, but I'm not getting any response
at all, which is rather frustrating.
Sorry, that is my fault, I hadn't gotten around to reviewing/testing them.
Ross' patch using mmap should be slightly more efficient so I'll likely go with that but I'd take a patch to sort the other hash functions (e.g. sha256) out similarly and now I'm much more familiar with the code and expecting the change, review should be much simpler/faster...
Sorry once again...
Cheers,
Richard