##// END OF EJS Templates
rust-dirstate-status: add first Rust implementation of `dirstate.status`...
rust-dirstate-status: add first Rust implementation of `dirstate.status` Note: This patch also added the rayon crate as a Cargo dependency. It will help us immensely in making Rust code parallel and easy to maintain. It is a stable, well-known, and supported crate maintained by people on the Rust team. The current `dirstate.status` method has grown over the years through bug reports and new features to the point where it got too big and too complex. This series does not yet improve the logic, but adds a Rust fast-path to speed up certain cases. Tested on mozilla-try-2019-02-18 with zstd compression: - `hg diff` on an empty working copy: - c: 1.64(+-)0.04s - rust+c before this change: 2.84(+-)0.1s - rust+c: 849(+-)40ms - `hg commit` when creating a file: - c: 5.960s - rust+c before this change: 5.828s - rust+c: 4.668s - `hg commit` when updating a file: - c: 4.866s - rust+c before this change: 4.371s - rust+c: 3.855s - `hg status -mard` - c: 1.82(+-)0.04s - rust+c before this change: 2.64(+-)0.1s - rust+c: 896(+-)30ms The numbers are clear: the current Rust `dirstatemap` implementation is super slow, its performance needs to be addressed. This will be done in a future series, immediately after this one, with the goal of getting Rust to be at least to the speed of the Python + C implementation in all cases before the 5.2 freeze. At worse, we gate dirstatemap to only be used in those cases. Cases where the fast-path is not executed: - for commands that need ignore support (`status`, for example) - if subrepos are found (should not be hard to add, but winter is coming) - any other matcher than an `alwaysmatcher`, like patterns, etc. - with extensions like `sparse` and `fsmonitor` The next step after this is to rethink the logic to be closer to Jane Street's Valentin Gatien-Baron's Rust fast-path which does a lot less work when possible. Differential Revision: https://phab.mercurial-scm.org/D7058

File last commit:

r43347:687b865b default
r43565:99394e6c default
Show More
state.py
149 lines | 4.7 KiB | text/x-python | PythonLexer
# state.py - fsmonitor persistent state
#
# Copyright 2013-2016 Facebook, Inc.
#
# This software may be used and distributed according to the terms of the
# GNU General Public License version 2 or any later version.
from __future__ import absolute_import
import errno
import os
import socket
import struct
from mercurial.i18n import _
from mercurial import (
pathutil,
util,
)
_version = 4
_versionformat = b">I"
class state(object):
def __init__(self, repo):
self._vfs = repo.vfs
self._ui = repo.ui
self._rootdir = pathutil.normasprefix(repo.root)
self._lastclock = None
self._identity = util.filestat(None)
self.mode = self._ui.config(b'fsmonitor', b'mode')
self.walk_on_invalidate = self._ui.configbool(
b'fsmonitor', b'walk_on_invalidate'
)
self.timeout = float(self._ui.config(b'fsmonitor', b'timeout'))
def get(self):
try:
file = self._vfs(b'fsmonitor.state', b'rb')
except IOError as inst:
self._identity = util.filestat(None)
if inst.errno != errno.ENOENT:
raise
return None, None, None
self._identity = util.filestat.fromfp(file)
versionbytes = file.read(4)
if len(versionbytes) < 4:
self._ui.log(
b'fsmonitor',
b'fsmonitor: state file only has %d bytes, '
b'nuking state\n' % len(versionbytes),
)
self.invalidate()
return None, None, None
try:
diskversion = struct.unpack(_versionformat, versionbytes)[0]
if diskversion != _version:
# different version, nuke state and start over
self._ui.log(
b'fsmonitor',
b'fsmonitor: version switch from %d to '
b'%d, nuking state\n' % (diskversion, _version),
)
self.invalidate()
return None, None, None
state = file.read().split(b'\0')
# state = hostname\0clock\0ignorehash\0 + list of files, each
# followed by a \0
if len(state) < 3:
self._ui.log(
b'fsmonitor',
b'fsmonitor: state file truncated (expected '
b'3 chunks, found %d), nuking state\n',
len(state),
)
self.invalidate()
return None, None, None
diskhostname = state[0]
hostname = socket.gethostname()
if diskhostname != hostname:
# file got moved to a different host
self._ui.log(
b'fsmonitor',
b'fsmonitor: stored hostname "%s" '
b'different from current "%s", nuking state\n'
% (diskhostname, hostname),
)
self.invalidate()
return None, None, None
clock = state[1]
ignorehash = state[2]
# discard the value after the last \0
notefiles = state[3:-1]
finally:
file.close()
return clock, ignorehash, notefiles
def set(self, clock, ignorehash, notefiles):
if clock is None:
self.invalidate()
return
# Read the identity from the file on disk rather than from the open file
# pointer below, because the latter is actually a brand new file.
identity = util.filestat.frompath(self._vfs.join(b'fsmonitor.state'))
if identity != self._identity:
self._ui.debug(
b'skip updating fsmonitor.state: identity mismatch\n'
)
return
try:
file = self._vfs(
b'fsmonitor.state', b'wb', atomictemp=True, checkambig=True
)
except (IOError, OSError):
self._ui.warn(_(b"warning: unable to write out fsmonitor state\n"))
return
with file:
file.write(struct.pack(_versionformat, _version))
file.write(socket.gethostname() + b'\0')
file.write(clock + b'\0')
file.write(ignorehash + b'\0')
if notefiles:
file.write(b'\0'.join(notefiles))
file.write(b'\0')
def invalidate(self):
try:
os.unlink(os.path.join(self._rootdir, b'.hg', b'fsmonitor.state'))
except OSError as inst:
if inst.errno != errno.ENOENT:
raise
self._identity = util.filestat(None)
def setlastclock(self, clock):
self._lastclock = clock
def getlastclock(self):
return self._lastclock