Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tree: fix add_tree to be constant-time #1267

Merged
merged 1 commit into from
Jan 29, 2021
Merged

Conversation

samoht
Copy link
Member

@samoht samoht commented Jan 28, 2021

We don't want to recursively explore the parameters to check if
add_tree is a no-op (to try to preserve physical equality). The
cost is too high in this case.

Spotted by @camlspotter and profiled by @craigfe and @Ngoguey42

@Ngoguey42
Copy link
Contributor

I'll try to highlight the complexity problem using #1237 's large mode, and the see if your PR fixes it

@Ngoguey42
Copy link
Contributor

Since tree.exe's large mode doesn't call add_tree I couldn't test this patch using it.

tree.exe's trace mode calls add_tree through the copy operation, but those copies are really quick:

 719872 "Add" 2.488 sec (4.4%)
 46842 "Remove" 1.299 sec (2.3%)
 2250115 "Find" 14.405 sec (25.6%)
 535786 "Mem" 2.742 sec (4.9%)
 329934 "Mem_tree" 0.389 sec (0.7%)
 9999 "Checkout" 0.020 sec (0.0%)
 46 "Copy" 0.000 sec (0.0%)
 10000 "Commit" 34.938 sec (62.1%)

I have then modified tree.exe's code to collapse the paths. Even if the test slows down by a factor 10, the copies are still quick:

 719872 "Add" 2.141 sec (0.4%)
 46842 "Remove" 197.285 sec (37.4%)
 2250115 "Find" 10.000 sec (1.9%)
 535786 "Mem" 1.784 sec (0.3%)
 329934 "Mem_tree" 0.624 sec (0.1%)
 9999 "Checkout" 0.023 sec (0.0%)
 46 "Copy" 0.000 sec (0.0%)
 10000 "Commit" 315.409 sec (59.8%)

I conclude that add_tree is not problematic for tz's bootstrap phase. We will have to further improve our benchmarking suite to reproduce this problem.


Example of a path collapse: data/commitments/00/00/56/97/02/a93c481a70a9801d3429c202d5bab1 to data/commitments/0000569702a93c481a70a9801d3429c202d5bab1.

@samoht
Copy link
Member Author

samoht commented Jan 28, 2021

Thanks for the analysis. Indeed, Tree.add_tree is not really used by Tezos currently but we plan to use it to speed-up "stitching" events, which are similar to database scheme migrations: you want to rewrite the current store by moving subtrees around (for instance by "flattening paths"). So a good test would be to:

  • create a tree with lot of deep directories, e.g. /aa/bb/cc/dd/aabbccdd/foo/bar, ...
  • fold over that tree to copies subtrees into flattened paths, e.g. /flat/aabbccdd/foo/bar, ...
  • try to do this with a lot of directory entries (eg. 3 millions)

Copy link
Contributor

@Ngoguey42 Ngoguey42 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I retested https://github.com/tarides/support/issues/10 on top of this PR and it fixes the issue 👍

||
match (x.v, y.v) with
| Hash (_, x), Hash (_, y) -> hash_equal x y
| Value (_, x, None), Value (_, y, None) -> equal_node x y
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can trop the 2 first cases as they are redundant with the ones below using cached_value and cached_hash

Copy link
Member Author

@samoht samoht Jan 29, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's not totally redundant: we have to decide which equality function we will be using first depending on their underlying representation. I don't know if that makes a big difference in practice (probably not) so yes can simplify this.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok I see what you mean.

I tried with and without and I couldn't see any difference.


(* same as [equal] but do not compare in-memory maps
recursively. *)
let maybe_equal (x : t) (y : t) =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The name is ambiguous, it returns false when they still might be equal.

What about shallow_equal?

We don't want to recursively explore the parameters to check if
add_tree is a no-op (to try to preserve physical equality). The
cost is too high in this case.

Spotted by @camlspotter and profiled by @craigfe and @Ngoguey42
@samoht
Copy link
Member Author

samoht commented Jan 29, 2021

Updated with a simplification of equal and a clearer return type for maybe_equal.

Copy link
Member

@craigfe craigfe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Merge when ready, and I'll cut a release containing this.

@samoht samoht merged commit dcde551 into mirage:master Jan 29, 2021
@samoht samoht deleted the fix-add-tree branch January 29, 2021 18:51
craigfe added a commit to craigfe/opam-repository that referenced this pull request Feb 2, 2021
… irmin-chunk, irmin-pack, irmin-test, irmin-http, irmin-unix, ppx_irmin, irmin-bench, irmin-graphql, irmin-containers, irmin-mirage-git and irmin-mirage-graphql (2.4.0)

CHANGES:

### Fixed
- **irmin-pack**
  - Fix a bug in `inode` where the `remove` function could cause hashing
    instabilities. No user-facing change since this function is not being used
    yet. (mirage/irmin#1247, @Ngoguey42, @icristescu)

- **irmin**
  - Ensure that `Tree.add_tree t k v` complexity does not depend on `v` size.
    (mirage/irmin#1267, @samoht @Ngoguey42 and @craigfe)

### Added

- **irmin**
  - Added a `Perms` module containing helper types for using phantom-typed
    capabilities as used by the store backends. (mirage/irmin#1262, @craigfe)

  - Added an `Exported_for_stores` module containing miscellaneous helper types
    for building backends. (mirage/irmin#1262, @craigfe)

  - Added new operations `Tree.update` and `Tree.update_tree` for efficient
    read-and-set on trees. (mirage/irmin#1274, @craigfe)

- **irmin-pack**:
  - Added `integrity-check-inodes` command to `irmin-fsck` for checking the
    integrity of inodes. (mirage/irmin#1253, @icristescu, @Ngoguey42)

- **irmin-bench**
  - Added benchmarks for tree operations. (mirage/irmin#1237, @icristescu, @Ngoguey42,
    @craigfe)

#### Changed

- The `irmin-mem` package is now included with the `irmin` package under the
  library name `irmin.mem`. It keeps the same top-level module name of
  `Irmin_mem`. (mirage/irmin#1276, @craigfe)

#### Removed

- `Irmin_mem` no longer provides the layered in-memory store `Make_layered`.
  This can be constructed manually via `Irmin_layers.Make`. (mirage/irmin#1276, @craigfe)
craigfe added a commit to craigfe/opam-repository that referenced this pull request Feb 2, 2021
… irmin-chunk, irmin-pack, irmin-test, irmin-http, irmin-unix, ppx_irmin, irmin-bench, irmin-graphql, irmin-containers, irmin-mirage-git and irmin-mirage-graphql (2.4.0)

CHANGES:

### Fixed
- **irmin-pack**
  - Fix a bug in `inode` where the `remove` function could cause hashing
    instabilities. No user-facing change since this function is not being used
    yet. (mirage/irmin#1247, @Ngoguey42, @icristescu)

- **irmin**
  - Ensure that `Tree.add_tree t k v` complexity does not depend on `v` size.
    (mirage/irmin#1267, @samoht @Ngoguey42 and @craigfe)

### Added

- **irmin**
  - Added a `Perms` module containing helper types for using phantom-typed
    capabilities as used by the store backends. (mirage/irmin#1262, @craigfe)

  - Added an `Exported_for_stores` module containing miscellaneous helper types
    for building backends. (mirage/irmin#1262, @craigfe)

  - Added new operations `Tree.update` and `Tree.update_tree` for efficient
    read-and-set on trees. (mirage/irmin#1274, @craigfe)

- **irmin-pack**:
  - Added `integrity-check-inodes` command to `irmin-fsck` for checking the
    integrity of inodes. (mirage/irmin#1253, @icristescu, @Ngoguey42)

- **irmin-bench**
  - Added benchmarks for tree operations. (mirage/irmin#1237, @icristescu, @Ngoguey42,
    @craigfe)

#### Changed

- The `irmin-mem` package is now included with the `irmin` package under the
  library name `irmin.mem`. It keeps the same top-level module name of
  `Irmin_mem`. (mirage/irmin#1276, @craigfe)

#### Removed

- `Irmin_mem` no longer provides the layered in-memory store `Make_layered`.
  This can be constructed manually via `Irmin_layers.Make`. (mirage/irmin#1276, @craigfe)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants