Skip to content
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

lz-string for Haxe #133

Open
markknol opened this issue Apr 11, 2019 · 6 comments
Open

lz-string for Haxe #133

markknol opened this issue Apr 11, 2019 · 6 comments

Comments

@markknol
Copy link

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)

@JobLeonard
Copy link
Collaborator

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

@markknol
Copy link
Author

markknol commented Apr 12, 2019

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

@JobLeonard
Copy link
Collaborator

JobLeonard commented Apr 12, 2019 via email

@markknol
Copy link
Author

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

@JobLeonard
Copy link
Collaborator

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.

@ncannasse
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants