It's been brought to my attention that many people are unhappy with how SourceCoder optimizes programs. The optimization features are actually not of my own design, but integrated from a project a half-decade ago called OptiBASIC by UnitedTI member Brazucs. I knew that the features worked relatively poorly, but always figured they were more of a guide to coders than something to pass your code through for optimization. I assumed people would prefer to just learn good coding habits and do hand-optimization. Here's what SourceCoder currently (tries) to do:

Check out this post if you want to see the Regex that makes this all possible

:: Remove a closing ) or }, but only if it's followed by a :, a newline, the end of the program, or some character I don't recognize with Unicode ID 8594.
:: Remove a single-quote followed by a newline, provided that it's not delimiting a blank line
:: Replace a number by itself in a string with just the number, to save the quote byte.
:: Remove the Then and End around a single statement after an If, including constructions like If \ Then \ Goto \ End.
:: Remove the construction Output(1,1,". I'm unsure what this is for.
:: Convert Disp "string" \ Pause to Pause "string"
:: Turn Stops into Returns. Because, come on.
:: Remove any Return that's the last command in a program
:: Replace 0->var with Delvar var
:: Remove the newline or colon after a Delvar.
:: I'd have to look closer at this to understand fully the logic it applies, but the high-level interpretation is that it changes If BLAH!=0 to If BLAH. and If BLAH=0 to If not(BLAH.
:: Remove the final ,1 increment in For( loops with it.
:: Replace ^2 with the exponent 2.
:: Same with ^3 -> exponent 3.
:: Implicit multiplication creation.
:: No point having 1 as a coefficient.
:: Replace series of Disp A \ Disp B or Prompt A \ Prompt B with Disp A,B or Prompt A,B
:: combine boolean expressions
:: more of same.
:: This massively complex thing does some kind of If \ Store statement compression to value(conditional)->variable expression
:: Remove blank lines
:: I have no idea. I'd have to work this one out more carefully.
:: No point storing to Ans.
:: remove extraneous parenthesis around division
:: same thing, but for the first half of the division
:: Remove extraneous addition and subtraction parentheses, but I think this one might be functionally incorrect (same with the division)
:: Combine Disp / Input or Disp / Prompt into a single Input or Prompt.

MY QUESTIONS TO YOU
(1) Would it be worth my time for the optimizer to work better?
(2) If yes to (1), tell me what you want the optimizer to do.
Problem with optimizing is you can only do so many of the basics of it. You have most of it. However, I would like to offer at least one thing that should be added to sourcecoder.

Making sure that there is a ) at the end of a For( command. That ) speeds up the loop.

Should we try to come up with a list of common things, whether they are already done or not, that people should just learn to do regardless? Might help with creating something a little more.. intelligent.
tifreak8x wrote:
Making sure that there is a ) at the end of a For( command. That ) speeds up the loop.
That is only true if the next command is an If, other wise it makes no difference, and in reality it can be any byte, even an extra : before the If works.
Hmm, I dunno, I noticed a slight speed improvement on the 83+ while testing my pokemon party menu. Just something that I figured might be brought up.
tifreak8x wrote:
Problem with optimizing is you can only do so many of the basics of it. You have most of it. However, I would like to offer at least one thing that should be added to sourcecoder.

Making sure that there is a ) at the end of a For( command. That ) speeds up the loop.

Should we try to come up with a list of common things, whether they are already done or not, that people should just learn to do regardless? Might help with creating something a little more.. intelligent.
That's exactly what I think would be helpful. I'd appreciate any suggestions from anyone. Some of the things should be universally true, some may have limited exceptions. I will need to add the knowledge of when it's in a string, since that seems to be a big current shortcoming.

I agree with TheStorm's criticism of that one; what do you think, Dan?
Do bug reports also go here? Since there are a few things i noticed that were incorrect optimizations that produced syntax errors.

As for more optimizations, there are a few that i came up with.

replacing .01 with Sub(1), .02 with Sub(2) ext... 1 byte less if last expression on line.

/100 to .01
/50 to .02
/25 to .04
/20 to .05
/10 to .1

powers of 10 greater than 2 convert to scientific notation

Subtracting a negative into addition

Adding a negative into subtraction

And then i had an idea to isolate individual parts of expressions to move around so that the parts with the most parenthesis are at the end Smile
Aye. For things that can be moved around, regexes are extremely powerful. The problem is that there's no good way to do a stateful ignoring of things inside strings. Smile And I'd appreciate the bug reports, although going through what SourceCoder currently does today I noticed a few things that will not only produce incorrect code, but also incorrect results when run. I like all your ideas, and I think most of them are feasible.

At this stage I'm starting to think that I'm going to need some kind of way of running a two-stage optimizer, perhaps the first stage a state machine to replace all the strings with some placeholders and do rudimentary things like end-of-line parenthesis and anything with special cases about when they may and may not be used, then a second pass that does anything globally find-and-replaceable things (and then a third that puts the strings back in, but we won't mention that).
What do you mean by "stateful ignoring of things inside strings"?
I think there is some tricky things to be done with passes and such, because i came across an instance where an optimization at one point created an optimization behind it:

Output(0,0,"HI")

You cant remove the closing quotes, because then the parentheses would be in the sting. Then the optimizer removes the parentheses and suddenly the quotes can be optimized, but we already passed them D:
swivelgames wrote:
What do you mean by "stateful ignoring of things inside strings"?
Going through the source code with a state machine that will suspend optimizations until a newline or ending quote, or making a state machine to remove string into some literal string cache and later re-insert them, or something along those lines.

@Builderboy: Yeah, those kinds of things are problematic. I meant that each of those two passes should be multiple sub passes. The current SourceCoder setup repeatedly optimizes the program until two successive passes either does not change the size, or makes it larger. I would hope to apply something like that to this.
":: Replace 0->var with Delvar var"

Sometimes ^ this can get a tad annoying
qazz42 wrote:
":: Replace 0->var with Delvar var"

Sometimes ^ this can get a tad annoying
Can you be more specific about that? "Annoying" as in it confuses you, or "annoying" as in it produces functionally incorrect code?
(not sure I'm in the good topic)

Hello, I was using Sourcecoder to optimize my code.

And he replaces the line

Code:
Text(-1,14,30,"text"

by

Text(-14,30,"text"


So I tried it on my calculator, and it said "error Data Type".

So I ask you if the problem comes from Sourcecoder or from my TI 83+...

Why a "Data Type" error and not "invalid" or "domain" error ?
What is the command waiting for argument ?

Thanks for your answers...
persalteas wrote:
(not sure I'm in the good topic)

Hello, I was using Sourcecoder to optimize my code.

And he replaces the line

Code:
Text(-1,14,30,"text"

by

Text(-14,30,"text"


So I tried it on my calculator, and it said "error Data Type".

So I ask you if the problem comes from Sourcecoder or from my TI 83+...

Why a "Data Type" error and not "invalid" or "domain" error ?
What is the command waiting for argument ?

Thanks for your answers...

It definitely shouldn't do that. I'll let Kerm decide if he wants to split your topic or leave it here.
Persalteas, that's a bug in SourceCoder. The optimization mode has never been completed; it was taken from a project by Brazucs (aka Joe Penna, aka MysteryGuitarMan) when his OptiBASIC was merged into SourceCoder. It would be best if it was somehow made into a parser that would render an AST of the BASIC code and work with that, but as a look-forward optimizer, it's relatively limited. I recommend that you instead learn clever optimization tricks on your own to apply. Smile

By the way, I've greeted you in our SAX chat widget at far left, and I recommend that you Introduce Yourself when you get a chance.
okay. thanks.
KermMartian wrote:
:: Remove the construction Output(1,1,". I'm unsure what this is for.

If the last instruction is an Output (I think it has to be on the first row of the screen, but I'm not sure), then the "Done" indicator does not display. I used to put that at the end of my programs because I don't like it always having to say "Done" at the end Razz
shkaboinka wrote:
KermMartian wrote:
:: Remove the construction Output(1,1,". I'm unsure what this is for.

If the last instruction is an Output (I think it has to be on the first row of the screen, but I'm not sure), then the "Done" indicator does not display. I used to put that at the end of my programs because I don't like it always having to say "Done" at the end Razz
It does not have to be on the first row. I usually use Output(2,7," instead of the standard 1,1 for no reason at all.
I hate that the output trick does not work on the MP OS's anymore.. It bugs me. That Done is the most annoying thing in the world.
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
 
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 vote in polls in this forum

 

Advertisement