Omnimaga

General Discussion => Technology and Development => Computer Projects and Ideas => Topic started by: Nick on January 05, 2012, 06:23:53 pm

Title: Lua code optimizer for pc
Post by: Nick on January 05, 2012, 06:23:53 pm
Hi

based on Deep Thought's idea, i made a program that reduces the file size of lua with about 15% (several tests came out, average reduction..)

it does not yet change anything really coding to the code yet, but:
     - tabs are removed
     - spaces are removed where possible ( var = 5 changes into var=5 )
     - empty new lines are removed
     - \000\ changes in \0\ for images
     - removes the == true in statements (not yet ==false, that will come later)
     - changes platform.window:invalidate() into p() and adds  'function p() platform.window:invalidate() end' because this takes in less place when used more than 2 times (which prob is the case)
     - comments are removed
     - gc:setAlpha(..)'s are removed
     - include external .lua files by using %, followed by the name of the file (without the .lua)
example main file:
Code: [Select]
function on.create()
%varsinit
end
varsinit.lua file:
Code: [Select]
menu = true
game = false
highscore = (var.recall("HS")  or 0)
score = 0

the resulting file is saved in the same directory as the source file, but with improved added to the name, so example.lua changes into exampleimproved.lua
i tried to remove the ,"top" from drawstring, and it all worked fine, but than it seems that it is drawn on baseline, i thought top was default?

anyway, here are screenies

(http://img.removedfromgame.com/imgs/LuaCodeImpr1.gif)

(http://img.removedfromgame.com/imgs/LuaCodeImpr2.gif)
Title: Re: Lua code improver for pc
Post by: epic7 on January 05, 2012, 06:29:24 pm
This like an optimizer?
Looks cool!

No download yet?
Title: Re: Lua code improver for pc
Post by: Nick on January 05, 2012, 06:31:16 pm
yeah, optimizer, that was the word i was looking for xp not yet, it's not totally finished, i would like to add the ,"top" or whatever and the ==false removed before release
Title: Re: Lua code improver for pc
Post by: Adriweb on January 05, 2012, 06:36:14 pm
Great idea, but for this kind of optimizations, people generally use LuaSrcDiet, a (well-known ?) Lua code size-reducer (extreme size decreases....)

Take a look here and try it on your code :
http://code.google.com/p/luasrcdiet/ (http://code.google.com/p/luasrcdiet/)

It's a lua script by itself. :)
Maybe you can take it and modify it to match Nspire Lua needs ? (It messes up the ti.images !!)
And put it together with your nice graphical UI ?

:D
Title: Re: Lua code improver for pc
Post by: renatose on January 05, 2012, 06:37:31 pm
this is great and you could also add a button to export to tns using luna...
Title: Re: Lua code optimizer for pc
Post by: Nick on January 05, 2012, 06:37:48 pm
thanks, didn't know that already existed Ö i'm sad now..

no, i'll have a look at it, might be very useful, since i do not bother variables yet
Title: Re: Lua code optimizer for pc
Post by: cyanophycean314 on January 05, 2012, 07:20:18 pm
That's pretty cool! Too bad there was already one... but good job anyways!  :thumbsup:
Title: Re: Lua code optimizer for pc
Post by: hoffa on January 05, 2012, 07:59:10 pm
Does changing \000 to \0 make any improvements speed-wise?
Title: Re: Lua code optimizer for pc
Post by: Nick on January 06, 2012, 03:31:45 am
i don't really know, but it compresses the filesize a lot, since many pics have those \000

--update--
gc:setAlpha() gets removed too now
Title: Re: Lua code optimizer for pc
Post by: Jim Bauwens on January 06, 2012, 03:48:36 am
Changing \000 to \0 is just an improvement for the storage size. It will not change anything for drawing it.
But there can be a danger with changing it: as you know a TI.Image string contains also normal characters. Lets say you got a part that looks like this:
"\000122" (two pixels)
When you change it, this will happen:
"\0122"
Suddenly, you created a new character, "\012".
You should do some checks on the image data before changing it ;)
Title: Re: Lua code optimizer for pc
Post by: Nick on January 06, 2012, 03:56:40 am
isn't there always a \ between two separate bytes? i've never seen any number longer than 3 digits O.o
Title: Re: Lua code optimizer for pc
Post by: Jim Bauwens on January 06, 2012, 03:59:07 am
No, I guess you don't understand :P
I'll separate the digits by space:
"\000 1 2 3"
When you change "\000" to "\0" this happens:
"\012 3"
It will think 1 and 2 are part of the escape sequence, and bad things will happen.
Title: Re: Lua code optimizer for pc
Post by: Nick on January 06, 2012, 04:01:51 am
ok, then i'll make it change \000\ into \0\, problem solved?
Title: Re: Lua code optimizer for pc
Post by: Jim Bauwens on January 06, 2012, 04:06:48 am
No, let me explain the \
It is used to create escape characters.
\xxx is the same as a normal character.
For example "\065" == "A".
The three (or less) digits represent the decimal code for the character.
Most characters in a TI.Image can't be displayed, so escape characters are used.
1, 2 and 3 are real characters in my example, they don't belong to an escape sequence.
What can happen is that they do become part of an escape sequence because of the conversion (and unexpected stuff will happen)
Title: Re: Lua code optimizer for pc
Post by: Nick on January 06, 2012, 04:12:55 am
oh, now i got it.. hmm, yeah, never thought of that xs

but if i use the \000\ it will work, since then the char after it is such an escape char too, so it won't be disturbing anything, but you're right about the \000
Title: Re: Lua code optimizer for pc
Post by: Jim Bauwens on January 06, 2012, 04:43:33 am
Yes, that should do :)
Title: Re: Lua code optimizer for pc
Post by: Nick on January 06, 2012, 04:47:45 am
thanks, changed it :)
Title: Re: Lua code optimizer for pc
Post by: Jim Bauwens on January 06, 2012, 04:52:26 am
This is a bit more trickier, but it should work too.
Escape sequences like "\012" or "\002" don't need the 0's too. But of course you must be sure they aren't followed by a normal character.
I suppose you might need some regular expressions for this.
Title: Re: Lua code optimizer for pc
Post by: Nick on January 06, 2012, 04:55:24 am
hmm, that's indeed a bit tricky xp

but i'll try it

are there any other optimizations in the code itself?
what is the default setting for text-align? since that default can be removed too..
Title: Re: Lua code optimizer for pc
Post by: Jim Bauwens on January 06, 2012, 04:56:38 am
Don't remove it, you need it for compatibility in newer versions AFAIK.
Title: Re: Lua code optimizer for pc
Post by: Nick on January 11, 2012, 01:36:23 pm
UPDATE!!!

include external .lua files by using %, followed by the name of the file (without the .lua)

example main file:
Code: [Select]
function on.create()
%varsinit
end


varsinit.lua file:
Code: [Select]
menu = true
game = false
highscore = (var.recall("HS")  or 0)
score = 0

results in:
Code: [Select]
function on.create()
menu = true
game = false
highscore = (var.recall("HS")  or 0)
score = 0
end
Title: Re: Lua code optimizer for pc
Post by: Adriweb on January 11, 2012, 02:23:25 pm
UPDATE!!!

include external .lua files by using #, followed by the name of the file (without the .lua)

example main file:
Code: [Select]
function on.create()
#varsinit
end


What ??

#varsinit returns the number of elements in the "varsinit" table ...
You've overriden the metatables ?
Title: Re: Lua code optimizer for pc
Post by: Nick on January 11, 2012, 02:26:24 pm
oh, forgot about that.. thanks for reminding me Ö i feel quite ashamed right now for not having thought of it myself  :-[


i changed it to %<filename>

i hope this isn't a problem?
Title: Re: Lua code optimizer for pc
Post by: Jim Bauwens on January 11, 2012, 03:53:48 pm
You should make that it only works in the beginning of the file, since % is used for modulus and stuff.
But nice idea anyway.
Title: Re: Lua code optimizer for pc
Post by: Nick on January 11, 2012, 03:57:14 pm
nah, i want to make it work through the whole file, since it might be useful to make whole parts in different files, and then paste them together.
i did not only do it to include initializations, but for functions and other stuff too

so maybe §<filename> ? i guess that's not used?

and just to be clear, it only adapts those who are at the total left border of the page, it does not change the ones that are in formulas or so..
Title: Re: Lua code optimizer for pc
Post by: epic7 on January 11, 2012, 06:22:01 pm
I don't have that wierd S-like symbol on my keyboard, and I have no clue what it means :P

Is it some foreign (to me :P) currency?
Title: Re: Lua code optimizer for pc
Post by: Nick on January 12, 2012, 01:02:43 am
no, it's  a paragraph sign.. well, if you don't have it, then i won't take it, maybe & or @ ?

aaargh, i really need to find a symbol for it :)
Title: Re: Lua code optimizer for pc
Post by: Nick on January 12, 2012, 11:16:17 am
UPDATE!!

now it saves settings to an external file, possible settings:

Possibilities for include file:

in fact, i like the second one the most, maybe that one?

the lay-out of the program itself changed too, but not that much to upload a new screenie

this is the settings pop-up screen:
Title: Re: Lua code optimizer for pc
Post by: epic7 on January 13, 2012, 09:43:37 pm
UPDATE!!!

include external .lua files by using #, followed by the name of the file (without the .lua)

example main file:
Code: [Select]
function on.create()
#varsinit
end


What ??

#varsinit returns the number of elements in the "varsinit" table ...
You've overriden the metatables ?

Does that mean
table={1,2,3,4,5,6,7,8,9}
x=#table
And x would equal 9?
That would be useful to use :P
Title: Re: Lua code optimizer for pc
Post by: Nick on January 14, 2012, 04:46:55 pm
yeah, that means that :)