Program is too large. How do I make it smaller?
jeff-o
Posts: 181
I'm working on a pretty complex program (at least for me!) that is too large to fit. At the moment it is too large by 100 longs. Inefficient programming? Likely. But there's still a bit of code that I need to add, so I need to drop some weight. I'm doing what I can to reduce it's size; here's what I've done so far:
Moved common instructions to functions so that I'm not repeating code. There may be more that I can do here, I will have to stare at it more.
I'm writing a lot of stuff to a 4x20 LCD screen. I found that it took less space to write the text to screen using a single string, rather than writing each line with a separate string. So that's what I did.
I've got a 64K EEPROM, so some variables that only need to be read at startup are stored there. I think I could store more strings in there to save program space.
Redundancies removed where possible...
So what else can I do? I need to stick with spin code only, since I don't know how to write in assembly. Got any tricks that I could try? Thanks!!
Moved common instructions to functions so that I'm not repeating code. There may be more that I can do here, I will have to stare at it more.
I'm writing a lot of stuff to a 4x20 LCD screen. I found that it took less space to write the text to screen using a single string, rather than writing each line with a separate string. So that's what I did.
I've got a 64K EEPROM, so some variables that only need to be read at startup are stored there. I think I could store more strings in there to save program space.
Redundancies removed where possible...
So what else can I do? I need to stick with spin code only, since I don't know how to write in assembly. Got any tricks that I could try? Thanks!!
Comments
I haven't seen much code that needs all the 32kB except VGA-double-buffers.
Do you use real big stack-variables?
I mean
There is an object to measure the needed stacksize.
Did you check the value-range of the variables you use?
especially arrays
If the value-range stays within 65535 you can change the variable-type to word saving two bytes compared to a long
If the value-range stays withing 255 you can change to variable-type to byte saving three bytes compared to a long
Did you already store ALL strings in the upper 32kB?
Best thing would be to post your entire code. But maybe you don't want to.
Anyway how many kB do all the strings need in your code?
(throw out the guitar user manual shown on the lcdisplay ;-)
best regards
Stefan
Delete any unused methods in objects you're using. (Brad's Spin Tool has a setting to do this for you if you use it insead of the PropTool.) A lot of "features" are actually code you're not using. I find I end up with special versions of nearly all objects for various projects because of this kind of optimization.
Reuse PASM images after the cogs are launched. This is actually kind of a pain in the rear end to do in Spin but it is possible; add functions to objects with PASM images to return the image location and length, and use direct memory access to reuse that memory for storage. Using a PASM image for a video buffer is a big win.
At the moment not all my strings are in the upper 32K of memory. I'll move them tonight to see if that helps. I'll also post code then (I'm posting from an iPhone now)
If you feel up to the task, I'd suggest porting your project to Spin and the underlying OBEX objects 12blocks uses to provide I/O. If you've got the logic debugged this is half the battle, so you can concentrate on implementing working functions in the new environment and syntax.
I'll second that - I've found that can save hundreds of longs, sometimes even double the code space.
Then again, I ran out of code space years ago, which is why I moved over to C on the propeller. Drop in the appropriate memory chip (64k, 512k or 32Mb) and there is always enough free memory.
One thing that helped in all of this (for anyone traveling the same path as me) was to build a small program that basically spits out all of the used and free memory in the upper 32K of EEPROM. I noticed some strangeness (including negative space remaining, presumably an overflow) that led me to suspect some of my memory location settings were not correct.
And now it is very late and I really should have gone to bed two hours ago. I'm gonna pay for this tomorrow...
Anyway, here's my code which I am releasing with great reluctance. I tend to hold my cards close to my chest, especially on something I've been working on for months. All of this will be released officially this summer, so please don't steal it ok?
Attached are my main three program files in .12b format. These will open just fine in the prop tool, but open them in 12blocks to really see what's going on. Also included are all the various objects needed to compile the project.
So yeah, if you're interested in having a go at my code, please feel free. Any help in compacting it a little would be greatly appreciated.
Is there a way to convert the 12blocks files to Spin? I have never used 12blocks before, so I installed it and tried to use it. I was able to view your source files, but I'm not able to view them as Spin files. I get an error from 12blocks saying that it can't save the file because of some XML error.
BTW, 12blocks looks interesting, but it reminds of the first time I used a Mac computer. I kept hoping I could find a DOS prompt.
Dave
If you're still having trouble I will export my original files to .spin and post them. I also need to upload my custom hardware file that goes into 12blocks' hardware folder...
Most of the 3,314 longs of variables are in the VAR space used by the four instances of Karplus-Strong. You could save space with fewer instances, or reduce the size of the buffer it uses.
I need all four instances of Karplus Strong though - one per string, all running at the same time. I'm especially excited about BST though, that will give me some breathing room.
I may be wrong about the buffer size thing. You should contact lonesock to find out for sure.
After reducing the sampling rate and buffer size I'm able to compile in 12blocks again, or in BST (love BST btw, kicking myself for not trying it sooner!)