Open xoob opened 7 years ago
@xoob this is brilliant! Especially benchmark 4. I am very surprised that 1(b) was not the fastest method given how simple it is. Good insight into how Apex is compiled/executed. It would be great to discuss further. I'll email you to setup a call.
Hello and thanks for the lib! Have these improvements been implemented? I'm running into CPU time limit exceeded errors with ~5mb zips and wondered if the changes would have any significant effect. #16 suggests the acceptable zip file sizes are very small.
Zippex.unzipAttachment()
is a dream compared to the alternatives...
@devshane we haven't yet incorporated these optimizations into the released code but I have partially implemented it for testing purposes and it does perform better in some specific cases like unzipping .jpg files. In other cases it slows things down because by switching to arrays we lose access to high level String operations. As a result, many times we end up having to iterate through every item in a large array instead of being able to call one String method.
Most likely we need to figure out a hybrid approach. @xoob and I are planing to meet to brainstorm but I have been swamped. In the mean time, if possible, you can unzip in asynchronous mode to get a larger CPU limit. In my projects I make a separate future call for each file that I am unzipping. So for instance if there are four files in a Zip, I spin four future methods to process one file each. Queueable would work too.
@pdalcol Thanks, I'll give futures a shot.
This PR adds Benchmark scripts to measure performance of different mechanisms for decoding binary data to integers. Each method has a comment explaining the implementation and its effect.
To run the benchmark, call
Benchmark.Run(10);
from the execute anonymous window.M_HEX_DEC
map.getChars()
, callmid()
for each char. Lookup the by string instead of int.HexUtil
formula which uses the underlying ASCII math.c[0]
andc[1]
.getChars()
, call charAt() for each position."\u00XX"
and then callgetChars()
to convert it to a list of byte values.Surprisingly, I discovered that this is by far the fastest and most efficient way to convert a Blob into a List of Integers:
It makes use of the fact that two hex characters can be turned into a unicode escape sequence, which is then easily converted into integers by
getChars()
. The logic works as follows:This was a ton of fun exploring and I'd love to spend some time talking about it in more depth @pdalcol and @smithpliny. Hope this can be useful for the project.