-
Notifications
You must be signed in to change notification settings - Fork 569
decompressFromUint8Array optimisation #80
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
Conversation
Remove the redundant temporary buffer, and push the output character directly into the output buffer. This saves memory and is quicker as it doesn't need to iterate twice.
|
Sorry for the delay. I will setup a small jsperf.com test with this vs the head of the repo to make sure it works on all browsers. When that's done a we have a little data I'll merge it. Thanks for the effort. |
|
But jsperf.com is down :-/ |
|
It has been for most of this year - makes life a lot harder for testing stuff :-/ You can manually test in multiple browsers - but the code can be eyeballed as being identical in function, just not using a temporary buffer. |
|
@pieroxy While we're on this topic, do you know why compressToUint8Array and decompressFromUint8Array don't use _compress and _decompress directly, and instead adds an extra conversion step using a UCS-2 string? For example, the code sample below is fully compatible with the current implementation: compressToUint8Array: function (input) {
var compressed = LZString._compress(input, 8, function (a) {
return f(a);
});
// For backwards compatibility with old format using UCS-2
if (compressed.length % 2 !== 0) {
compressed = compressed + f(0);
}
var buf = new Uint8Array(compressed.length);
for (var i = 0, TotalLen = compressed.length; i < TotalLen; i++) {
buf[i] = compressed.charCodeAt(i);
}
return buf;
},
decompressFromUint8Array: function (compressed) {
if (compressed === null || compressed === undefined) {
return LZString.decompress(compressed);
}
else {
if (compressed.length == 0) return null;
return LZString._decompress(compressed.length, 128, function (index) {
return compressed[index];
});
}
},If you think this new implementation is worth a shot, I could send in a PR. |
|
@pieroxy worth noting that jsperf.com is back online! |
|
Right, thanks! |
|
@lishid: Actually, this can be optimised even further for So if we make a new function, say return context_data.join('');becomes: return context_data;And then rewrite _compress: function (uncompressed, bitsPerChar, getCharFromInt) {
return compressToArray(uncompressed, bitsPerChar, getCharFromInt).join('')
}(which makes it backwards compatible with existing compressors at the cost of a single extra function call), then we can change your code to: compressToUint8Array: function (input) {
var compressed = LZString.compressToArray(input, 8, function (a) {
return f(a);
});
// For backwards compatibility with old format using UCS-2
if (compressed.length % 2 !== 0) {
compressed.push(f(0));
}
// we could even replace this with Uint8Array.from(compressed)
// if not for backwards compatibility
var buf = new Uint8Array(compressed.length);
// this can theoretically be sped up even further with loop-unrolling:
// http://jsbench.github.io/#80b894adfe3e635ef03e7f6ebc55d086
for (var i = 0, TotalLen = compressed.length; i < TotalLen; i++) {
buf[i] = compressed[i];
}
return buf;
},This saves use the creation of a string, and array lookup is probably faster than |
|
@JobLeonard I didn't even look at it that deeply - nice - if you make that into a PR this one can be closed :-D |
|
@Rycochet: look a bit closer: my optimisation is for compression, yours for decompression ;). The mistake is understandable though, they're doing similar things. You know what, tomorrow I'll try to combine all suggestions in this thread and push a PR! |
|
@JobLeonard Doh, apparently I didn't look back at my change either lol - go for it ;-) |
|
Closing in preference of #98 - includes this and other code in these comments. |
Remove the redundant temporary buffer, and push the output character directly into the output buffer. This saves memory and is quicker as it doesn't need to iterate twice.
(Minified version not updated as edited on GitHub directly)