Skip to content

Conversation

kazcw
Copy link
Contributor

@kazcw kazcw commented Apr 16, 2016

It looks like maybe this swap method is from an earlier iteration of prevector that used the LSB of size as a "direct/indirect" tag. The bad path isn't ever hit because in all current instances of swapping two prevectors, one is newly value-initialized and thus has an even size (0).

@laanwj
Copy link
Member

laanwj commented Apr 16, 2016

Concept ACK.
I think we need a unit test that fail before this, and succeed after this.

@sipa
Copy link
Member

sipa commented Apr 16, 2016

Nice catch, thanks.

Code review ACK, but agree that a test for this behaviour would be welcome.

@kazcw kazcw force-pushed the pvfix branch 2 times, most recently from 48493e7 to 7c5891a Compare April 16, 2016 15:03
kazcw added 3 commits April 16, 2016 08:05
Fixes a bug in which pop_back did not call the deleted item's destructor.

Using the most general erase() implementation to implement all the others
prevents similar bugs because the coupling between deallocation and destructor
invocation only needs to be maintained in one place.
Also reduces duplication of complex memmove logic.
- add a swap operation to prevector tests (fails due to broken prevector::swap)
- fix 2 prevector test operation conditions that were impossible
swap was using an incorrect condition to determine when to apply an optimization
(not swapping the full direct[] when swapping two indirect prevectors).

Rather than correct the optimization I'm removing it for simplicity. Removing
this optimization minutely improves performance in the typical (currently only)
usage of member swap(), which is swapping with a freshly value-initialized
object.
@laanwj
Copy link
Member

laanwj commented Apr 18, 2016

Thanks for adding the test. I verified that it passes with, and fails without a7af72a.
tACK a7af72a

@laanwj laanwj merged commit a7af72a into bitcoin:master Apr 18, 2016
laanwj added a commit that referenced this pull request Apr 18, 2016
a7af72a prevector::swap: fix (unreached) data corruption (Kaz Wesley)
4ed41a2 test prevector::swap (Kaz Wesley)
1e2c29f prevector: destroy elements only via erase() (Kaz Wesley)
@kazcw kazcw deleted the pvfix branch April 22, 2016 02:11
sickpig referenced this pull request in sickpig/BitcoinUnlimited Mar 9, 2018
a7af72a prevector::swap: fix (unreached) data corruption (Kaz Wesley)
4ed41a2 test prevector::swap (Kaz Wesley)
1e2c29f prevector: destroy elements only via erase() (Kaz Wesley)
Fuzzbawls added a commit to PIVX-Project/PIVX that referenced this pull request May 19, 2020
a8b965e Explicitly initialize prevector _union (random-zebra)
bdd98e8 [Trivial] Add a comment on the use of prevector in script. (random-zebra)
bbaa6e3 Clarify prevector::erase and avoid swap-to-clear (random-zebra)
28a8435 prevector: assert successful allocation (random-zebra)
de41cb5 Only call clear on prevector if it isn't trivially destructible (random-zebra)
8784df3 Make CScript (and prevector) c++11 movable. (random-zebra)
83f9ac6 serialize: Deprecate `begin_ptr` / `end_ptr` (random-zebra)
c3ecc12 prevector: add C++11-like data() method (random-zebra)
5490aa0 test prevector::swap (random-zebra)
035760e prevector::swap: fix (unreached) data corruption (random-zebra)
0e71400 prevector: destroy elements only via erase() (random-zebra)
9811a68 [Core] Prevector type (random-zebra)

Pull request description:

  Based on:
  - [x] #1554

  This introduces `prevector<N, T>`, a new basic data type which is a fully API compatible drop-in replacement for `std::vector<T>` and uses it for the script, to reduce the considerable memory overhead of vectors in cases where they normally contain a small number of small elements.

  Original tests in Bitcoin showed a reduction in dbcache memory usage by **23%**, and made an initial sync **13%** faster.

  Backported from the following upstream's PRs, with only additional edits in the 2nd layer scripts (`masternode-payments`, `masternode-budget`) and in the unit tests (to address the differences in `insecure_rand`):

  - bitcoin#6914
  - bitcoin#7888
  - bitcoin#8850
  - bitcoin#9349
  - bitcoin#9505
  - bitcoin#9856
  - bitcoin#10534
  - bitcoin#11011
  - bitcoin#14028

  ~~NOTE: Updates to `memusage.h` needed as well, when #1531 is merged.~~

ACKs for top commit:
  furszy:
    Tested ACK a8b965e .
  Fuzzbawls:
    ACK a8b965e

Tree-SHA512: 203b660dd8d9f98780be660dae0ac951766ba438836bd6f0ec921e7749c1a84143f3b920fe49f2cc399f4aa7e763098f78746d3b6ff845bcf913bb13de984bc1
zkbot added a commit to zcash/zcash that referenced this pull request Feb 17, 2021
@bitcoin bitcoin locked as resolved and limited conversation to collaborators Sep 8, 2021
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.

3 participants