Commit Graph

83 Commits

Author SHA1 Message Date
Jun Wu
b82b0daab5 indexedlog: make LinkOffset also return next link offset
Summary:
Previously, the code was focusing on getting the hardest (index) part right,
but less about the value part. There is no way to get all values in the
linked list, as designed, yet. This diff starts the work.

Similar to `KeyOffset::key_and_link_offset`, change the internal API of
LinkOffset to return both value and the next link offset.

Reviewed By: DurhamG

Differential Revision: D7472879

fbshipit-source-id: 4a4512d7c63abbb667146de582e0f8cd04c9c04a
2018-04-13 21:51:46 -07:00
Jun Wu
b9b1f1e907 indexedlog: use OpenOptions
Summary:
`Index::open` now takes too many parameters, which is not very convenient to
use. Inspired by `fs::OpenOptions`, use a dedicated strut for specifying
open options.

Motivation: To test checksum ability more confidently, I'd like to write
something that randomly mutates 1 byte from a sane index. To make sure the
checksum coverage is "correct", checksum chunk size is another parameter.

Reviewed By: DurhamG

Differential Revision: D7464182

fbshipit-source-id: 469ce7d1cfa5de3946028418567a9f3e2bc303fa
2018-04-13 21:51:46 -07:00
Jun Wu
6cb2b1dd23 indexedlog: make OffsetMap::get have no assumption about offset
Summary:
Address DurhamG's review comment on D7422832.

Previously, `OffsetMap::get` expects a dirty offset. That's because it was
changed from `HashMap` and we don't control `HashMap::get`. It's cleaner to
let `OffsetMap` do the `is_dirty` check.

Reviewed By: DurhamG

Differential Revision: D7461707

fbshipit-source-id: 9f2abdf6c6f993d98d9443f16bafcc6154ee0dbb
2018-04-13 21:51:46 -07:00
Jun Wu
9787cfc15b indexedlog: add more tests about leaf split
Summary:
The new test covers the `else` branch inside `LeafOffset::set_link`
previously not covered.

Coverage was checked by the following script:

```
from __future__ import absolute_import

import glob
import os
import shutil

os.system('cargo rustc --lib --profile test -- -Ccodegen-units=1 -Clink-dead-code -Zno-landing-pads')
path = max((os.stat(path).st_mtime, path) for path in glob.glob('./target/debug/*-????????????????'))[1]
shutil.rmtree('target/kcov')
os.system('kcov --include-path $PWD/src --verify target/kcov %s' % path)
```

Reviewed By: DurhamG

Differential Revision: D7446902

fbshipit-source-id: 293da2ff53b83c8f11534f0f8e5e7fd102216a01
2018-04-13 21:51:46 -07:00
Jun Wu
5209e8360b indexedlog: support external keys
Summary:
Change `insert_advanced` to accept an enum that could be either a key, or an
(offset, len) that refers to the external key buffer.

Insertion becomes slower due to new flexibility overhead.  For some reason,
"index lookup (no verify)" becomes faster (restores pre-D7440248 performance):

  index insertion                 6.434 ms
  index flush                     3.757 ms
  index lookup (memory)           1.068 ms
  index lookup (disk, no verify)  1.969 ms
  index lookup (disk, verified)   7.805 ms

With 2M 20-byte keys, the non-external key version generates a 105MB index:

  seconds operation
  1.247   insert
  0.622   flush
  1.859   flush done
  0.702   lookup (without checksum)
  1.395   lookup (with checksum)

Using external keys,the index is 70MB, and time for each operation:

  seconds operation
  1.086   insert
  0.702   flush
  0.665   lookup (without checksums)
  1.602   lookup (with checksums)

The external key will have more space wins for longer keys, ex. file path.

`Index` module was made public so `InsertKey` type is usable.

Reviewed By: DurhamG

Differential Revision: D7444907

fbshipit-source-id: b89d95246845799c2c55fb73ad203a7e6724b85e
2018-04-13 21:51:46 -07:00
Jun Wu
36dfda984c indexedlog: relax leaf entry's key offset type
Summary:
Previously, a leaf entry can only have a `KeyOffset`. This diff makes it
possible to be either `KeyOffset`, or `ExtKeyOffset`. The API didn't change
much since `LeafOffset::key_and_link_offset` handles the difference
transparently.

Latest benchmark result:

  index insertion                 4.879 ms
  index flush                     3.620 ms
  index lookup (memory)           1.827 ms
  index lookup (disk, no verify)  3.508 ms
  index lookup (disk, verified)   7.861 ms

Reviewed By: DurhamG

Differential Revision: D7444909

fbshipit-source-id: 5441e1ae187d42931377d7213dcb77156b2af714
2018-04-13 21:51:46 -07:00
Jun Wu
44a0998bc6 indexedlog: let leaf entry return key content
Summary:
The leaf entry has a `key_and_link_offset` method. Previously it returns a
`KeyOffset`, since we now have `ExtKeyOffset`, it's friendly to handle the
key entry type difference at the leaf entry level, instead of requiring the
caller to handle it.

Reviewed By: DurhamG

Differential Revision: D7444905

fbshipit-source-id: 56d87641a2a5a50ddca8b1e4c74c9aaa3891b542
2018-04-13 21:51:46 -07:00
Jun Wu
1294c1b471 indexedlog: add an "external key" entry type
Summary:
Previously, I thought there is only one index that will use "commit hash" as
keys, that is the nodemap, and other indexes (like childmap) would just use
shorter integer keys (ex. revision number, or offsets). So the space overhead
of storing full keys only applies to one index and seems acceptable.

But that implies strict topo order for the source of truth data (ex. to use
integers as keys in childmap, you have to know how to translate parent
revisions from hashes to integers at the time writing the revision).

Thinking about it again, it seems the topo-order requirement would make a lot
of things less flexible. It's much easier to just use hashes as keys in the
index. Then it's worthwhile to address the space efficiency problem by
introducing an "external key buffer" concept. That's actually what `radixbuf`
does.

This is the start. It adds the type to the strcut. The feature is not completed
yet.

Reviewed By: DurhamG

Differential Revision: D7444904

fbshipit-source-id: 60a83c9e6e8b0734450f0c5827928a7c5bd111d5
2018-04-13 21:51:45 -07:00
Jun Wu
5e828307f4 indexedlog: verify checksum for all reads
Summary:
It further slows down lookups, even when checksum is disabled, since even a
`is_none()` check is not free:

  index insertion                 4.697 ms
  index flush                     3.764 ms
  index lookup (memory)           2.878 ms
  index lookup (disk, no verify)  3.564 ms
  index lookup (disk, verified)   7.788 ms

The "verified" version basically needs 2x time due to more memory lookups.

Unfortunately this means eventual lookup performance will be slower than
gdbm, but insertion is still much faster. And the index still has a better
locking properties (lock-free read) that gdbm does not have.

With correct time complexity (no O(len(changelog)) index-only operations for
example), I'd expect it's rare for the overall performance to be bounded by
index performance. Data integrity is more important.

With a larger number of nodes, ex. 2M 20-byte strings: inserting to memory
takes 1.4 seconds, flushing to disk takes 0.9 seconds, looking up without
checksum takes 0.9 seconds, looking up with checksum takes 1.7 seconds.

Reviewed By: DurhamG

Differential Revision: D7440248

fbshipit-source-id: 020e5204606f9f0a4f68843a491009a6a6f75751
2018-04-13 21:51:42 -07:00
Jun Wu
ca8f60eb0a indexedlog: verify checksum for type bytes
Summary:
This is in the critical path for lookup, and has very visible performance
penalty:

  index insertion                 3.923 ms
  index flush                     3.921 ms
  index lookup (memory)           1.070 ms
  index lookup (disk, no verify)  1.980 ms
  index lookup (disk, verified)   5.206 ms

Reviewed By: DurhamG

Differential Revision: D7440252

fbshipit-source-id: 49540f974faff1cdd0603a72328f141ccd054ee2
2018-04-13 21:51:42 -07:00
Jun Wu
55fc90dfea indexedlog: verify checksum for Mem* structs
Summary:
Previously checksum is only for `MemRoot`, now it's for all `Mem` structs.
Since `Mem*` structs are not frequently used in the normal lookup code path,
there is no visible performance change.

Reviewed By: DurhamG

Differential Revision: D7440253

fbshipit-source-id: 945f5a8c38d228f59190a487b0cf6dbc5daac4f7
2018-04-13 21:51:42 -07:00
Jun Wu
a7e3e7884d indexedlog: add a type alias for Option<ChecksumTable>
Summary:
The type will be used all over the place and may make `rustfmt` wrap lines.
Use a shorter type to make it slightly cleaner.

Reviewed By: DurhamG

Differential Revision: D7436338

fbshipit-source-id: ecaada23916a22658f65669b748632a077e60df2
2018-04-13 21:51:42 -07:00
Jun Wu
bfd8e33370 indexedlog: verify checksum for root entry
Summary:
This only affects `Index::open` right now. So it's a one time check and does
not affect performance.

Reviewed By: DurhamG

Differential Revision: D7436341

fbshipit-source-id: 30313064bf2ea50320ac744fc18c03bff4b12c89
2018-04-13 21:51:42 -07:00
Jun Wu
a0cec9853c indexedlog: add checksum table to index struct
Summary:
Add `ChecksumTable` to the `Index` struct. But it's not functional yet.
The checksum will mainly affect "index lookup (disk)" case. Add another
benchmark for showing the difference with checksum on and off. They do not
have much difference right now:

  index insertion                 3.756 ms
  index flush                     3.469 ms
  index lookup (memory)           0.990 ms
  index lookup (disk, no verify)  1.768 ms
  index lookup (disk, verified)   1.766 ms

Reviewed By: DurhamG

Differential Revision: D7436339

fbshipit-source-id: 60a6554a2c96067a53ce9e1753cd51d0d61c0bea
2018-04-13 21:51:42 -07:00
Jun Wu
8d7d4de8ee indexedlog: separate benchmarks
Summary:
The minibench framework does not provide benchmark filtering. So let's
separate benchmarks using different entry points.

Reviewed By: DurhamG

Differential Revision: D7440250

fbshipit-source-id: 11e7790a5074ebf4c08e33c312a490a66a921926
2018-04-13 21:51:42 -07:00
Jun Wu
d86adc417e indexedlog: remove "index clone" benchmarks
Summary:
The "clone" benchmarks were added to be subtracted from "lookup" to
workaround the test framework limitation.

The new minibench framework makes it easier to exclude preparation cost.
Therefore the clone benchmarks are no longer needed.

  index insertion                 3.881 ms
  index flush                     3.286 ms
  index lookup (memory)           0.928 ms
  index lookup (disk)             1.685 ms

"index lookup (memory)" is basically "index lookup (memory)" minus
"index clone (memory)" in previous benchmarks.

Reviewed By: DurhamG

Differential Revision: D7440251

fbshipit-source-id: 0e6a1fb7ee64f9a393ee9ada4db6e6eb052e20bf
2018-04-13 21:51:42 -07:00
Jun Wu
9b9dd289e4 indexedlog: use minibench to do benchmark
Summary:
See the previous minibench diff for the motivation.

"failure" was removed from build dependencies since it's not used yet.

Run benchmark a few times. It seems the first several items are less stable
due to possibly warming up issues. Otherwise the result looks good enough.
The test also compiles and runs much faster.

```
base16 iterating 1M bytes       0.921 ms
index insertion                 4.804 ms
index flush                     5.104 ms
index lookup (memory)           2.929 ms
index lookup (disk)             1.767 ms
index clone (memory)            2.036 ms
index clone (disk)              0.010 ms

base16 iterating 1M bytes       0.853 ms
index insertion                 4.512 ms
index flush                     4.717 ms
index lookup (memory)           2.907 ms
index lookup (disk)             1.755 ms
index clone (memory)            1.856 ms
index clone (disk)              0.010 ms

base16 iterating 1M bytes       1.525 ms
index insertion                 4.577 ms
index flush                     4.901 ms
index lookup (memory)           2.800 ms
index lookup (disk)             1.790 ms
index clone (memory)            1.794 ms
index clone (disk)              0.010 ms

base16 iterating 1M bytes       0.768 ms
index insertion                 4.486 ms
index flush                     4.918 ms
index lookup (memory)           2.658 ms
index lookup (disk)             1.721 ms
index clone (memory)            1.763 ms
index clone (disk)              0.010 ms

base16 iterating 1M bytes       0.732 ms
index insertion                 4.489 ms
index flush                     4.792 ms
index lookup (memory)           2.689 ms
index lookup (disk)             1.739 ms
index clone (memory)            1.850 ms
index clone (disk)              0.009 ms

base16 iterating 1M bytes       1.124 ms
index insertion                 7.188 ms
index flush                     4.888 ms
index lookup (memory)           2.829 ms
index lookup (disk)             1.609 ms
index clone (memory)            2.642 ms
index clone (disk)              0.010 ms

base16 iterating 1M bytes       1.055 ms
index insertion                 4.683 ms
index flush                     4.996 ms
index lookup (memory)           2.782 ms
index lookup (disk)             1.710 ms
index clone (memory)            1.802 ms
index clone (disk)              0.009 ms
```

Reviewed By: DurhamG

Differential Revision: D7440249

fbshipit-source-id: 0f946ab184455acd40c5a38cf46ff94d9e3755c8
2018-04-13 21:51:42 -07:00
Jun Wu
f9fb60337a minibench: add a simple library to do benchmark
Summary:
It's sad to find that existing Rust benchmark frameworks do not fit well in
our simple benchmark purpose. The benchmark library shipped with Rust [1] has
been in "nightly-only" for long. Third-party choices like "criterion.rs" does
too many things and misses certain small features. Namely, indexedlog wants:

  - More stable benchmark result. This means not picking the average time,
    but the "best" time among all runs, like what Mercurial does.
  - Do not measure setup cost from repetitive runs. As in D7404532, do not
    clone the index, and do not have separate "clone" benchmarks.
  - Faster benchmarks. This means getting rid of unused parts like calling
    gnuplot.

Besides, having the test framework to be lightweight also helps compilation
time. Looking at `indexedlog`'s dependencies (with unused "failure"
removed), 70% of them are from `criterion.rs`.

```
indexedlog v0.1.0 (lib/indexedlog)
[dependencies]
|-- atomicwrites v0.1.5
|   [dependencies]
|   |-- nix v0.9.0
|   |   [dependencies]
|   |   |-- bitflags v0.9.1
|   |   |-- cfg-if v0.1.2
|   |   |-- libc v0.2.39
|   |   `-- void v1.0.2
|   `-- tempdir v0.3.6
|       [dependencies]
|       |-- rand v0.4.2
|       |   [dependencies]
|       |   `-- libc v0.2.39 (*)
|       `-- remove_dir_all v0.3.0
|           [dependencies]
|           |-- kernel32-sys v0.2.2
|           |   [dependencies]
|           |   `-- winapi v0.2.8
|           |   [build-dependencies]
|           |   `-- winapi-build v0.1.1
|           `-- winapi v0.2.8 (*)
|-- byteorder v1.2.1
|-- fs2 v0.4.3
|   [dependencies]
|   `-- libc v0.2.39 (*)
|-- memmap v0.6.2
|   [dependencies]
|   `-- libc v0.2.39 (*)
|-- twox-hash v1.1.0
|   [dependencies]
|   `-- rand v0.3.22
|       [dependencies]
|       |-- libc v0.2.39 (*)
|       `-- rand v0.4.2 (*)
`-- vlqencoding v0.1.0 (lib/vlqencoding)
[dev-dependencies]
|-- criterion v0.2.1
|   [dependencies]
|   |-- atty v0.2.8
|   |   [dependencies]
|   |   `-- libc v0.2.39 (*)
|   |-- clap v2.31.1
|   |   [dependencies]
|   |   |-- ansi_term v0.11.0
|   |   |-- atty v0.2.8 (*)
|   |   |-- bitflags v1.0.1
|   |   |-- strsim v0.7.0
|   |   |-- textwrap v0.9.0
|   |   |   [dependencies]
|   |   |   `-- unicode-width v0.1.4
|   |   |-- unicode-width v0.1.4 (*)
|   |   `-- vec_map v0.8.0
|   |-- criterion-plot v0.2.1
|   |   [dependencies]
|   |   |-- byteorder v1.2.1 (*)
|   |   |-- cast v0.2.2
|   |   `-- itertools v0.7.7
|   |       [dependencies]
|   |       `-- either v1.4.0
|   |-- criterion-stats v0.2.1
|   |   [dependencies]
|   |   |-- cast v0.2.2 (*)
|   |   |-- num-traits v0.2.1
|   |   |-- num_cpus v1.8.0
|   |   |   [dependencies]
|   |   |   `-- libc v0.2.39 (*)
|   |   |-- rand v0.4.2 (*)
|   |   `-- thread-scoped v1.0.2
|   |-- failure v0.1.1
|   |   [dependencies]
|   |   |-- backtrace v0.3.5
|   |   |   [dependencies]
|   |   |   |-- backtrace-sys v0.1.16
|   |   |   |   [dependencies]
|   |   |   |   `-- libc v0.2.39 (*)
|   |   |   |   [build-dependencies]
|   |   |   |   `-- cc v1.0.8
|   |   |   |-- cfg-if v0.1.2 (*)
|   |   |   |-- libc v0.2.39 (*)
|   |   |   `-- rustc-demangle v0.1.7
|   |   `-- failure_derive v0.1.1
|   |       [dependencies]
|   |       |-- quote v0.3.15
|   |       |-- syn v0.11.11
|   |       |   [dependencies]
|   |       |   |-- quote v0.3.15 (*)
|   |       |   |-- synom v0.11.3
|   |       |   |   [dependencies]
|   |       |   |   `-- unicode-xid v0.0.4
|   |       |   `-- unicode-xid v0.0.4 (*)
|   |       `-- synstructure v0.6.1
|   |           [dependencies]
|   |           |-- quote v0.3.15 (*)
|   |           `-- syn v0.11.11 (*)
|   |-- failure_derive v0.1.1 (*)
|   |-- handlebars v0.31.0
|   |   [dependencies]
|   |   |-- lazy_static v1.0.0
|   |   |-- log v0.4.1
|   |   |   [dependencies]
|   |   |   `-- cfg-if v0.1.2 (*)
|   |   |-- pest v1.0.6
|   |   |-- pest_derive v1.0.6
|   |   |   [dependencies]
|   |   |   |-- pest v1.0.6 (*)
|   |   |   |-- quote v0.3.15 (*)
|   |   |   `-- syn v0.11.11 (*)
|   |   |-- quick-error v1.2.1
|   |   |-- regex v0.2.10
|   |   |   [dependencies]
|   |   |   |-- aho-corasick v0.6.4
|   |   |   |   [dependencies]
|   |   |   |   `-- memchr v2.0.1
|   |   |   |       [dependencies]
|   |   |   |       `-- libc v0.2.39 (*)
|   |   |   |-- memchr v2.0.1 (*)
|   |   |   |-- regex-syntax v0.5.3
|   |   |   |   [dependencies]
|   |   |   |   `-- ucd-util v0.1.1
|   |   |   |-- thread_local v0.3.5
|   |   |   |   [dependencies]
|   |   |   |   |-- lazy_static v1.0.0 (*)
|   |   |   |   `-- unreachable v1.0.0
|   |   |   |       [dependencies]
|   |   |   |       `-- void v1.0.2 (*)
|   |   |   `-- utf8-ranges v1.0.0
|   |   |-- serde v1.0.33
|   |   `-- serde_json v1.0.11
|   |       [dependencies]
|   |       |-- dtoa v0.4.2
|   |       |-- itoa v0.3.4
|   |       |-- num-traits v0.2.1 (*)
|   |       `-- serde v1.0.33 (*)
|   |-- itertools v0.7.7 (*)
|   |-- itertools-num v0.1.1
|   |   [dependencies]
|   |   `-- num-traits v0.1.43
|   |       [dependencies]
|   |       `-- num-traits v0.2.1 (*)
|   |-- log v0.4.1 (*)
|   |-- serde v1.0.33 (*)
|   |-- serde_derive v1.0.33
|   |   [dependencies]
|   |   |-- proc-macro2 v0.2.3
|   |   |   [dependencies]
|   |   |   `-- unicode-xid v0.1.0
|   |   |-- quote v0.4.2
|   |   |   [dependencies]
|   |   |   `-- proc-macro2 v0.2.3 (*)
|   |   |-- serde_derive_internals v0.21.0
|   |   |   [dependencies]
|   |   |   |-- proc-macro2 v0.2.3 (*)
|   |   |   `-- syn v0.12.14
|   |   |       [dependencies]
|   |   |       |-- proc-macro2 v0.2.3 (*)
|   |   |       |-- quote v0.4.2 (*)
|   |   |       `-- unicode-xid v0.1.0 (*)
|   |   `-- syn v0.12.14 (*)
|   |-- serde_json v1.0.11 (*)
|   `-- simplelog v0.5.0
|       [dependencies]
|       |-- chrono v0.4.0
|       |   [dependencies]
|       |   |-- num v0.1.42
|       |   |   [dependencies]
|       |   |   |-- num-integer v0.1.36
|       |   |   |   [dependencies]
|       |   |   |   `-- num-traits v0.2.1 (*)
|       |   |   |-- num-iter v0.1.35
|       |   |   |   [dependencies]
|       |   |   |   |-- num-integer v0.1.36 (*)
|       |   |   |   `-- num-traits v0.2.1 (*)
|       |   |   `-- num-traits v0.2.1 (*)
|       |   `-- time v0.1.39
|       |       [dependencies]
|       |       `-- libc v0.2.39 (*)
|       |       [dev-dependencies]
|       |       `-- winapi v0.3.4
|       |-- log v0.4.1 (*)
|       `-- term v0.4.6
|-- quickcheck v0.6.2
|   [dependencies]
|   |-- env_logger v0.5.6
|   |   [dependencies]
|   |   |-- atty v0.2.8 (*)
|   |   |-- humantime v1.1.1
|   |   |   [dependencies]
|   |   |   `-- quick-error v1.2.1 (*)
|   |   |-- log v0.4.1 (*)
|   |   |-- regex v0.2.10 (*)
|   |   `-- termcolor v0.3.5
|   |-- log v0.4.1 (*)
|   `-- rand v0.4.2 (*)
|-- rand v0.4.2 (*)
`-- tempdir v0.3.6 (*)
```

[1]: https://github.com/rust-lang/rust/issues/29553

Reviewed By: DurhamG

Differential Revision: D7440254

fbshipit-source-id: 53cdbd470945388db96702ab771a3f73b456da37
2018-04-13 21:51:42 -07:00
Jun Wu
8bcff92cab indexedlog: use a dedicated map type for offset translation
Summary:
The dirty -> non-dirty offset mapping can be optimized using a dedicated
"map" type that is backed by `vec`s, because dirty offsets are continuous
per type.

This makes "flush" significantly faster:

```
index flush             time:   [5.8808 ms 6.1800 ms 6.4813 ms]
                        change: [-62.250% -59.481% -56.325%] (p = 0.00 < 0.05)
                        Performance has improved.
```

Reviewed By: DurhamG

Differential Revision: D7422832

fbshipit-source-id: 9ab8a70d1663155941dae5b4f02f7452f5e3cadf
2018-04-13 21:51:42 -07:00
Jun Wu
00503a6d94 indexedlog: avoid a memory allocation
Summary:
It seems to improve the performance a bit:

```
index insertion         time:   [5.4643 ms 5.6818 ms 5.9188 ms]
                        change: [-24.526% -17.384% -10.315%] (p = 0.00 < 0.05)
                        Performance has improved.
```

Reviewed By: DurhamG

Differential Revision: D7422831

fbshipit-source-id: fc1c72f402258db7e189cd8724583757d48affb7
2018-04-13 21:51:42 -07:00
Jun Wu
4cb2cc1abb indexedlog: use Box<[u8]> instead of Vec<u8>
Summary:
For key entries, the key is immutable once stored. So just use `Box<[u8]>`.
It saves a `usize` per entry. On 64-bit platform, that's a lot.

Performance is slightly improved and it catches up with D7404532 before
typed offset refactoring now:

  index insertion         time:   [6.1852 ms 6.6598 ms 7.2433 ms]
  index flush             time:   [15.814 ms 16.538 ms 17.235 ms]
  index lookup (memory)   time:   [3.7636 ms 3.9403 ms 4.1424 ms]
  index lookup (disk)     time:   [1.9413 ms 2.0366 ms 2.1325 ms]
  index clone (memory)    time:   [2.6952 ms 2.9221 ms 3.0968 ms]
  index clone (disk)      time:   [5.0296 us 5.2862 us 5.5629 us]

Reviewed By: DurhamG

Differential Revision: D7422837

fbshipit-source-id: 4aabfdc028aefb8e796803e103f0b2e4965f84e6
2018-04-13 21:51:42 -07:00
Jun Wu
36793b7c14 indexedlog: simplify insert_advanced API
Summary:
Previously, both `value` and `link` are optional in `insert_advanced`.
This diff makes `value` required.

`maybe_create_link_entry` becomes unused and removed.

No visible performance change.

Reviewed By: DurhamG

Differential Revision: D7422838

fbshipit-source-id: 8d7d3cc1cc325f6fea7e8ce996d0a43d3ee49839
2018-04-13 21:51:41 -07:00
Jun Wu
892fcd6dfd indexedlog: use typed offsets
Summary:
This is a large refactoring that replaces `u64` offsets with strong typed
ones.

Tests about serialization are removed since they generate illegal data that
cannot pass type check.

It seems to slow down the code a bit, comparing with D7404532. But there are
still room to improve.

  index insertion         time:   [6.9395 ms 7.3863 ms 7.7620 ms]
  index flush             time:   [15.949 ms 17.965 ms 20.246 ms]
  index lookup (memory)   time:   [3.6212 ms 3.8855 ms 4.1923 ms]
  index lookup (disk)     time:   [2.2496 ms 2.4649 ms 2.8090 ms]
  index clone (memory)    time:   [2.7292 ms 2.9399 ms 3.2055 ms]
  index clone (disk)      time:   [4.9239 us 5.5928 us 6.3167 us]

Reviewed By: DurhamG

Differential Revision: D7422833

fbshipit-source-id: 7357cb0f4f573f620e829c5e300cd423619dbd62
2018-04-13 21:51:41 -07:00
Jun Wu
283b8d130d pathmatcher: initial Rust matcher that handles gitignore lazily
Summary:
The "pathmatcher" crate is intended to eventually cover more "matcher"
abilities so all Python "matcher" related logic can be handled by Rust.
For now, it only contains a gitignore matcher.

The gitignore matcher is designed to work in a repo (no need to create
multiple gitignore matchers for a repo from a higher layer), and be lazy
i.e. be tree-aware, and do not parse ".gitignore" unless necessary.

Worth mentioning that the gitignore logic provided by the "ignore" crate
seems decent in time complexity - it uses regular expression, which uses state
machines to achieve "testing against multiple patterns at once", instead of
testing patterns one-by-one like what git currently does.

Note: The "ignore" crate provides a nice "Walker" interface but that does
not fit very well with the required laziness here. So the walker interface
is not used.

Reviewed By: markbt

Differential Revision: D7319609

fbshipit-source-id: ebd131adf45a38f83acdf653f5e49d0624012152
2018-04-13 21:51:40 -07:00
Jun Wu
a87fea077c indexedlog: prefix in-memory entries with Mem
Summary: This makes it clear the code has different code paths for on-disk entries.

Reviewed By: DurhamG

Differential Revision: D7422836

fbshipit-source-id: 018fa0e2c20682d4e1beba99f3307550e1f40388
2018-04-13 21:51:40 -07:00
Jun Wu
3332522d43 indexedlog: add some benchmarks
Summary:
Add benchmarks inserting / looking up 20K entries.

Benchmark results on my laptop are:

  index insertion         time:   [6.5339 ms 6.8174 ms 7.1805 ms]
  index flush             time:   [15.651 ms 16.103 ms 16.537 ms]
  index lookup (memory)   time:   [3.6995 ms 4.0252 ms 4.3046 ms]
  index lookup (disk)     time:   [1.9986 ms 2.1224 ms 2.2464 ms]
  index clone (memory)    time:   [2.5943 ms 2.6866 ms 2.7749 ms]
  index clone (disk)      time:   [5.2302 us 5.5477 us 5.9518 us]

Comparing with highly optimized radixbuf:

  index insertion         time:   [991.89 us 1.1708 ms 1.3844 ms]
  index lookup            time:   [863.83 us 945.69 us 1.0304 ms]

Insertion takes 6x time. Lookup from memory takes 1.4x time, from disk takes
2.2x time. Flushing is the slowest - it needs 16x radixbuf insertion time.

Note: need to subtract "clone" time from "lookup" to get meaningful values
about "lookup". This cannot be done automatically due to the limitation of the
benchmark framework.

Although it's slower than radixbuf, the index is still faster than gdbm and
rocksdb. Note: the index does less than gdbm/rocksdb since it does not return
a `[u8]`-ish which requires extra lookups. So it's not a very fair comparison.

  gdbm insertion          time:   [69.607 ms 75.102 ms 79.334 ms]
  gdbm lookup             time:   [9.0855 ms 9.8480 ms 10.637 ms]
  gdbm prepare            time:   [110.35 us 120.40 us 135.63 us]
  rocksdb insertion       time:   [117.96 ms 123.42 ms 127.85 ms]
  rocksdb lookup          time:   [24.413 ms 26.147 ms 28.153 ms]
  rocksdb prepare         time:   [3.8316 ms 4.1776 ms 4.5039 ms]

Note: Subtract "prepare" from "insertion" to get meaningful values.

Code to benchmark rocksdb and gdbm:

```
extern crate criterion;
extern crate gnudbm;
extern crate rand;
extern crate rocksdb;
extern crate tempdir;

use criterion::Criterion;
use gnudbm::GdbmOpener;
use rand::{ChaChaRng, Rng};
use rocksdb::DB;
use tempdir::TempDir;

const N: usize = 20480;

/// Generate random buffer
fn gen_buf(size: usize) -> Vec<u8> {
    let mut buf = vec![0u8; size];
    ChaChaRng::new_unseeded().fill_bytes(buf.as_mut());
    buf
}

fn criterion_benchmark(c: &mut Criterion) {
    c.bench_function("rocksdb prepare", |b| {
        b.iter(move || {
            let dir = TempDir::new("index").expect("TempDir::new");
            let _db = DB::open_default(dir.path().join("a")).unwrap();
        });
    });

    c.bench_function("rocksdb insertion", |b| {
        let buf = gen_buf(N * 20);
        b.iter(move || {
            let dir = TempDir::new("index").expect("TempDir::new");
            let db = DB::open_default(dir.path().join("a")).unwrap();
            for i in 0..N {
                db.put(&&buf[20 * i..20 * (i + 1)], b"v").unwrap();
            }
        });
    });

    c.bench_function("rocksdb lookup", |b| {
        let dir = TempDir::new("index").expect("TempDir::new");
        let db = DB::open_default(dir.path().join("a")).unwrap();
        let buf = gen_buf(N * 20);
        for i in 0..N {
            db.put(&&buf[20 * i..20 * (i + 1)], b"v").unwrap();
        }
        b.iter(move || {
            for i in 0..N {
                db.get(&&buf[20 * i..20 * (i + 1)]).unwrap();
            }
        });
    });

    c.bench_function("gdbm prepare", |b| {
        let buf = gen_buf(N * 20);
        b.iter(move || {
            let dir = TempDir::new("index").expect("TempDir::new");
            let _db = GdbmOpener::new().create(true).readwrite(dir.path().join("a")).unwrap();
        });
    });

    c.bench_function("gdbm insertion", |b| {
        let buf = gen_buf(N * 20);
        b.iter(move || {
            let dir = TempDir::new("index").expect("TempDir::new");
            let mut db = GdbmOpener::new().create(true).readwrite(dir.path().join("a")).unwrap();
            for i in 0..N {
                db.store(&&buf[20 * i..20 * (i + 1)], b"v").unwrap();
            }
        });
    });

    c.bench_function("gdbm lookup", |b| {
        let dir = TempDir::new("index").expect("TempDir::new");
        let mut db = GdbmOpener::new().create(true).readwrite(dir.path().join("a")).unwrap();
        let buf = gen_buf(N * 20);
        for i in 0..N {
            db.store(&&buf[20 * i..20 * (i + 1)], b"v").unwrap();
        }
        b.iter(move || {
            for i in 0..N {
                db.fetch(&&buf[20 * i..20 * (i + 1)]).unwrap();
            }
        });
    });
}

criterion_group!{
    name=benches;
    config=Criterion::default().sample_size(20);
    targets=criterion_benchmark
}
criterion_main!(benches);
```

Reviewed By: DurhamG

Differential Revision: D7404532

fbshipit-source-id: ff39f520b78ad1b71eb36970506b313bb2ff426b
2018-04-13 21:51:40 -07:00
Jun Wu
5576402ea9 indexedlog: add ability to clone a Index object
Summary:
This will be useful for benchmarks - prepare an index as a template, and
clone it in the tests.

Reviewed By: DurhamG

Differential Revision: D7422835

fbshipit-source-id: 190bbdee7cb7c1526274b4d4dab07af4984b5df6
2018-04-13 21:51:40 -07:00
Jun Wu
2f30189748 indexedlog: reorder "use"s
Summary:
The latest rustfmt disagrees about the order of `std::io` imports. Move the
troublesome line to a separate group so both the old and new rustfmt agress
on the format.

Reviewed By: DurhamG

Differential Revision: D7422834

fbshipit-source-id: 9f5289ef2af1a691559fe691e121190f6d845162
2018-04-13 21:51:40 -07:00
Jun Wu
704eef1e4e radixbuf: use criterion for benchmark
Summary:
The old `rustc-test` crate no longer works. There is an upstream
bug report at https://github.com/servo/rustc-test/issues/7.

This change makes it possible to compare radixbuf performance
with the new index.

Reviewed By: DurhamG

Differential Revision: D7404531

fbshipit-source-id: 515e732a65388db4c865c7b139d0f57ead76f788
2018-04-13 21:51:40 -07:00
Jun Wu
9672c45582 indexedlog: add a test comparing with std HashMap
Reviewed By: DurhamG

Differential Revision: D7404529

fbshipit-source-id: a52da9aa9661b48eefc015ce351886677f842d66
2018-04-13 21:51:40 -07:00
Jun Wu
9077cbb5a7 indexedlog: reverse the writing order of radix entries
Summary:
Radix entries need to be written in an reversed order given the order they
are added to the vector.

Reviewed By: DurhamG

Differential Revision: D7404530

fbshipit-source-id: 403189b5c0fa6f21183e62eea04ce4ce7c4e1129
2018-04-13 21:51:40 -07:00
Jun Wu
2075ad87c2 indexedlog: implement leaf splitting
Summary: Complete the insertion interface.

Reviewed By: DurhamG

Differential Revision: D7377210

fbshipit-source-id: 96645ac03a3fd65f22d9a9a54d8479715f49e67d
2018-04-13 21:51:39 -07:00
Jun Wu
a436d0554d indexedlog: add more helper methods
Summary: Those little read and write helpers are used in the next diff.

Reviewed By: DurhamG

Differential Revision: D7377214

fbshipit-source-id: c6e2d240334c11a0b08b15cd7d5c114b6f4d8ace
2018-04-13 21:51:39 -07:00
Jun Wu
61bf1f3854 indexedlog: add a helper function to get key content
Summary:
Add a helper function `peek_key_entry_content` that checks key type and
return the key content.

Reviewed By: DurhamG

Differential Revision: D7377211

fbshipit-source-id: 0ce509aba30309373a709cf5fbcb909dd80471dc
2018-04-13 21:51:39 -07:00
Jun Wu
bf55572f78 indexedlog: partially implement insertion
Summary:
Implement insertion when there is no need to split a leaf entry.

The API may be subject to change if we want other value types. For now, it's
better to get something working and can be benchmarked so we have data about
performance impact with new format changes.

Reviewed By: DurhamG

Differential Revision: D7343423

fbshipit-source-id: 9761f72168046dbafcb00883634aa7ad513a522b
2018-04-13 21:51:39 -07:00
Jun Wu
2389fd95c0 indexedlog: add helper methods about writing data
Summary:
Like the `peek_` family of helper methods. Those methods handles writing
data for both dirty (in-memory) and non-dirty (on-disk) cases. They will
be used in the next diff.

Reviewed By: DurhamG

Differential Revision: D7377208

fbshipit-source-id: f458a20da4bb7808f37daeed3077be2f7e90a9df
2018-04-13 21:51:39 -07:00
Jun Wu
cb58628046 indexedlog: add debug formatter
Summary:
Add code to print out Index's on-disk and in-memory entries in
human-friendly form. This is useful for explaining its internal state, so it
could be used in tests.

Reviewed By: DurhamG

Differential Revision: D7343427

fbshipit-source-id: 706a35404ea42c413657b389166729f8dd1315a3
2018-04-13 21:51:39 -07:00
Jun Wu
a3f7ec3f9b indexedlog: fix root entry serialization
Summary:
Offset stored in it needs to be translated, as done in other types of
entries.  I forgot it.

Reviewed By: DurhamG

Differential Revision: D7404528

fbshipit-source-id: fb09a9c3052ddfe8f8016440290062084d5d8b03
2018-04-13 21:51:39 -07:00
Jun Wu
fcc71af3ab indexedlog: add API to find link offset from a key
Summary:
This is a low-level API that follows the base16 sequence of a key, and
return potentially matched `LinkOffset`.

Reviewed By: DurhamG

Differential Revision: D7343424

fbshipit-source-id: 38f260064d1a23695a28dda6f7dc921f88c7fccc
2018-04-13 21:51:39 -07:00
Jun Wu
871ca6c96b indexedlog: add helper methods to read data
Summary:
Add a bunch of helper methods to "peek" data inside all kinds of entries.
They will be used in the next diff.

The benefit of those helper methods is they handle both dirty offsets and
non-dirty offsets transparently. Previously I have tried to always parse
on-disk entries into in-memory ones and stored them in a hashmap cache.
But that turned to have too much overhead so always reading from disk is
more desirable. It seems to provide at least 2x perf improvement from my
previous quick test.

Reviewed By: DurhamG

Differential Revision: D7377207

fbshipit-source-id: 1b393f1fe64c1d54b986ba7c3b03c790adb694d4
2018-04-13 21:51:39 -07:00
Jun Wu
983d6920f5 indexedlog: add a non-dirty helper method
Summary:
The `non_dirty` helper method enforces the offset to be a non-dirty one.
It will be used frequently for checking offsets read from the disk, since
the on-disk offsets shouldn't have any reference to dirty (in-memory)
entries.

Reviewed By: DurhamG

Differential Revision: D7377209

fbshipit-source-id: c6c381c065d3ba8aaa65698224e4778b86edbc4a
2018-04-13 21:51:39 -07:00
Jun Wu
f0b5cd6eae indexedlog: add simple DirtyOffset abstraction
Summary: The `DirtyOffset` enum converts between array indexes and u64.

Reviewed By: DurhamG

Differential Revision: D7377215

fbshipit-source-id: 29d4f7d74f15523034c11abcc09329a1b21142b1
2018-04-13 21:51:39 -07:00
Jun Wu
3859d00394 indexedlog: implement flush for the main index
Summary:
The flush method will write buffered data to disk.

A mistake in Root entry serialization is fixed - it needs to translate dirty
offsets to non-dirty ones.

Reviewed By: DurhamG

Differential Revision: D7223729

fbshipit-source-id: baeaab27627d6cfb7c5798d3a39be4d2b8811e5f
2018-04-13 21:51:35 -07:00
Jun Wu
8f5c35c8d2 indexedlog: initial main index structure
Summary:
Add the main `Index` structure and its constructor.

The structure focus on the index logic itself. It does not have the checksum
part yet.

Some notes about choices made:
- The use of mmap: mmap is good for random I/O, and has the benefit of
  sharing buffers between processes reading the same file. We may be able to
  do good user-space caching for the random I/O part. But it's harder to
  share the buffers between processes.
- The "read_only" auto decision. Common "open" pattern requires the caller
  to pass whether they want to read or write. The index makes the decision
  for the caller for convenience (ex. running "hg log" on somebody else's
  repo).
- The "load root entry from the end of the file" feature. It's just for
  convenience for users wanting to use the Index in a standalone way. We
  probably

Reviewed By: DurhamG

Differential Revision: D7208358

fbshipit-source-id: 14b74d7e32ef28bd5bc3483fd560c489d36bf8e5
2018-04-13 21:51:35 -07:00
Jun Wu
545f670504 pathencoding: utility for converting between bytes and paths
Summary:
A simple utility that does paths <-> local bytes conversion. It's needed
since Mercurial stores paths using local encoding in manifests.

For POSIX, the code is zero-cost - no real conversion or error can happen.
This is in theory cheaper than what treedirstate does.

For Windows, the "local_encoding" crate is selected as Yuya suggested the
`MultiByteToWideChar` Win32 API [1] and "local_encoding" uses it. It does
the right thing given my experiment with GBK (Chinese, simplified) encoding.

```
  ....
  C:\Users\quark\enc>hg debugshell --config extensions.debugshell=
  >>> repo[0].manifest().text()
  '\xc4\xbf\xc2\xbc1/\xce\xc4\xbc\xfe1\x00b80de5d138758541c5f05265ad144ab9fa86d1db\n'
  >>> repo[0].files()
  ['\xc4\xbf\xc2\xbc1/\xce\xc4\xbc\xfe1']
  extern crate local_encoding;
  use std::path::PathBuf;
  use local_encoding::{Encoder, Encoding};
  const mpath: &[u8] = b"\xc4\xbf\xc2\xbc1/\xce\xc4\xbc\xfe1";
  fn main() {
      let p = PathBuf::from(Encoding::OEM.to_string(mpath).unwrap());
      println!("exists: {}", p.exists());
      println!("mpath len: {}, osstr len: {}", mpath.len(), p.as_path().as_os_str().len());
  }
  exists: true
  mpath len: 11, osstr len: 15
```

In the future, we might normalize the paths to UTF-8 before storing them in
manifest to avoid issues.

Differential Revision: D7319604

fbshipit-source-id: a7ed5284be116c4176598b4c742e8228abcc3b02
2018-04-13 21:51:35 -07:00
Jun Wu
78f4faea65 xdiff: add a preprocessing step that trims files
Summary:
xdiff has a `xdl_trim_ends` step that removes common lines, unmatchable
lines. That is in theory good, but happens too late - after splitting,
hashing, and adjusting the hash values so they are unique. Those splitting,
hashing and adjusting hash values steps could have noticeable overhead.

For not uncommon cases like diffing two large files with minor differences,
the raw performance of those preparation steps seriously matter. Even
allocating an O(N) array and storing line offsets to it is expensive.
Therefore my previous attempts [1] [2] cannot be good enough since they do
not remove the O(N) array assignment.

This patch adds a preprocessing step - `xdl_trim_files` that runs before
other preprocessing steps. It counts common prefix and suffix and lines in
them (needed for displaying line number), without doing anything else.

Testing with a crafted large (169MB) file, with minor change:

```
  open('a','w').write(''.join('%s\n' % (i % 100000) for i in xrange(30000000) if i != 6000000))
  open('b','w').write(''.join('%s\n' % (i % 100000) for i in xrange(30000000) if i != 6003000))
```

Running xdiff by a simple binary [3], this patch improves the xdiff perf by
more than 10x for the above case:

```
  # xdiff before this patch
  2.41s user 1.13s system 98% cpu 3.592 total
  # xdiff after this patch
  0.14s user 0.16s system 98% cpu 0.309 total
  # gnu diffutils
  0.12s user 0.15s system 98% cpu 0.272 total
  # (best of 20 runs)
```

It's still slightly slower than GNU diffutils. But it's pretty close now.

Testing with real repo data:

For the whole repo, this patch makes xdiff 25% faster:

```
  # hg perfbdiff --count 100 --alldata -c d334afc585e2 --blocks [--xdiff]
  # xdiff, after
  ! wall 0.058861 comb 0.050000 user 0.050000 sys 0.000000 (best of 100)
  # xdiff, before
  ! wall 0.077816 comb 0.080000 user 0.080000 sys 0.000000 (best of 91)
  # bdiff
  ! wall 0.117473 comb 0.120000 user 0.120000 sys 0.000000 (best of 67)
```

For files that are long (ex. commands.py), the speedup is more than 3x, very
significant:

```
  # hg perfbdiff --count 3000 --blocks commands.py.i 1 [--xdiff]
  # xdiff, after
  ! wall 0.690583 comb 0.690000 user 0.690000 sys 0.000000 (best of 12)
  # xdiff, before
  ! wall 2.240361 comb 2.210000 user 2.210000 sys 0.000000 (best of 4)
  # bdiff
  ! wall 2.469852 comb 2.440000 user 2.440000 sys 0.000000 (best of 4)
```

The improvement is also seen for the `json` test case mentioned in D7124455.
xdiff's time improves from 0.3s to 0.04s, similar to GNU diffutils.

This patch is also sent as https://phab.mercurial-scm.org/D2686.

[1]: https://phab.mercurial-scm.org/D2631
[2]: https://phab.mercurial-scm.org/D2634
[3]:

```
// Code to run xdiff from command line. No proper error handling.
mmfile_t readfile(const char *path) {
  struct stat st; int fd = open(path, O_RDONLY);
  fstat(fd, &st); mmfile_t f = { malloc(st.st_size), st.st_size };
  ensure(read(fd, f.ptr, st.st_size) == st.st_size); close(fd); return f; }
static int xdiff_outf(void *priv_, mmbuffer_t *mb, int nbuf) { int i;
  for (i = 0; i < nbuf; i++) { write(STDOUT_FILENO, mb[i].ptr, mb[i].size); }
  return 0; }
int main(int argc, char const *argv[]) {
  mmfile_t a = readfile(argv[1]), b = readfile(argv[2]);
  xpparam_t xpp = { XDF_INDENT_HEURISTIC, 0 };
  xdemitconf_t xecfg = { 3, 0 }; xdemitcb_t ecb = { 0, &xdiff_outf };
  xdl_diff(&a, &b, &xpp, &xecfg, &ecb); return 0; }
```

Reviewed By: ryanmce

Differential Revision: D7151582

fbshipit-source-id: 3f2dd43b74da118bd827af4fc5e1bf65be191ad2
2018-04-13 21:51:25 -07:00
Jun Wu
865700883d indexedlog: move mmap_readonly to utils
Summary:
`mmap_readonly` will be reused in `index.rs` so let's moved it to a shared
utils module.

Reviewed By: DurhamG

Differential Revision: D7208359

fbshipit-source-id: d98779e4e21765ce0e185281c9560245b59b174c
2018-04-13 21:51:25 -07:00
Jun Wu
d3b0f0cdfb indexedlog: add RAII file lock
Summary:
Add ScopedFileLock. This is similar to Python's contextmanager.
It's easier to use than the fs2 raw API, since it guarantees the file is
unlocked.

Reviewed By: jsgf

Differential Revision: D7203684

fbshipit-source-id: 5d7beed99ff992466ab7bf1fbea0353de4dfe4f9
2018-04-13 21:51:25 -07:00
Jun Wu
605cd36716 indexedlog: add serialization for root entry
Reviewed By: DurhamG

Differential Revision: D7191653

fbshipit-source-id: 4c82a6b2a00d8e4cb3c67ecb382659ff8946bdad
2018-04-13 21:51:25 -07:00
Jun Wu
0f9d39cae8 indexedlog: add serialization for key entry
Reviewed By: DurhamG

Differential Revision: D7191651

fbshipit-source-id: 8eb8cbc00f0b15660e6d9e988ae41b761d854fa2
2018-04-13 21:51:25 -07:00