It is currently Fri Apr 28, 2017 1:09 am



Welcome
Welcome to rfobasic

You are currently viewing our boards as a guest, which gives you limited access to view most discussions and access our other features. By joining our free community, you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content, and access many other special features. In addition, registered members also see less advertisements. Registration is fast, simple, and absolutely free, so please, join our community today. **You are not required to provide truthful information to any registration questions. Be whomever you wish to be.!


Post new topic Reply to topic  [ 8 posts ] 
Author Message
 Post subject: function name optimizer
Unread postPosted: Sun Nov 08, 2015 11:36 pm 
Offline

Joined: Wed Apr 15, 2015 3:57 am
Posts: 423
So I am pretty sure that using shorter function names can dramatically increase speed so I decided to try making
a function name optimizer program to squeeze out more speed from my .bas programs

Basically, all it does is look for FN.DEF and extract out the function names and enumerate them to (usually) shorter and obfuscated names.

I ran it on some of my programs and got 4-10% savings in code size... (It saved about 10% on GW.bas but I have to re-install the themes to test the demo out).. I use it along with my merge INCLUDE program to reduce source file sizes and hopefully increase interpreter speed and load times

I haven't fully tested it so I am not sure it is perfect so please use at your own risk..

Code:

!
! function optimizer/obfuscator
!
!

FN.DEF nocasereplace$(s$, f$, r$)
start=1
DO
  i=IS_IN(LOWER$(f$),LOWER$(s$),start)
  IF !i THEN D_U.BREAK
  s$=left$(s$,i-1)+r$+right$(s$,len(s$)-i-len(f$)+1)
  start=i+LEN(r$)
UNTIL !i
FN.RTN s$
FN.END

FN.DEF optfn(p$)
TEXT.OPEN r, h1, p$
LIST.CREATE s, funcr

! scan for function names
DO
  TEXT.READLN h1, a$
  eof=(a$="EOF")
  IF eof THEN D_U.BREAK

  a$=TRIM$(a$)

  IF IS_IN("fn.def ",LOWER$(a$))

   fcount++
   d$="("

   a$=nocasereplace$(a$,"fn.def ","")

   i=IS_IN(d$,a$)

   fname$ = LOWER$(MID$(a$,1,i-1))

list.size funcr,siz
if siz>1
j=0
do
j++
list.get funcr,j,l$
until len(fname$)>=len(l$) | j>=siz
LIST.insert funcr,j,fname$
else
list.add funcr,fname$
endif


   PRINT fname$,fcount
  ENDIF
UNTIL eof
PRINT "eof"
TEXT.CLOSE h1

GRABFILE prog$, p$
oldl=len(prog$)
! replace function names
LIST.SIZE funcr, size
FOR i = 1 TO size
print i
  LIST.GET funcr, i, oldf$
  newf$="f"+int$(i)
if is_in("$",oldf$) then newf$+="$"
  prog$=nocasereplace$(prog$, oldf$+"(", newf$+"(")
  PRINT "replaced "+oldf$
NEXT i
newl=len(prog$)
TEXT.OPEN w, h, "../source/output.bas"
TEXT.WRITELN h, prog$
TEXT.CLOSE h

print int$(100*(oldl-newl)/newl)+"% saved!"

FN.END

input "enter file to optimize", p$
optfn("../source/"+p$)

end


Top
 Profile  
 
 Post subject: Re: function name optimizer
Unread postPosted: Tue Nov 10, 2015 5:29 am 
Offline

Joined: Wed Apr 15, 2015 3:57 am
Posts: 423
Actually the position of the function name has a much more dramatic effect. I was surprised when I ran this and stuck in GW-lib as a test code:


Code:
fn.def f2()
i=2
fn.rtn i
fn.end

include GW.bas
html.close

fn.def f1()
i=2
fn.rtn i
fn.end

x=100000
do
print x, " iter"
s=clock()
for i=1 to x
f2()
next i
print clock()-s
s=clock()
for i=1 to x
f1()
next i
print clock()-s

x=x/10
until x<=100

end


On my phone function f2() ran 3x faster than f1()!

Still cutting down on the overall size of the source file is probably not a bad idea.


Top
 Profile  
 
 Post subject: Re: function name optimizer
Unread postPosted: Tue Nov 10, 2015 6:36 am 
Offline
User avatar

Joined: Tue Jan 03, 2012 9:31 am
Posts: 5487
Location: Paris, France
Yes that is because currently variable lookup (including function names) is linear.
It scans names starting at #1: your f2() function, going through the numerous GW functions, and ending at #last: your f1() function.
This should change if we manage to switch to a variable binary search.

_________________
- Creator of the Android BASIC! Compiler


Top
 Profile  
 
 Post subject: Re: function name optimizer
Unread postPosted: Tue Nov 10, 2015 6:24 pm 
Offline

Joined: Wed Apr 15, 2015 3:57 am
Posts: 423
It's probably not a huge deal since I don't mind sticking the most time critical functions near the top of my source files.
Hopefully binary search won't cost too much memory or increase the distribution apk sizes too much.. things run really zippy right now if you do a bit of tweaking. As it is my entire food diary app is only a 650KB apk.. pretty lean!


Top
 Profile  
 
 Post subject: Re: function name optimizer
Unread postPosted: Tue Dec 22, 2015 5:08 pm 
Offline

Joined: Wed Apr 15, 2015 3:57 am
Posts: 423
Ok... so I have been working on my mergeinc program optimizer and ran it on GW demo version 1.9...
It shrunk the entire program from 173K down to about 114K

It seems to run a lot faster with the smaller function names. seems to have a weird bug though since I can't get the dialogs to pop up in my optimized version. Might have something to do with spaces in front of function ( 's like fn.def f () instead of fn.def f()



Compare GWdemoopt.bas which is optimized versus the original GWdemooptdev.bas

http://laughton.com/basic/programs/tools/MergeIncludes/


Top
 Profile  
 
 Post subject: Re: function name optimizer
Unread postPosted: Wed Dec 23, 2015 2:25 am 
Offline
User avatar

Joined: Thu Jan 08, 2015 11:28 am
Posts: 1088
Location: .NET
Fn.def f () = fn.def f() = fn.deff()

All spaces get pushed out before interpreting

_________________
https://github.com/evolbug
http://toobasic.jimdo.com


Top
 Profile  
 
 Post subject: Re: function name optimizer
Unread postPosted: Thu Dec 24, 2015 11:14 pm 
Offline

Joined: Wed Oct 03, 2012 9:53 am
Posts: 2795
Location: Colorado, U.S.
The interpreter never needs the spaces, but there is one case where the preprocessor requires them. I think this is the only case.

Single-line IF is like this:
Code:
IF <cond> THEN <statement> ELSE <statement>

However, if you make either of the <statement>s into a compound statement, it can no longer be a single-line IF:
Code:
IF x < y THEN a = b : c = d ELSE x = y
The preprocessor does you a favor: it converts this into a standard IF statement:
Code:
IF x < y THEN : a = b : c = d : ELSE : x = y : ENDIF
There is a limitation in the preprocessor: it can't do this IF conversion unless there are spaces around the THEN and the ELSE.

I don't think there is any problem with space or no space after a function name.

- Marc


Top
 Profile  
 
 Post subject: Re: function name optimizer
Unread postPosted: Fri Dec 25, 2015 8:10 pm 
Offline

Joined: Wed Apr 15, 2015 3:57 am
Posts: 423
no problems with BASIC! interpreting spaces around functions.. Just my function name-shortening program isn't smart enough
to tell the difference between an expression with parentheses and a function name... I'll work on it more. Thanks.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 8 posts ] 


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
suspicion-preferred