revlog: don't flush data file after every added revision
The current behavior of revlogs is to flush the data file when writing
data to it. Tracing system calls revealed that changegroup processing
incurred numerous write(2) calls for values much smaller than the
default buffer size (Python defaults to 4096, but it can be adjusted
based on detected block size at run time by CPython).
The reason we flush revlogs is so readers have all data available.
For example, the current code in revlog.py will re-open the revlog
file (instead of seeking an existing file handle) to read the text
of a revision. This happens when starting a new delta chain when
adding several revisions from changegroups, for example. Yes, this
is likely sub-optimal (we should probably be sharing file descriptors
between readers and writers to avoid the flushing and associated
overhead of re-opening files).
While flushing revlogs is necessary, it appears all callers are
diligent about flushing files before a read is performed (see
buildtext() in _addrevision()), making the flush in
_writeentry() redundant and unncessary. So, we remove it. In practice,
this means we incur a write(2) a) when the buffer is full (typically
4096 bytes) b) when a new delta chain is created rather than after
every added revision. This applies to every revlog, but by volume
it mostly impacts filelogs.
Removing the redundant flush from _writeentry() significantly
reduces the number of write(2) calls during changegroup processing on
my Linux machine. When applying a changegroup of the hg repo based on
my local repo, the total number of write(2) calls during application
of the mercurial/localrepo.py revlogs dropped from 1,320 to 217 with
this patch applied. Total I/O related system calls dropped from 1,577
to 474.
When unbundling a mozilla-central gzipped bundle (264,403 changesets
with 1,492,215 changes to 222,507 files), total write(2) calls
dropped from 1,252,881 to 827,106 and total system calls dropped from
3,601,259 to 3,178,636 - a reduction of 425,775!
While the system call reduction is significant, it appears
to have no impact on wall time on my Linux and Windows machines. Still,
fewer syscalls is fewer syscalls. Surely this can't hurt. If nothing
else, it makes examining remaining system call usage simpler and opens
the door to experimenting with the performance impact of different
buffer sizes.
Set vars:
$ CONTRIBDIR="$TESTDIR/../contrib"
Prepare repo-a:
$ hg init repo-a
$ cd repo-a
$ echo this is file a > a
$ hg add a
$ hg commit -m first
$ echo adding to file a >> a
$ hg commit -m second
$ echo adding more to file a >> a
$ hg commit -m third
$ hg verify
checking changesets
checking manifests
crosschecking files in changesets and manifests
checking files
1 files, 3 changesets, 3 total revisions
Dumping revlog of file a to stdout:
$ python "$CONTRIBDIR/dumprevlog" .hg/store/data/a.i
file: .hg/store/data/a.i
node: 183d2312b35066fb6b3b449b84efc370d50993d0
linkrev: 0
parents: 0000000000000000000000000000000000000000 0000000000000000000000000000000000000000
length: 15
-start-
this is file a
-end-
node: b1047953b6e6b633c0d8197eaa5116fbdfd3095b
linkrev: 1
parents: 183d2312b35066fb6b3b449b84efc370d50993d0 0000000000000000000000000000000000000000
length: 32
-start-
this is file a
adding to file a
-end-
node: 8c4fd1f7129b8cdec6c7f58bf48fb5237a4030c1
linkrev: 2
parents: b1047953b6e6b633c0d8197eaa5116fbdfd3095b 0000000000000000000000000000000000000000
length: 54
-start-
this is file a
adding to file a
adding more to file a
-end-
Dump all revlogs to file repo.dump:
$ find .hg/store -name "*.i" | sort | xargs python "$CONTRIBDIR/dumprevlog" > ../repo.dump
$ cd ..
Undumping into repo-b:
$ hg init repo-b
$ cd repo-b
$ python "$CONTRIBDIR/undumprevlog" < ../repo.dump
.hg/store/00changelog.i
.hg/store/00manifest.i
.hg/store/data/a.i
$ cd ..
Rebuild fncache with clone --pull:
$ hg clone --pull -U repo-b repo-c
requesting all changes
adding changesets
adding manifests
adding file changes
added 3 changesets with 3 changes to 1 files
Verify:
$ hg -R repo-c verify
checking changesets
checking manifests
crosschecking files in changesets and manifests
checking files
1 files, 3 changesets, 3 total revisions
Compare repos:
$ hg -R repo-c incoming repo-a
comparing with repo-a
searching for changes
no changes found
[1]
$ hg -R repo-a incoming repo-c
comparing with repo-c
searching for changes
no changes found
[1]
Test simplemerge command:
$ cp "$CONTRIBDIR/simplemerge" .
$ echo base > base
$ echo local > local
$ cat base >> local
$ cp local orig
$ cat base > other
$ echo other >> other
changing local directly
$ python simplemerge local base other && echo "merge succeeded"
merge succeeded
$ cat local
local
base
other
$ cp orig local
printing to stdout
$ python simplemerge -p local base other
local
base
other
local:
$ cat local
local
base
conflicts
$ cp base conflict-local
$ cp other conflict-other
$ echo not other >> conflict-local
$ echo end >> conflict-local
$ echo end >> conflict-other
$ python simplemerge -p conflict-local base conflict-other
base
<<<<<<< conflict-local
not other
end
=======
other
end
>>>>>>> conflict-other
warning: conflicts during merge.
[1]
1 label
$ python simplemerge -p -L foo conflict-local base conflict-other
base
<<<<<<< foo
not other
end
=======
other
end
>>>>>>> conflict-other
warning: conflicts during merge.
[1]
2 labels
$ python simplemerge -p -L foo -L bar conflict-local base conflict-other
base
<<<<<<< foo
not other
end
=======
other
end
>>>>>>> bar
warning: conflicts during merge.
[1]
3 labels
$ python simplemerge -p -L foo -L bar -L base conflict-local base conflict-other
base
<<<<<<< foo
not other
end
||||||| base
=======
other
end
>>>>>>> bar
warning: conflicts during merge.
[1]
too many labels
$ python simplemerge -p -L foo -L bar -L baz -L buz conflict-local base conflict-other
abort: can only specify three labels.
[255]
binary file
$ $PYTHON -c "f = file('binary-local', 'w'); f.write('\x00'); f.close()"
$ cat orig >> binary-local
$ python simplemerge -p binary-local base other
warning: binary-local looks like a binary file.
[1]
binary file --text
$ python simplemerge -a -p binary-local base other 2>&1
warning: binary-local looks like a binary file.
\x00local (esc)
base
other
help
$ python simplemerge --help
simplemerge [OPTS] LOCAL BASE OTHER
Simple three-way file merge utility with a minimal feature set.
Apply to LOCAL the changes necessary to go from BASE to OTHER.
By default, LOCAL is overwritten with the results of this operation.
options:
-L --label labels to use on conflict markers
-a --text treat all files as text
-p --print print results instead of overwriting LOCAL
--no-minimal no effect (DEPRECATED)
-h --help display help and exit
-q --quiet suppress output
wrong number of arguments
$ python simplemerge
simplemerge: wrong number of arguments
simplemerge [OPTS] LOCAL BASE OTHER
Simple three-way file merge utility with a minimal feature set.
Apply to LOCAL the changes necessary to go from BASE to OTHER.
By default, LOCAL is overwritten with the results of this operation.
options:
-L --label labels to use on conflict markers
-a --text treat all files as text
-p --print print results instead of overwriting LOCAL
--no-minimal no effect (DEPRECATED)
-h --help display help and exit
-q --quiet suppress output
[1]
bad option
$ python simplemerge --foo -p local base other
simplemerge: option --foo not recognized
simplemerge [OPTS] LOCAL BASE OTHER
Simple three-way file merge utility with a minimal feature set.
Apply to LOCAL the changes necessary to go from BASE to OTHER.
By default, LOCAL is overwritten with the results of this operation.
options:
-L --label labels to use on conflict markers
-a --text treat all files as text
-p --print print results instead of overwriting LOCAL
--no-minimal no effect (DEPRECATED)
-h --help display help and exit
-q --quiet suppress output
[1]