hgbook

view fr/ch04-concepts.xml @ 979:64475a75365b

Merged from rpelisse
author Jean-Marie Clément <JeanMarieClement@web.de>
date Fri Sep 04 18:24:06 2009 +0200 (2009-09-04)
parents
children e6894aa7baf2
line source
1 <!-- vim: set filetype=docbkxml shiftwidth=2 autoindent expandtab tw=77 : -->
3 <chapter>
4 <title>Behind the scenes</title>
5 <para>\label{chap:concepts}</para>
7 <para>Unlike many revision control systems, the concepts upon which
8 Mercurial is built are simple enough that it's easy to understand how
9 the software really works. Knowing this certainly isn't necessary,
10 but I find it useful to have a <quote>mental model</quote> of what's going on.</para>
12 <para>This understanding gives me confidence that Mercurial has been
13 carefully designed to be both <emphasis>safe</emphasis> and <emphasis>efficient</emphasis>. And
14 just as importantly, if it's easy for me to retain a good idea of what
15 the software is doing when I perform a revision control task, I'm less
16 likely to be surprised by its behaviour.</para>
18 <para>In this chapter, we'll initially cover the core concepts behind
19 Mercurial's design, then continue to discuss some of the interesting
20 details of its implementation.</para>
22 <sect1>
23 <title>Mercurial's historical record</title>
25 <sect2>
26 <title>Tracking the history of a single file</title>
28 <para>When Mercurial tracks modifications to a file, it stores the history
29 of that file in a metadata object called a <emphasis>filelog</emphasis>. Each entry
30 in the filelog contains enough information to reconstruct one revision
31 of the file that is being tracked. Filelogs are stored as files in
32 the <filename role="special" class="directory">.hg/store/data</filename> directory. A filelog contains two kinds
33 of information: revision data, and an index to help Mercurial to find
34 a revision efficiently.</para>
36 <para>A file that is large, or has a lot of history, has its filelog stored
37 in separate data (<quote><literal>.d</literal></quote> suffix) and index (<quote><literal>.i</literal></quote>
38 suffix) files. For small files without much history, the revision
39 data and index are combined in a single <quote><literal>.i</literal></quote> file. The
40 correspondence between a file in the working directory and the filelog
41 that tracks its history in the repository is illustrated in
42 figure <xref linkend="fig:concepts:filelog"/>.</para>
44 <informalfigure>
46 <para> <mediaobject><imageobject><imagedata fileref="filelog"/></imageobject><textobject><phrase>XXX add text</phrase></textobject></mediaobject>
47 \caption{Relationships between files in working directory and
48 filelogs in repository}
49 \label{fig:concepts:filelog}</para>
50 </informalfigure>
52 </sect2>
53 <sect2>
54 <title>Managing tracked files</title>
56 <para>Mercurial uses a structure called a <emphasis>manifest</emphasis> to collect
57 together information about the files that it tracks. Each entry in
58 the manifest contains information about the files present in a single
59 changeset. An entry records which files are present in the changeset,
60 the revision of each file, and a few other pieces of file metadata.</para>
62 </sect2>
63 <sect2>
64 <title>Recording changeset information</title>
66 <para>The <emphasis>changelog</emphasis> contains information about each changeset. Each
67 revision records who committed a change, the changeset comment, other
68 pieces of changeset-related information, and the revision of the
69 manifest to use.
70 </para>
72 </sect2>
73 <sect2>
74 <title>Relationships between revisions</title>
76 <para>Within a changelog, a manifest, or a filelog, each revision stores a
77 pointer to its immediate parent (or to its two parents, if it's a
78 merge revision). As I mentioned above, there are also relationships
79 between revisions <emphasis>across</emphasis> these structures, and they are
80 hierarchical in nature.
81 </para>
83 <para>For every changeset in a repository, there is exactly one revision
84 stored in the changelog. Each revision of the changelog contains a
85 pointer to a single revision of the manifest. A revision of the
86 manifest stores a pointer to a single revision of each filelog tracked
87 when that changeset was created. These relationships are illustrated
88 in figure <xref linkend="fig:concepts:metadata"/>.
89 </para>
91 <informalfigure>
93 <para> <mediaobject><imageobject><imagedata fileref="metadata"/></imageobject><textobject><phrase>XXX add text</phrase></textobject></mediaobject>
94 <caption><para>Metadata relationships</para></caption>
95 \label{fig:concepts:metadata}
96 </para>
97 </informalfigure>
99 <para>As the illustration shows, there is <emphasis>not</emphasis> a <quote>one to one</quote>
100 relationship between revisions in the changelog, manifest, or filelog.
101 If the manifest hasn't changed between two changesets, the changelog
102 entries for those changesets will point to the same revision of the
103 manifest. If a file that Mercurial tracks hasn't changed between two
104 changesets, the entry for that file in the two revisions of the
105 manifest will point to the same revision of its filelog.
106 </para>
108 </sect2>
109 </sect1>
110 <sect1>
111 <title>Safe, efficient storage</title>
113 <para>The underpinnings of changelogs, manifests, and filelogs are provided
114 by a single structure called the <emphasis>revlog</emphasis>.
115 </para>
117 <sect2>
118 <title>Efficient storage</title>
120 <para>The revlog provides efficient storage of revisions using a
121 <emphasis>delta</emphasis> mechanism. Instead of storing a complete copy of a file
122 for each revision, it stores the changes needed to transform an older
123 revision into the new revision. For many kinds of file data, these
124 deltas are typically a fraction of a percent of the size of a full
125 copy of a file.
126 </para>
128 <para>Some obsolete revision control systems can only work with deltas of
129 text files. They must either store binary files as complete snapshots
130 or encoded into a text representation, both of which are wasteful
131 approaches. Mercurial can efficiently handle deltas of files with
132 arbitrary binary contents; it doesn't need to treat text as special.
133 </para>
135 </sect2>
136 <sect2>
137 <title>Safe operation</title>
138 <para>\label{sec:concepts:txn}
139 </para>
141 <para>Mercurial only ever <emphasis>appends</emphasis> data to the end of a revlog file.
142 It never modifies a section of a file after it has written it. This
143 is both more robust and efficient than schemes that need to modify or
144 rewrite data.
145 </para>
147 <para>In addition, Mercurial treats every write as part of a
148 <emphasis>transaction</emphasis> that can span a number of files. A transaction is
149 <emphasis>atomic</emphasis>: either the entire transaction succeeds and its effects
150 are all visible to readers in one go, or the whole thing is undone.
151 This guarantee of atomicity means that if you're running two copies of
152 Mercurial, where one is reading data and one is writing it, the reader
153 will never see a partially written result that might confuse it.
154 </para>
156 <para>The fact that Mercurial only appends to files makes it easier to
157 provide this transactional guarantee. The easier it is to do stuff
158 like this, the more confident you should be that it's done correctly.
159 </para>
161 </sect2>
162 <sect2>
163 <title>Fast retrieval</title>
165 <para>Mercurial cleverly avoids a pitfall common to all earlier
166 revision control systems: the problem of <emphasis>inefficient retrieval</emphasis>.
167 Most revision control systems store the contents of a revision as an
168 incremental series of modifications against a <quote>snapshot</quote>. To
169 reconstruct a specific revision, you must first read the snapshot, and
170 then every one of the revisions between the snapshot and your target
171 revision. The more history that a file accumulates, the more
172 revisions you must read, hence the longer it takes to reconstruct a
173 particular revision.
174 </para>
176 <informalfigure>
178 <para> <mediaobject><imageobject><imagedata fileref="snapshot"/></imageobject><textobject><phrase>XXX add text</phrase></textobject></mediaobject>
179 <caption><para>Snapshot of a revlog, with incremental deltas</para></caption>
180 \label{fig:concepts:snapshot}
181 </para>
182 </informalfigure>
184 <para>The innovation that Mercurial applies to this problem is simple but
185 effective. Once the cumulative amount of delta information stored
186 since the last snapshot exceeds a fixed threshold, it stores a new
187 snapshot (compressed, of course), instead of another delta. This
188 makes it possible to reconstruct <emphasis>any</emphasis> revision of a file
189 quickly. This approach works so well that it has since been copied by
190 several other revision control systems.
191 </para>
193 <para>Figure <xref linkend="fig:concepts:snapshot"/> illustrates the idea. In an entry
194 in a revlog's index file, Mercurial stores the range of entries from
195 the data file that it must read to reconstruct a particular revision.
196 </para>
198 <sect3>
199 <title>Aside: the influence of video compression</title>
201 <para>If you're familiar with video compression or have ever watched a TV
202 feed through a digital cable or satellite service, you may know that
203 most video compression schemes store each frame of video as a delta
204 against its predecessor frame. In addition, these schemes use
205 <quote>lossy</quote> compression techniques to increase the compression ratio, so
206 visual errors accumulate over the course of a number of inter-frame
207 deltas.
208 </para>
210 <para>Because it's possible for a video stream to <quote>drop out</quote> occasionally
211 due to signal glitches, and to limit the accumulation of artefacts
212 introduced by the lossy compression process, video encoders
213 periodically insert a complete frame (called a <quote>key frame</quote>) into the
214 video stream; the next delta is generated against that frame. This
215 means that if the video signal gets interrupted, it will resume once
216 the next key frame is received. Also, the accumulation of encoding
217 errors restarts anew with each key frame.
218 </para>
220 </sect3>
221 </sect2>
222 <sect2>
223 <title>Identification and strong integrity</title>
225 <para>Along with delta or snapshot information, a revlog entry contains a
226 cryptographic hash of the data that it represents. This makes it
227 difficult to forge the contents of a revision, and easy to detect
228 accidental corruption.
229 </para>
231 <para>Hashes provide more than a mere check against corruption; they are
232 used as the identifiers for revisions. The changeset identification
233 hashes that you see as an end user are from revisions of the
234 changelog. Although filelogs and the manifest also use hashes,
235 Mercurial only uses these behind the scenes.
236 </para>
238 <para>Mercurial verifies that hashes are correct when it retrieves file
239 revisions and when it pulls changes from another repository. If it
240 encounters an integrity problem, it will complain and stop whatever
241 it's doing.
242 </para>
244 <para>In addition to the effect it has on retrieval efficiency, Mercurial's
245 use of periodic snapshots makes it more robust against partial data
246 corruption. If a revlog becomes partly corrupted due to a hardware
247 error or system bug, it's often possible to reconstruct some or most
248 revisions from the uncorrupted sections of the revlog, both before and
249 after the corrupted section. This would not be possible with a
250 delta-only storage model.
251 </para>
253 <para>\section{Revision history, branching,
254 and merging}
255 </para>
257 <para>Every entry in a Mercurial revlog knows the identity of its immediate
258 ancestor revision, usually referred to as its <emphasis>parent</emphasis>. In fact,
259 a revision contains room for not one parent, but two. Mercurial uses
260 a special hash, called the <quote>null ID</quote>, to represent the idea <quote>there
261 is no parent here</quote>. This hash is simply a string of zeroes.
262 </para>
264 <para>In figure <xref linkend="fig:concepts:revlog"/>, you can see an example of the
265 conceptual structure of a revlog. Filelogs, manifests, and changelogs
266 all have this same structure; they differ only in the kind of data
267 stored in each delta or snapshot.
268 </para>
270 <para>The first revision in a revlog (at the bottom of the image) has the
271 null ID in both of its parent slots. For a <quote>normal</quote> revision, its
272 first parent slot contains the ID of its parent revision, and its
273 second contains the null ID, indicating that the revision has only one
274 real parent. Any two revisions that have the same parent ID are
275 branches. A revision that represents a merge between branches has two
276 normal revision IDs in its parent slots.
277 </para>
279 <informalfigure>
281 <para> <mediaobject><imageobject><imagedata fileref="revlog"/></imageobject><textobject><phrase>XXX add text</phrase></textobject></mediaobject>
282 \caption{}
283 \label{fig:concepts:revlog}
284 </para>
285 </informalfigure>
287 </sect2>
288 </sect1>
289 <sect1>
290 <title>The working directory</title>
292 <para>In the working directory, Mercurial stores a snapshot of the files
293 from the repository as of a particular changeset.
294 </para>
296 <para>The working directory <quote>knows</quote> which changeset it contains. When you
297 update the working directory to contain a particular changeset,
298 Mercurial looks up the appropriate revision of the manifest to find
299 out which files it was tracking at the time that changeset was
300 committed, and which revision of each file was then current. It then
301 recreates a copy of each of those files, with the same contents it had
302 when the changeset was committed.
303 </para>
305 <para>The <emphasis>dirstate</emphasis> contains Mercurial's knowledge of the working
306 directory. This details which changeset the working directory is
307 updated to, and all of the files that Mercurial is tracking in the
308 working directory.
309 </para>
311 <para>Just as a revision of a revlog has room for two parents, so that it
312 can represent either a normal revision (with one parent) or a merge of
313 two earlier revisions, the dirstate has slots for two parents. When
314 you use the <command role="hg-cmd">hg update</command> command, the changeset that you update to
315 is stored in the <quote>first parent</quote> slot, and the null ID in the second.
316 When you <command role="hg-cmd">hg merge</command> with another changeset, the first parent
317 remains unchanged, and the second parent is filled in with the
318 changeset you're merging with. The <command role="hg-cmd">hg parents</command> command tells you
319 what the parents of the dirstate are.
320 </para>
322 <sect2>
323 <title>What happens when you commit</title>
325 <para>The dirstate stores parent information for more than just book-keeping
326 purposes. Mercurial uses the parents of the dirstate as \emph{the
327 parents of a new changeset} when you perform a commit.
328 </para>
330 <informalfigure>
332 <para> <mediaobject><imageobject><imagedata fileref="wdir"/></imageobject><textobject><phrase>XXX add text</phrase></textobject></mediaobject>
333 <caption><para>The working directory can have two parents</para></caption>
334 \label{fig:concepts:wdir}
335 </para>
336 </informalfigure>
338 <para>Figure <xref linkend="fig:concepts:wdir"/> shows the normal state of the working
339 directory, where it has a single changeset as parent. That changeset
340 is the <emphasis>tip</emphasis>, the newest changeset in the repository that has no
341 children.
342 </para>
344 <informalfigure>
346 <para> <mediaobject><imageobject><imagedata fileref="wdir-after-commit"/></imageobject><textobject><phrase>XXX add text</phrase></textobject></mediaobject>
347 <caption><para>The working directory gains new parents after a commit</para></caption>
348 \label{fig:concepts:wdir-after-commit}
349 </para>
350 </informalfigure>
352 <para>It's useful to think of the working directory as <quote>the changeset I'm
353 about to commit</quote>. Any files that you tell Mercurial that you've
354 added, removed, renamed, or copied will be reflected in that
355 changeset, as will modifications to any files that Mercurial is
356 already tracking; the new changeset will have the parents of the
357 working directory as its parents.
358 </para>
360 <para>After a commit, Mercurial will update the parents of the working
361 directory, so that the first parent is the ID of the new changeset,
362 and the second is the null ID. This is shown in
363 figure <xref linkend="fig:concepts:wdir-after-commit"/>. Mercurial doesn't touch
364 any of the files in the working directory when you commit; it just
365 modifies the dirstate to note its new parents.
366 </para>
368 </sect2>
369 <sect2>
370 <title>Creating a new head</title>
372 <para>It's perfectly normal to update the working directory to a changeset
373 other than the current tip. For example, you might want to know what
374 your project looked like last Tuesday, or you could be looking through
375 changesets to see which one introduced a bug. In cases like this, the
376 natural thing to do is update the working directory to the changeset
377 you're interested in, and then examine the files in the working
378 directory directly to see their contents as they were when you
379 committed that changeset. The effect of this is shown in
380 figure <xref linkend="fig:concepts:wdir-pre-branch"/>.
381 </para>
383 <informalfigure>
385 <para> <mediaobject><imageobject><imagedata fileref="wdir-pre-branch"/></imageobject><textobject><phrase>XXX add text</phrase></textobject></mediaobject>
386 <caption><para>The working directory, updated to an older changeset</para></caption>
387 \label{fig:concepts:wdir-pre-branch}
388 </para>
389 </informalfigure>
391 <para>Having updated the working directory to an older changeset, what
392 happens if you make some changes, and then commit? Mercurial behaves
393 in the same way as I outlined above. The parents of the working
394 directory become the parents of the new changeset. This new changeset
395 has no children, so it becomes the new tip. And the repository now
396 contains two changesets that have no children; we call these
397 <emphasis>heads</emphasis>. You can see the structure that this creates in
398 figure <xref linkend="fig:concepts:wdir-branch"/>.
399 </para>
401 <informalfigure>
403 <para> <mediaobject><imageobject><imagedata fileref="wdir-branch"/></imageobject><textobject><phrase>XXX add text</phrase></textobject></mediaobject>
404 <caption><para>After a commit made while synced to an older changeset</para></caption>
405 \label{fig:concepts:wdir-branch}
406 </para>
407 </informalfigure>
409 <note>
410 <para> If you're new to Mercurial, you should keep in mind a common
411 <quote>error</quote>, which is to use the <command role="hg-cmd">hg pull</command> command without any
412 options. By default, the <command role="hg-cmd">hg pull</command> command <emphasis>does not</emphasis>
413 update the working directory, so you'll bring new changesets into
414 your repository, but the working directory will stay synced at the
415 same changeset as before the pull. If you make some changes and
416 commit afterwards, you'll thus create a new head, because your
417 working directory isn't synced to whatever the current tip is.
418 </para>
420 <para> I put the word <quote>error</quote> in quotes because all that you need to do
421 to rectify this situation is <command role="hg-cmd">hg merge</command>, then <command role="hg-cmd">hg commit</command>. In
422 other words, this almost never has negative consequences; it just
423 surprises people. I'll discuss other ways to avoid this behaviour,
424 and why Mercurial behaves in this initially surprising way, later
425 on.
426 </para>
427 </note>
429 </sect2>
430 <sect2>
431 <title>Merging heads</title>
433 <para>When you run the <command role="hg-cmd">hg merge</command> command, Mercurial leaves the first
434 parent of the working directory unchanged, and sets the second parent
435 to the changeset you're merging with, as shown in
436 figure <xref linkend="fig:concepts:wdir-merge"/>.
437 </para>
439 <informalfigure>
441 <para> <mediaobject><imageobject><imagedata fileref="wdir-merge"/></imageobject><textobject><phrase>XXX add text</phrase></textobject></mediaobject>
442 <caption><para>Merging two heads</para></caption>
443 \label{fig:concepts:wdir-merge}
444 </para>
445 </informalfigure>
447 <para>Mercurial also has to modify the working directory, to merge the files
448 managed in the two changesets. Simplified a little, the merging
449 process goes like this, for every file in the manifests of both
450 changesets.
451 </para>
452 <itemizedlist>
453 <listitem><para>If neither changeset has modified a file, do nothing with that
454 file.
455 </para>
456 </listitem>
457 <listitem><para>If one changeset has modified a file, and the other hasn't,
458 create the modified copy of the file in the working directory.
459 </para>
460 </listitem>
461 <listitem><para>If one changeset has removed a file, and the other hasn't (or
462 has also deleted it), delete the file from the working directory.
463 </para>
464 </listitem>
465 <listitem><para>If one changeset has removed a file, but the other has modified
466 the file, ask the user what to do: keep the modified file, or remove
467 it?
468 </para>
469 </listitem>
470 <listitem><para>If both changesets have modified a file, invoke an external
471 merge program to choose the new contents for the merged file. This
472 may require input from the user.
473 </para>
474 </listitem>
475 <listitem><para>If one changeset has modified a file, and the other has renamed
476 or copied the file, make sure that the changes follow the new name
477 of the file.
478 </para>
479 </listitem></itemizedlist>
480 <para>There are more details&emdash;merging has plenty of corner cases&emdash;but
481 these are the most common choices that are involved in a merge. As
482 you can see, most cases are completely automatic, and indeed most
483 merges finish automatically, without requiring your input to resolve
484 any conflicts.
485 </para>
487 <para>When you're thinking about what happens when you commit after a merge,
488 once again the working directory is <quote>the changeset I'm about to
489 commit</quote>. After the <command role="hg-cmd">hg merge</command> command completes, the working
490 directory has two parents; these will become the parents of the new
491 changeset.
492 </para>
494 <para>Mercurial lets you perform multiple merges, but you must commit the
495 results of each individual merge as you go. This is necessary because
496 Mercurial only tracks two parents for both revisions and the working
497 directory. While it would be technically possible to merge multiple
498 changesets at once, the prospect of user confusion and making a
499 terrible mess of a merge immediately becomes overwhelming.
500 </para>
502 </sect2>
503 </sect1>
504 <sect1>
505 <title>Other interesting design features</title>
507 <para>In the sections above, I've tried to highlight some of the most
508 important aspects of Mercurial's design, to illustrate that it pays
509 careful attention to reliability and performance. However, the
510 attention to detail doesn't stop there. There are a number of other
511 aspects of Mercurial's construction that I personally find
512 interesting. I'll detail a few of them here, separate from the <quote>big
513 ticket</quote> items above, so that if you're interested, you can gain a
514 better idea of the amount of thinking that goes into a well-designed
515 system.
516 </para>
518 <sect2>
519 <title>Clever compression</title>
521 <para>When appropriate, Mercurial will store both snapshots and deltas in
522 compressed form. It does this by always <emphasis>trying to</emphasis> compress a
523 snapshot or delta, but only storing the compressed version if it's
524 smaller than the uncompressed version.
525 </para>
527 <para>This means that Mercurial does <quote>the right thing</quote> when storing a file
528 whose native form is compressed, such as a <literal>zip</literal> archive or a
529 JPEG image. When these types of files are compressed a second time,
530 the resulting file is usually bigger than the once-compressed form,
531 and so Mercurial will store the plain <literal>zip</literal> or JPEG.
532 </para>
534 <para>Deltas between revisions of a compressed file are usually larger than
535 snapshots of the file, and Mercurial again does <quote>the right thing</quote> in
536 these cases. It finds that such a delta exceeds the threshold at
537 which it should store a complete snapshot of the file, so it stores
538 the snapshot, again saving space compared to a naive delta-only
539 approach.
540 </para>
542 <sect3>
543 <title>Network recompression</title>
545 <para>When storing revisions on disk, Mercurial uses the <quote>deflate</quote>
546 compression algorithm (the same one used by the popular <literal>zip</literal>
547 archive format), which balances good speed with a respectable
548 compression ratio. However, when transmitting revision data over a
549 network connection, Mercurial uncompresses the compressed revision
550 data.
551 </para>
553 <para>If the connection is over HTTP, Mercurial recompresses the entire
554 stream of data using a compression algorithm that gives a better
555 compression ratio (the Burrows-Wheeler algorithm from the widely used
556 <literal>bzip2</literal> compression package). This combination of algorithm
557 and compression of the entire stream (instead of a revision at a time)
558 substantially reduces the number of bytes to be transferred, yielding
559 better network performance over almost all kinds of network.
560 </para>
562 <para>(If the connection is over <command>ssh</command>, Mercurial <emphasis>doesn't</emphasis>
563 recompress the stream, because <command>ssh</command> can already do this
564 itself.)
565 </para>
567 </sect3>
568 </sect2>
569 <sect2>
570 <title>Read/write ordering and atomicity</title>
572 <para>Appending to files isn't the whole story when it comes to guaranteeing
573 that a reader won't see a partial write. If you recall
574 figure <xref linkend="fig:concepts:metadata"/>, revisions in the changelog point to
575 revisions in the manifest, and revisions in the manifest point to
576 revisions in filelogs. This hierarchy is deliberate.
577 </para>
579 <para>A writer starts a transaction by writing filelog and manifest data,
580 and doesn't write any changelog data until those are finished. A
581 reader starts by reading changelog data, then manifest data, followed
582 by filelog data.
583 </para>
585 <para>Since the writer has always finished writing filelog and manifest data
586 before it writes to the changelog, a reader will never read a pointer
587 to a partially written manifest revision from the changelog, and it will
588 never read a pointer to a partially written filelog revision from the
589 manifest.
590 </para>
592 </sect2>
593 <sect2>
594 <title>Concurrent access</title>
596 <para>The read/write ordering and atomicity guarantees mean that Mercurial
597 never needs to <emphasis>lock</emphasis> a repository when it's reading data, even
598 if the repository is being written to while the read is occurring.
599 This has a big effect on scalability; you can have an arbitrary number
600 of Mercurial processes safely reading data from a repository safely
601 all at once, no matter whether it's being written to or not.
602 </para>
604 <para>The lockless nature of reading means that if you're sharing a
605 repository on a multi-user system, you don't need to grant other local
606 users permission to <emphasis>write</emphasis> to your repository in order for them
607 to be able to clone it or pull changes from it; they only need
608 <emphasis>read</emphasis> permission. (This is <emphasis>not</emphasis> a common feature among
609 revision control systems, so don't take it for granted! Most require
610 readers to be able to lock a repository to access it safely, and this
611 requires write permission on at least one directory, which of course
612 makes for all kinds of nasty and annoying security and administrative
613 problems.)
614 </para>
616 <para>Mercurial uses locks to ensure that only one process can write to a
617 repository at a time (the locking mechanism is safe even over
618 filesystems that are notoriously hostile to locking, such as NFS). If
619 a repository is locked, a writer will wait for a while to retry if the
620 repository becomes unlocked, but if the repository remains locked for
621 too long, the process attempting to write will time out after a while.
622 This means that your daily automated scripts won't get stuck forever
623 and pile up if a system crashes unnoticed, for example. (Yes, the
624 timeout is configurable, from zero to infinity.)
625 </para>
627 <sect3>
628 <title>Safe dirstate access</title>
630 <para>As with revision data, Mercurial doesn't take a lock to read the
631 dirstate file; it does acquire a lock to write it. To avoid the
632 possibility of reading a partially written copy of the dirstate file,
633 Mercurial writes to a file with a unique name in the same directory as
634 the dirstate file, then renames the temporary file atomically to
635 <filename>dirstate</filename>. The file named <filename>dirstate</filename> is thus
636 guaranteed to be complete, not partially written.
637 </para>
639 </sect3>
640 </sect2>
641 <sect2>
642 <title>Avoiding seeks</title>
644 <para>Critical to Mercurial's performance is the avoidance of seeks of the
645 disk head, since any seek is far more expensive than even a
646 comparatively large read operation.
647 </para>
649 <para>This is why, for example, the dirstate is stored in a single file. If
650 there were a dirstate file per directory that Mercurial tracked, the
651 disk would seek once per directory. Instead, Mercurial reads the
652 entire single dirstate file in one step.
653 </para>
655 <para>Mercurial also uses a <quote>copy on write</quote> scheme when cloning a
656 repository on local storage. Instead of copying every revlog file
657 from the old repository into the new repository, it makes a <quote>hard
658 link</quote>, which is a shorthand way to say <quote>these two names point to the
659 same file</quote>. When Mercurial is about to write to one of a revlog's
660 files, it checks to see if the number of names pointing at the file is
661 greater than one. If it is, more than one repository is using the
662 file, so Mercurial makes a new copy of the file that is private to
663 this repository.
664 </para>
666 <para>A few revision control developers have pointed out that this idea of
667 making a complete private copy of a file is not very efficient in its
668 use of storage. While this is true, storage is cheap, and this method
669 gives the highest performance while deferring most book-keeping to the
670 operating system. An alternative scheme would most likely reduce
671 performance and increase the complexity of the software, each of which
672 is much more important to the <quote>feel</quote> of day-to-day use.
673 </para>
675 </sect2>
676 <sect2>
677 <title>Other contents of the dirstate</title>
679 <para>Because Mercurial doesn't force you to tell it when you're modifying a
680 file, it uses the dirstate to store some extra information so it can
681 determine efficiently whether you have modified a file. For each file
682 in the working directory, it stores the time that it last modified the
683 file itself, and the size of the file at that time.
684 </para>
686 <para>When you explicitly <command role="hg-cmd">hg add</command>, <command role="hg-cmd">hg remove</command>, <command role="hg-cmd">hg rename</command> or
687 <command role="hg-cmd">hg copy</command> files, Mercurial updates the dirstate so that it knows
688 what to do with those files when you commit.
689 </para>
691 <para>When Mercurial is checking the states of files in the working
692 directory, it first checks a file's modification time. If that has
693 not changed, the file must not have been modified. If the file's size
694 has changed, the file must have been modified. If the modification
695 time has changed, but the size has not, only then does Mercurial need
696 to read the actual contents of the file to see if they've changed.
697 Storing these few extra pieces of information dramatically reduces the
698 amount of data that Mercurial needs to read, which yields large
699 performance improvements compared to other revision control systems.
700 </para>
702 </sect2>
703 </sect1>
704 </chapter>
706 <!--
707 local variables:
708 sgml-parent-document: ("00book.xml" "book" "chapter")
709 end:
710 -->