pieroxy / lz-string

LZ-based compression algorithm for JavaScript

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

lz-string for Haxe

markknol opened this issue · comments

Just wanted to let you know I've ported lz-string to Haxe
Find the source here https://github.com/markknol/hx-lzstring

(This allows Haxe developers to use the library in all of Haxe's targets; JavaScript, PHP, C++, Lua, Python, Java, C# and its own interpreter / VM's)

Nice! I'm also curious what the JavaScript version of compiled Haxe lookes like

It will look like this
https://gist.github.com/markknol/1012a7ad76d9cffd363d7c29656e4105

Some notes to understand where you are looking at:

  • Including the test it is 18kb (Minified this will be ~7kb. gzipped thats ~3kb)
  • As comparison, the original lz-string.js in this repo is 16kb
  • Haxe always bundles all code into one file
  • In my version I changed the dictionaries for a typed Map, which makes it type safe and work on all Haxe targets, but it adds some standard library code.
  • Haxe has a static analyzer that does dead code elimination, so unused functions are stripped. In my example I only use compressFromBase64 and decompressFromBase64, you won't see the compressFromUint8 function for example.
  • I added both ES5 and ES6 ouput

What do you mean with optimize away? It converts ints to string.

This case is actually already quite nice. it unrolls a loop, this is the code I wrote

		for (i in 0...3) {
			dictionary[i] = '$i';
		}

https://github.com/markknol/hx-lzstring/blob/master/src/lzstring/LZString.hx#L337-L339

Well, since these are constants it could have done further strength reduction to:

    var dictionary_h = { };
    /* ... */
    dictionary_h[0] = "0";
    dictionary_h[1] = "1";
    dictionary_h[2] = "2";

And since the full context of that bit of code is:

    var dictionary_h = { };
    /* ... */
    dictionary_h[0] = "" + 0;
    dictionary_h[1] = "" + 1;
    dictionary_h[2] = "" + 2;

Theoretically it could even have reduced it further to:

    var dictionary_h = { 0: 0, 1: 1, 2: 2 };

The last optimization would probably be a difficult pattern for the average compiler to detect, but the first one seems pretty straightforward.

Yes we don't optimize string concat in order to limit the number of strings in the string pool on some targets. We could optimize for JS.