Skip to content

Commit

Permalink
Avoid taking a lock for reading
Browse files Browse the repository at this point in the history
This isn't needed as git will replace this file atomicially,
hence we always see a fully written file when reading.

Only when writing we need to obtain a lock.
  • Loading branch information
Byron committed Jan 7, 2022
1 parent 2141eae commit d79d20d
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 16 deletions.
17 changes: 2 additions & 15 deletions git/index/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,30 +127,17 @@ def __init__(self, repo: 'Repo', file_path: Union[PathLike, None] = None) -> Non

def _set_cache_(self, attr: str) -> None:
if attr == "entries":
# read the current index
# try memory map for speed
lfd = LockedFD(self._file_path)
ok = False
try:
fd = lfd.open(write=False, stream=False)
ok = True
fd = os.open(self._file_path, os.O_RDONLY)
except OSError:
# in new repositories, there may be no index, which means we are empty
self.entries: Dict[Tuple[PathLike, StageType], IndexEntry] = {}
return None
finally:
if not ok:
lfd.rollback()
# END exception handling

stream = file_contents_ro(fd, stream=True, allow_mmap=True)

try:
self._deserialize(stream)
finally:
lfd.rollback()
# The handles will be closed on destruction
# END read from default index on demand
self._deserialize(stream)
else:
super(IndexFile, self)._set_cache_(attr)

Expand Down

0 comments on commit d79d20d

Please sign in to comment.