erocarrera / pefile

pefile is a Python module to read and work with PE (Portable Executable) files
MIT License
1.86k stars 519 forks source link

Speed up relocation process #344

Closed elicn closed 2 years ago

elicn commented 2 years ago

Relocation can take up to several minutes for large files (e.g. 64-bit version of kernelbase.dll).

The reason is largely because __data__ stores contents as bytes, which does not have a __setitem__ method. When data needs to be modified it uses slicing - which casues the entire content to be copied over and over and slow down the entire relocation process. This patch simply turns __data__ into a bytearray and have it leverage its __setitem__ method to modify its contents much faster.

Note: after analyzing the slowdown rootcause and writing the patch, I noticed this issue was already reported and had a PR submitted to fix it almost 3 years ago (#266). I don't know why that PR was not accepted, but hopefully a fresh one would do.

Note: this patch does not maintain compatibility to Python2

elicn commented 2 years ago

Hi @erocarrera, Is there anything specific that holds this PR from being merged..? Please let me know if there is something I can do about it.

erocarrera commented 2 years ago

Hi @elicn. Sorry for the delay, I don't get to work on pefile very regularly. The PR is great and the speed up very significant! I had overlooked #266, thanks for also bringing it to my attention.

elicn commented 2 years ago

Great, thanks!

psrok1 commented 2 years ago

Thank you as well!