comparison shasum.py @ 60:21d2589c96b9

Use madvise if available to sequentially read a file
author Franz Glasner <fzglas.hg@dom66.de>
date Tue, 08 Feb 2022 21:45:14 +0100
parents b96d3585e8ce
children c9f9401abc0c
comparison
equal deleted inserted replaced
59:b96d3585e8ce 60:21d2589c96b9
479 # Use mmap 479 # Use mmap
480 # 480 #
481 # NOTE: On Windows mmapped files with length 0 are not supported. 481 # NOTE: On Windows mmapped files with length 0 are not supported.
482 # So ensure to not call mmap.mmap() if the file size is 0. 482 # So ensure to not call mmap.mmap() if the file size is 0.
483 # 483 #
484 try:
485 madvise = mmap.mmap.madvise
486 except AttributeError:
487 madvise = None
484 if filesize < MAP_CHUNK_SIZE: 488 if filesize < MAP_CHUNK_SIZE:
485 mapsize = filesize 489 mapsize = filesize
486 else: 490 else:
487 mapsize = MAP_CHUNK_SIZE 491 mapsize = MAP_CHUNK_SIZE
488 mapoffset = 0 492 mapoffset = 0
490 while rest > 0: 494 while rest > 0:
491 m = mmap.mmap(fd, 495 m = mmap.mmap(fd,
492 mapsize, 496 mapsize,
493 access=mmap.ACCESS_READ, 497 access=mmap.ACCESS_READ,
494 offset=mapoffset) 498 offset=mapoffset)
499 if madvise:
500 madvise(m, mmap.MADV_SEQUENTIAL)
495 try: 501 try:
496 h.update(m) 502 h.update(m)
497 finally: 503 finally:
498 m.close() 504 m.close()
499 rest -= mapsize 505 rest -= mapsize