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

Replace bytes_xor with much faster numpy based version #15

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions makeelf/utils.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
#!/usr/bin/env python3
## \file utils.py
# \brief Utility functions

# Old code was quite slow on large files (e.g., several seconds on a 32MB ELF).
# Most of this was xor. Large speedup from this SO answer:
#https://stackoverflow.com/questions/2119761/simple-python-challenge-fastest-bitwise-xor-on-data-buffers
#
# Although, this does add numpy as a dependency, so perhaps this should be checked at runtime?
Comment on lines +5 to +9
Copy link
Owner

Choose a reason for hiding this comment

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

This is perfect information for commit message, but serves no purpose inline, as whoever reads this does not see previous version.

from numpy import frombuffer, bitwise_xor, byte

def bytes_xor(lhs, rhs):
res = []
for a, b in zip(lhs, rhs):
res.append(a ^ b)
return bytes(res)
a = frombuffer(lhs, dtype=byte)
b = frombuffer(rhs, dtype=byte)
c = bitwise_xor(a, b)
r = c.tostring()
return bytes(r)