Skip to content

Conversation

ellemouton
Copy link
Contributor

This PR cleans up the CTxMemPool interface by including the ratio used to determine when a mempool sanity check should run in the constructor of CTxMempool instead of using nCheckFrequency which required a cast from a double to a uint32_t. Since nCheckFrequency (now called m_check_ratio) is set in the constructor and only every read from there after, it can be turned into a const and no longer needs to be guarded by the 'cs' lock.

Since nCheckFrequency/m_check_ratio no longer needs to lock the 'cs' mutux, mutex lock line in the "CTxMempool::check" function can be moved below where the m_check_ratio variable is checked. Since the variable is 0 by default (meaning that "CTxMempool::check" will most likely not run its logic) this saves us from unnecessarily grabbing the lock.

@jnewbery
Copy link
Contributor

Concept ACK! Removing unnecessary interface functions, making constant values const, reducing mutex locking and avoiding casts between int and float types are all excellent improvements 🙂

@practicalswift
Copy link
Contributor

Concept ACK

Very nice first-time contribution @ellemouton! Warm welcome as a contributor! :)

@DrahtBot
Copy link
Contributor

DrahtBot commented Oct 22, 2020

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

Conflicts

Reviewers, this pull request conflicts with the following ones:

If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

Copy link
Member

@glozow glozow left a comment

Choose a reason for hiding this comment

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

Concept ACK 😄 what a cool first contribution! Code looks good to me. Linter is failing on trailing whitespace in the CTxMemPool constructor doxygen comments

Copy link
Contributor

@jnewbery jnewbery left a comment

Choose a reason for hiding this comment

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

utACK c65a593

I've left two tiny style comments. Feel free to take or leave.

Use a ratio instead of a frequency that requires a double to int cast
for determining how often a mempool sanity check should run.
…gument

Since m_check_ratio is only set once and since the CTxMemPool object is
no longer a global variable, m_check_ratio can be passed into the
constructor of CTxMemPool. Since it is only read from after
initialization, m_check_ratio can also be made a const and hence no
longer needs to be guarded by the cs mutex.
Shorten the CTxMemPool initializer list using default initialization
for members that dont depend on the constuctor parameters.
@jnewbery
Copy link
Contributor

utACK f15e780

Copy link
Member

@glozow glozow left a comment

Choose a reason for hiding this comment

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

utACK f15e780

@theStack
Copy link
Contributor

Concept ACK
Welcome as a new contributor @ellemouton, very nice first PR! 👍

It would be very interesting on why the original version of the mempool sanity check (introduced in #6776) used a random range of 2^32, needing rather complicated floating-point logic... I guess back then the motivation was to avoid a (potentially expensive) modulo operation?

@ellemouton
Copy link
Contributor Author

Thanks @theStack 😄

Yeah, good question. I will need to look into that a bit!

@sipa
Copy link
Member

sipa commented Oct 27, 2020

@theStack That's exactly the reason.

@jnewbery
Copy link
Contributor

jnewbery commented Oct 27, 2020

the motivation was to avoid a (potentially expensive) modulo operation?

Correct me if I'm wrong, but GetRand() no longer includes a modulo operation, so we can switch to using GetRand() without worrying about that.

@ellemouton
Copy link
Contributor Author

cool, had a look now and I think @jnewbery is correct, GetRand() used to use a modulo operation but that changed in commit 152146e.

Copy link
Contributor

@theStack theStack left a comment

Choose a reason for hiding this comment

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

Code Review ACK f15e780

@theStack
Copy link
Contributor

theStack commented Nov 1, 2020

the motivation was to avoid a (potentially expensive) modulo operation?

Correct me if I'm wrong, but GetRand() no longer includes a modulo operation, so we can switch to using GetRand() without worrying about that.

Agreed, GetRand() uses FastRandomContext::randrange() now, which in turn generates a random number within a specific range by repeatedly generating random numbers with as many bits as the range (which shouldn't take more than 2 rounds on average if I'm not mistaken).

@maflcko
Copy link
Member

maflcko commented Dec 1, 2020

ACK f15e780 👘

Show signature and timestamp

Signature:

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA512

ACK f15e780b9e57554c723bc02aa41150ecf3e3a8c9 👘
-----BEGIN PGP SIGNATURE-----

iQGzBAEBCgAdFiEE+rVPoUahrI9sLGYTzit1aX5ppUgFAlwqrYAACgkQzit1aX5p
pUi4bQv7BHwX3R87hsf4rDy4IO4itsm9oYcMsTz6EMYkRFJhiD7Kb6+3Co3CR6wo
S3iuKBJdiLePALCH+u27iqQ+pJEH7evrHZWQYwM7jEuCO0CerHkPONZwcQTl8/V7
OOmEF7bfCMOJtrx2+yjgkg6HfL7ly+L5zwHt7UQdyXUdSJg03kdjd0CEOfh2yfaW
hA3+rH5SMLtfULhYGqM8XXXn0Ek619gjTla4axZ0Eh5ytPxorvxoxqW6SRqDrHjM
iU4Zs3qJcNgP64teR5IVtnBZE9FCR3puQzH0UD0ddvIpRo1b6agaJ2JOtM69lrcw
Hkb4y2rF+vSUzXZYog1HnPzxeMiMlPDHjm0Maaygx897igHBr0Tnmksog5WGwliS
vV5cDHhEKLdDVTaVCeK78dxys0Ohhvvj9m4y7Xu42y0a5Qey9gQSYOetT0MA/5zQ
+C+YWV5lyigypdM9jO1DwwelPAb1orIvGt91b96kuowU3DCArIcYmUKFUcS0oKbh
mI+1MDT/
=AV9k
-----END PGP SIGNATURE-----

Timestamp of file with hash 548f07ab88d61d2cf2c5d77ad9d0bd700f42cfa1d87e97bf8649532160b609a5 -

@maflcko maflcko merged commit cd72033 into bitcoin:master Dec 1, 2020
sidhujag pushed a commit to syscoin/syscoin that referenced this pull request Dec 1, 2020
f15e780 refactor: Clean up CTxMemPool initializer list (Elle Mouton)
e331069 refactor: Make CTxMemPool::m_check_ratio a const and a constructor argument (Elle Mouton)
9d4b4b2 refactor: Avoid double to int cast for nCheckFrequency (Elle Mouton)

Pull request description:

  This PR cleans up the CTxMemPool interface by including the ratio used to determine when a mempool sanity check should run in the constructor of CTxMempool instead of using nCheckFrequency which required a cast from a double to a uint32_t. Since nCheckFrequency (now called m_check_ratio) is set in the constructor and only every read from there after, it can be turned into a const and no longer needs to be guarded by the 'cs' lock.

  Since nCheckFrequency/m_check_ratio no longer needs to lock the 'cs' mutux, mutex lock line in the "CTxMempool::check" function can be moved below where the m_check_ratio variable is checked. Since the variable is 0 by default (meaning that "CTxMempool::check" will most likely not run its logic) this saves us from unnecessarily grabbing the lock.

ACKs for top commit:
  jnewbery:
    utACK f15e780
  MarcoFalke:
    ACK f15e780 👘
  glozow:
    utACK bitcoin@f15e780
  theStack:
    Code Review ACK f15e780

Tree-SHA512: d83f3b5311ca128847b621e5e999c7e1bf0f4e6261d4cc090fb13e229a0f7eecd66ad997f654f50a838baf708d1515740aa3bffc244909a001d01fd5ae398b68
Fabcien pushed a commit to Bitcoin-ABC/bitcoin-abc that referenced this pull request Jan 19, 2022
Summary:
PR description:
> This PR cleans up the CTxMemPool interface by including the ratio used to determine when a mempool sanity check should run in the constructor of CTxMempool instead of using nCheckFrequency which required a cast from a double to a uint32_t. Since nCheckFrequency (now called m_check_ratio) is set in the constructor and only every read from there after, it can be turned into a const and no longer needs to be guarded by the 'cs' lock.
>
> Since nCheckFrequency/m_check_ratio no longer needs to lock the 'cs' mutux, mutex lock line in the "CTxMempool::check" function can be moved below where the m_check_ratio variable is checked. Since the variable is 0 by default (meaning that "CTxMempool::check" will most likely not run its logic) this saves us from unnecessarily grabbing the lock.

Use a ratio instead of a frequency that requires a double to int cast for determining how often a mempool sanity check should run.

This is a backport of [[bitcoin/bitcoin#20222 | core#20222]] [1/3]
bitcoin/bitcoin@9d4b4b2

Test Plan: `ninja all check-all`

Reviewers: #bitcoin_abc, Fabien

Reviewed By: #bitcoin_abc, Fabien

Differential Revision: https://reviews.bitcoinabc.org/D10841
Fabcien pushed a commit to Bitcoin-ABC/bitcoin-abc that referenced this pull request Jan 19, 2022
…gument

Summary:
Since m_check_ratio is only set once and since the CTxMemPool object is
no longer a global variable, m_check_ratio can be passed into the
constructor of CTxMemPool. Since it is only read from after
initialization, m_check_ratio can also be made a const and hence no
longer needs to be guarded by the cs mutex.

This is a backport of [[bitcoin/bitcoin#20222 | core#20222]] [2/3]
bitcoin/bitcoin@e331069

Depends on D10841

Test Plan:
With TSAN:
`ninja && ninja check check-functional`

Reviewers: #bitcoin_abc, Fabien

Reviewed By: #bitcoin_abc, Fabien

Differential Revision: https://reviews.bitcoinabc.org/D10842
Fabcien pushed a commit to Bitcoin-ABC/bitcoin-abc that referenced this pull request Jan 19, 2022
Summary:
Shorten the CTxMemPool initializer list using default initialization
for members that dont depend on the constuctor parameters.

This is a backport of [[bitcoin/bitcoin#20222 | core#20222]] [3/3]
bitcoin/bitcoin@f15e780

Depends on D10842

Test Plan:
`ninja all check-all`

eviewers:

Reviewers: #bitcoin_abc, Fabien

Reviewed By: #bitcoin_abc, Fabien

Differential Revision: https://reviews.bitcoinabc.org/D10843
@bitcoin bitcoin locked as resolved and limited conversation to collaborators Feb 15, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants