Optimize and Cleanup CScript::FindAndDelete #7907

pull pstratem wants to merge 4 commits into bitcoin:master from pstratem:2016-04-17-findanddelete changing 2 files +129 −3
  1. pstratem commented at 6:01 AM on April 19, 2016: contributor

    This PR improves the worst-case behavior of CScript::FindAndDelete.

    I have emphasized the obvious correctness of the algorithm in this commit.

    Additionally I have done extensive fuzzing to ensure the result is identical to the current implementation.

  2. pstratem force-pushed on Apr 19, 2016
  3. laanwj added the label Validation on Apr 19, 2016
  4. laanwj added the label Resource usage on Apr 19, 2016
  5. Replace memcmp with std::equal in CScript::FindAndDelete
    Function is stl; std::equal just makes more sense.
    ec9ad5f199
  6. Replace c-style cast with c++ style static_cast. c0f660c3a3
  7. Unit test for CScript::FindAndDelete e2a30bc9a9
  8. pstratem force-pushed on Apr 19, 2016
  9. TheBlueMatt commented at 10:38 PM on April 20, 2016: contributor

    I highly prefer this to #7884...the original is kinda hard to read, but #7884 is impossible to reasonably convince yourself works, even if it probably does. I benchmarked syncing on tmpfs with the final call to VerifySignature in CheckSig() disabled on this and #7884 and couldn't detect any difference in sync time through about 280k (just happened to be some blk* flies I had lying around went until there).

  10. in src/script/script.h:None in 3322744ab5 outdated
     588 | +            pc2 = pc;
     589 |          }
     590 |          while (GetOp(pc, opcode));
     591 | +
     592 | +        result.insert(result.end(), pc2, end());
     593 | +        *this = result;
    


    TheBlueMatt commented at 10:39 PM on April 20, 2016:

    You should gate this and the last insert on an if(nFound)

  11. pstratem force-pushed on Apr 20, 2016
  12. dcousens commented at 2:29 AM on April 21, 2016: contributor

    utACK 6443169, but I'd prefer benchmarks before merge.

    edit: To clarify: benchmarks that show an improvement

  13. sipa commented at 5:03 AM on April 21, 2016: member

    @kazcw @dcousens It is only intended to improve the worst case behaviour, and @TheBlueMatt already benchmarked that it does not measurably affect the average case. @pstratem If, after including Matt's suggestion, you drop the result.reserve() call, I think this will run with zero heap effect for non-match situations.

  14. pstratem force-pushed on Apr 21, 2016
  15. pstratem commented at 5:22 AM on April 21, 2016: contributor

    @sipa indeed you're right I've made that change

  16. Improve worst-case behavior of CScript::FindAndDelete
    Thanks to Sergio Lerner for identifying this issue and suggesting this kind of solution.
    d1d7775587
  17. pstratem force-pushed on Apr 22, 2016
  18. dcousens commented at 12:27 AM on April 22, 2016: contributor

    utACK d1d7775, looks good to merge

  19. laanwj commented at 10:11 AM on April 28, 2016: member

    utACK d1d7775

  20. sipa commented at 4:58 PM on May 5, 2016: member

    utACK d1d7775587473410a107e7079616b9ecaae8dd06

  21. laanwj merged this on May 5, 2016
  22. laanwj closed this on May 5, 2016

  23. laanwj referenced this in commit 006cdf64dc on May 5, 2016
  24. svost referenced this in commit eff0cd3a46 on May 10, 2016
  25. sipa cross-referenced this on May 17, 2016 from issue Optimize CScript.FindAndDelete by gavinandresen
  26. SergioDemianLerner cross-referenced this on Sep 16, 2016 from issue ZCA­002 - Inheriting FindAndDelete from Bitcoin is considered dangerous by nathan-at-least
  27. sickpig cross-referenced this on Jan 20, 2017 from issue Port Core PR 7907 (Optimize and Cleanup CScript::FindAndDelete) by sickpig
  28. brakmic cross-referenced this on Jan 4, 2018 from issue backport: Bitcoin - optimize CScript.FindAndDelete by brakmic
  29. DanGould cross-referenced this on Aug 29, 2018 from issue Go through all BTC CVE by noescape00
  30. akshaynexus cross-referenced this on Jan 8, 2020 from issue [Script] Optimize and Cleanup CScript::FindAndDelete by akshaynexus
  31. random-zebra referenced this in commit fbcc5305d6 on Feb 17, 2020
  32. barton2526 cross-referenced this on Aug 23, 2021 from issue refactor: Optimize and Cleanup CScript::FindAndDelete by barton2526
  33. bitcoin locked this on Sep 8, 2021

github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2026-05-20 06:55 UTC