topac / rmega

mega.nz ruby API and command-line tool
MIT License
147 stars 32 forks source link

Excessively slow aes_cbc_mac function #24

Closed sarahzrf closed 6 years ago

sarahzrf commented 7 years ago

This function is very slow and uses excessive amounts of CPU: https://github.com/topac/rmega/blob/83edafeaad3340ed9d8d607bffda835cbdeede76/lib/rmega/crypto/aes_cbc.rb#L24-L43

This is a pragmatic issue for me. I've been downloading a ~50G directory over several invocations, and each time I start rmega-dl, it takes a long time to verify the already-downloaded portion and makes my fan turn on. As the function is currently implemented, it only verifies about 30M per second.

The issue appears to be the fact that it processes the data 16 bytes at a time. This probably makes sense in a language like C, but in Ruby, a tight loop like this has a ton of overhead. I tried modifying this function to pass the data in all at once and then just extract the last 16 bytes, and not only did it approximately triple the speed of verification, it also didn't make my fan turn on. I don't think the specific modification I made is necessarily the best solution, but it does prove that the current one is suboptimal.

topac commented 6 years ago

fixed with version 0.2.4