-
Notifications
You must be signed in to change notification settings - Fork 786
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
2123: Improve the speed of bit flipping code #2204
Conversation
Codecov Report
@@ Coverage Diff @@
## develop #2204 +/- ##
===========================================
- Coverage 57.17% 57.16% -0.01%
===========================================
Files 501 501
Lines 13440 13439 -1
===========================================
- Hits 7684 7683 -1
Misses 5756 5756
Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here. |
547c476
to
2b5718f
Compare
:param data: The bytes whose bits to flip | ||
:return: Bytes with the bits flipped | ||
""" | ||
return bytes(generate_flipped_bits(data)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe try this and see if performance changes:
return bytes(generate_flipped_bits(data)) | |
return bytes(map(lambda byte: 255 - byte, data)) |
We could also try:
return bytes(generate_flipped_bits(data)) | |
from functools import partial | |
from operator import sub | |
flip_bits = partial(sub, 255) | |
return bytes(map(flip_bits, data)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The map call is actually about 25% slower than the generator:
$ python3.7 ./test_bit_flip_new.py
Generating random data...
Testing encryption methods...
New method: 21.68712888698792
Old method: 16.69294242199976
speedup: 0.7697165682459411
$ python3.7 ./test_bit_flip_new.py
Generating random data...
Testing encryption methods...
New method: 21.919233062013518
Old method: 16.677228108019335
speedup: 0.7608490708062826
The use of the partial function improves the map call slightly, but it's still 20% slower than the generator:
$ python3.7 ./test_bit_flip_new.py
Generating random data...
Testing encryption methods...
New method: 21.326405769999838
Old method: 17.232965190021787
speedup: 0.8080576434620619
$ python3.7 ./test_bit_flip_new.py
Generating random data...
Testing encryption methods...
New method: 21.268566457991255
Old method: 16.70874281899887
speedup: 0.7856073822371269
- Remove a function call - Use a generator - Use a more efficient flip calculation (subtraction instead of xor) Issue #2123
2b5718f
to
51ce19b
Compare
What does this PR do?
Fixes #2123.
Improves the ransomware encryption performance by about 2-3x via the following improvements (in order of effectiveness):
PR Checklist
Testing Checklist