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

Use hashbrown replacements for std equivalents #947

Merged
merged 1 commit into from
Jun 20, 2021

Conversation

GeneFerneau
Copy link
Contributor

No description provided.

@@ -29,6 +29,7 @@ unstable = []
[dependencies]
bitcoin = "0.26"

hashbrown = { version = "0.9" }
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Current hashbrown is 0.11.2, can bump to 0.11, if MSRV 1.49 is OK

Can also feature-gate inside the prelude module, too.

@TheBlueMatt
Copy link
Collaborator

Can we make this optional instead with a "hashbrown" feature (even an implicit one where you just make "hashbrown" an optional dependency)? Then we can make no-std depend on it.

@GeneFerneau GeneFerneau force-pushed the hash branch 2 times, most recently from 5c10136 to d9ca4ea Compare June 10, 2021 03:24
@codecov
Copy link

codecov bot commented Jun 10, 2021

Codecov Report

Merging #947 (da7a851) into main (4d1c1a3) will increase coverage by 0.01%.
The diff coverage is n/a.

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #947      +/-   ##
==========================================
+ Coverage   90.56%   90.58%   +0.01%     
==========================================
  Files          60       60              
  Lines       30423    30423              
==========================================
+ Hits        27553    27559       +6     
+ Misses       2870     2864       -6     
Impacted Files Coverage Δ
lightning/src/chain/chainmonitor.rs 95.80% <ø> (ø)
lightning/src/chain/channelmonitor.rs 90.76% <ø> (ø)
lightning/src/chain/keysinterface.rs 95.41% <ø> (ø)
lightning/src/chain/onchaintx.rs 94.14% <ø> (ø)
lightning/src/ln/chanmon_update_fail_tests.rs 97.78% <ø> (ø)
lightning/src/ln/channelmanager.rs 83.85% <ø> (ø)
lightning/src/ln/functional_test_utils.rs 94.81% <ø> (ø)
lightning/src/ln/functional_tests.rs 97.22% <ø> (+0.09%) ⬆️
lightning/src/ln/peer_handler.rs 44.30% <ø> (ø)
lightning/src/ln/reorg_tests.rs 99.62% <ø> (ø)
... and 4 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 4d1c1a3...da7a851. Read the comment docs.

Copy link
Member

@devrandom devrandom left a comment

Choose a reason for hiding this comment

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

ACK other than nits

#[cfg(feature = "no_std")]
Copy link
Collaborator

@TheBlueMatt TheBlueMatt Jun 10, 2021

Choose a reason for hiding this comment

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

I think we should directly check for the hashbrown feature. Ideally we'd be able to use hashbrown with std or no_std, but require it for no_std. Specifically, I'd like to use hashbrown with std in the fuzzers, as I can disable the default randomization of keys while fuzzing.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hrm, I tried directly naming a hashbrown feature, but I think it collides with the dependency name. Maybe I'm doing something wrong?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Huh, I thought if you don't even write out the feature and just make the dependency optional you can use it as a feature in cfg.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok, maybe that's what I was doing wrong. I tried writing out hashbrown = ["hashbrown"] as a feature. Will try what you described.

@@ -6,3 +6,4 @@ cargo check
cargo doc
cargo doc --document-private-items
cd fuzz && cargo check --features=stdin_fuzz
cd ../lightning && cargo check --no-default-features --features=no_std
Copy link
Collaborator

Choose a reason for hiding this comment

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

In .github/workflows/build.yml can you also add a cd lightning && cargo test --verbose --color always --features hashbrown to run the test suite with hashbrown + std?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Which builds should that run under, or do you want a new build target?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Uhhmmmmm, yes? I don't think it matters too much, maybe in the existing ones is simplest, given it's ~one line?

pub(super) fn random_init_seed() -> u64 {
// Because the default HashMap in std pulls OS randomness, we can use it as a (bad) RNG.
use core::hash::{BuildHasher, Hasher};
let seed = std::collections::hash_map::RandomState::new().build_hasher().finish();
println!("Using seed of {}", seed);
seed
}
#[cfg(not(feature = "no_std"))]
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: indentation with tabs.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

agh! yeah, my editor defaults to indenting with spaces. Would you be open to adding a commit hook to automatically format?

Copy link
Collaborator

Choose a reason for hiding this comment

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

If there's an easy way to just swap spaces for tabs, sure (I don't think there is, though?), but we've tried rustfmt a few times and it makes tons of code completely unreadable. It's desire to lay out all your code completely vertically means you lose a ton of context when reviewing more often than not. There's a few issues open upstream to make it more sensible, but I don't believe there's been much progress on any of them.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok, I won't worry about it then. Thought there might be a simple fix.

@GeneFerneau GeneFerneau force-pushed the hash branch 4 times, most recently from 75b8a14 to 9bac852 Compare June 11, 2021 21:18
@@ -56,6 +56,8 @@ jobs:
cargo build --verbose --color always --features rpc-client
cargo build --verbose --color always --features rpc-client,rest-client
cargo build --verbose --color always --features rpc-client,rest-client,tokio
cd ../lightning
cargo build --verbose --color always --features hashbrown
Copy link
Collaborator

Choose a reason for hiding this comment

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

We should also test with hashbrown in the "! matrix.build-net-tokio" block immediately above this one.

@@ -92,6 +98,8 @@ jobs:
RUSTFLAGS="-C link-dead-code" cargo test --verbose --color always --features rpc-client
RUSTFLAGS="-C link-dead-code" cargo test --verbose --color always --features rpc-client,rest-client
RUSTFLAGS="-C link-dead-code" cargo test --verbose --color always --features rpc-client,rest-client,tokio
cd ../lightning
RUSTFLAGS="-C link-dead-code" cargo test --verbose --color always --features hashbrown
Copy link
Collaborator

Choose a reason for hiding this comment

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

I dunno if we want the last test we run in "matrix.coverage" (ie the binaries which we generate coverage reports from) to be hashbrown ones - maybe move this up above (or as the first command in) "Test on Rust ${{ matrix.toolchain }} with net-tokio and full code-linking for coverage generation"?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

SGTM, wasn't sure, so just added it after the other tests.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

hrm, seems like this causes build failure:

error[E0658]: use of unstable library feature 'ptr_cast'
   --> /home/runner/.cargo/registry/src/github.com-1ecc6299db9ec823/hashbrown-0.11.2/src/raw/mod.rs:504:57
    |
504 |         NonNull::new_unchecked(self.table.ctrl.as_ptr().cast())
    |                                                         ^^^^
    |
    = note: for more information, see https://github.com/rust-lang/rust/issues/60602

Copy link
Contributor Author

@GeneFerneau GeneFerneau Jun 18, 2021

Choose a reason for hiding this comment

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

Might only make sense to use hashbrown in later versions of Rust, per rust-lang/rust#60602

MSRV of hashbrown 0.11.2 is rust 1.49

Copy link
Collaborator

Choose a reason for hiding this comment

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

Right, it needs to only run on non-1.36 versions of rust. Maybe add this:

      - name: Test no_std builds Rust ${{ matrix.toolchain }}
        if: "matrix.build-net-tokio"
        run: cargo test --verbose --color always --features hashbrown

Copy link
Collaborator

Choose a reason for hiding this comment

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

MSRV of hashbrown 0.11.2 is rust 1.49

Oh, I missed this comment, I guess we need the if there to just be something new that matches on stable instaed.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Try:

diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml
index 231db705..2f14a4c2 100644
--- a/.github/workflows/build.yml
+++ b/.github/workflows/build.yml
@@ -14,18 +14,27 @@ jobs:
                      # 1.41.0 is Debian stable
                      1.41.0,
                      # 1.45.2 is MSRV for lightning-net-tokio, lightning-block-sync, and coverage generation
-                     1.45.2]
+                     1.45.2,
+                     # 1.49 is MSRV for no_std
+                     1.49.0]
         include:
           - toolchain: stable
             build-net-tokio: true
+            build-no-std: true
           - toolchain: stable
             platform: macos-latest
             build-net-tokio: true
+            build-no-std: true
           - toolchain: stable
             platform: windows-latest
             build-net-tokio: true
+            build-no-std: true
           - toolchain: beta
             build-net-tokio: true
+            build-no-std: true
+          - toolchain: 1.45.2
+            build-net-tokio: true
+            build-no-std: true
           - toolchain: 1.45.2
             build-net-tokio: true
             coverage: true
@@ -76,6 +85,9 @@ jobs:
           cd ../lightning
           RUSTFLAGS="-C link-dead-code" cargo build --verbose --color always --features hashbrown
           cd ..
+      - name: Test no_std builds Rust ${{ matrix.toolchain }}
+        if: "matrix.build-no-std"
+        run: cargo test --verbose --color always --features hashbrown
       - name: Test on Rust ${{ matrix.toolchain }} with net-tokio
         if: "matrix.build-net-tokio && !matrix.coverage"
         run: cargo test --verbose --color always

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I added a check build-no-std, and a 1.49.0 to the toolchain. It took a little experimentation, but I think I understand the CI build scripts better now.

The fuzz build seems to be failing for other reasons...

@GeneFerneau GeneFerneau force-pushed the hash branch 5 times, most recently from 0b09ee8 to e76a37f Compare June 18, 2021 21:43
Copy link
Collaborator

@TheBlueMatt TheBlueMatt left a comment

Choose a reason for hiding this comment

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

Looks good, only diff since devrandom's ack at d9ca4ea are CI and comment addressing, fuzz test failure is due to a libsecp bump that exists on current HEAD anyway, so shouldn't hold this up.

@TheBlueMatt TheBlueMatt merged commit 94528f0 into lightningdevkit:main Jun 20, 2021
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.

3 participants