Author Topic: Lua code optimizer for pc  (Read 12189 times)

0 Members and 1 Guest are viewing this topic.

Offline Nick

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1166
  • Rating: +161/-3
  • You just got omnom'd
    • View Profile
    • Nick Steen
Lua code optimizer for pc
« 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



« Last Edit: January 11, 2012, 03:22:13 pm by Nick »

Offline epic7

  • Chopin!
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2200
  • Rating: +135/-8
  • I like robots
    • View Profile
Re: Lua code improver for pc
« Reply #1 on: January 05, 2012, 06:29:24 pm »
This like an optimizer?
Looks cool!

No download yet?

Offline Nick

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1166
  • Rating: +161/-3
  • You just got omnom'd
    • View Profile
    • Nick Steen
Re: Lua code improver for pc
« Reply #2 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

Offline Adriweb

  • Editor
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1708
  • Rating: +229/-17
    • View Profile
    • TI-Planet.org
Re: Lua code improver for pc
« Reply #3 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/

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
« Last Edit: January 05, 2012, 06:37:09 pm by adriweb »
My calculator programs
TI-Planet.org co-admin.
TI-Nspire Lua programming : Tutorials  |  API Documentation

Offline renatose

  • LV5 Advanced (Next: 300)
  • *****
  • Posts: 209
  • Rating: +4/-0
  • watch out the power balls
    • View Profile
Re: Lua code improver for pc
« Reply #4 on: January 05, 2012, 06:37:31 pm »
this is great and you could also add a button to export to tns using luna...

Offline Nick

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1166
  • Rating: +161/-3
  • You just got omnom'd
    • View Profile
    • Nick Steen
Re: Lua code optimizer for pc
« Reply #5 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

Offline cyanophycean314

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 363
  • Rating: +43/-1
  • It's You!
    • View Profile
Re: Lua code optimizer for pc
« Reply #6 on: January 05, 2012, 07:20:18 pm »
That's pretty cool! Too bad there was already one... but good job anyways!  :thumbsup:

Offline hoffa

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 304
  • Rating: +131/-13
    • View Profile
Re: Lua code optimizer for pc
« Reply #7 on: January 05, 2012, 07:59:10 pm »
Does changing \000 to \0 make any improvements speed-wise?

Offline Nick

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1166
  • Rating: +161/-3
  • You just got omnom'd
    • View Profile
    • Nick Steen
Re: Lua code optimizer for pc
« Reply #8 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
« Last Edit: January 06, 2012, 03:32:27 am by Nick »

Offline Jim Bauwens

  • Lua! Nspire! Linux!
  • Editor
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1881
  • Rating: +206/-7
  • Linux!
    • View Profile
    • nothing...
Re: Lua code optimizer for pc
« Reply #9 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 ;)

Offline Nick

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1166
  • Rating: +161/-3
  • You just got omnom'd
    • View Profile
    • Nick Steen
Re: Lua code optimizer for pc
« Reply #10 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

Offline Jim Bauwens

  • Lua! Nspire! Linux!
  • Editor
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1881
  • Rating: +206/-7
  • Linux!
    • View Profile
    • nothing...
Re: Lua code optimizer for pc
« Reply #11 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.

Offline Nick

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1166
  • Rating: +161/-3
  • You just got omnom'd
    • View Profile
    • Nick Steen
Re: Lua code optimizer for pc
« Reply #12 on: January 06, 2012, 04:01:51 am »
ok, then i'll make it change \000\ into \0\, problem solved?

Offline Jim Bauwens

  • Lua! Nspire! Linux!
  • Editor
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1881
  • Rating: +206/-7
  • Linux!
    • View Profile
    • nothing...
Re: Lua code optimizer for pc
« Reply #13 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)

Offline Nick

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1166
  • Rating: +161/-3
  • You just got omnom'd
    • View Profile
    • Nick Steen
Re: Lua code optimizer for pc
« Reply #14 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