473,396 Members | 1,785 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Techniques for reducing code size?

I already know some techniques for writing small footprint software,
but now I need to really squeeze the software I'm writing into a small
space (embedded software).

Is there a good web page that discsuesses techniques for this?
A good book?

/Martin
Nov 14 '05 #1
2 13332
In <e6*************************@posting.google.com> d8*****@dtek.chalmers.se (Martin) writes:
I already know some techniques for writing small footprint software,
but now I need to really squeeze the software I'm writing into a small
space (embedded software).


1. If your compiler has the option to optimise for size (instead of
speed), use it.

2. If #1 hasn't solved your problem, recode in assembly as much as
necessary.

The C language doesn't address the issue of code size, so the only things
you can do at the C source code level are:

o Reduce the amount of data allocated by the program. Reuse your data
as much as possible.

o Prefer simple algorithms to more complex ones. The most efficient
algorithms tend to be (much) more complex than some of the less
efficient ones or to use more data. Compare Eratosthenes' sieve with
a purely computational method of generating prime numbers.

If you have constraints of both space and speed, you may be forced to
resort to assembly.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #2
Martin wrote:
I already know some techniques for writing small footprint software,
but now I need to really squeeze the software I'm writing into a small
space (embedded software).

Is there a good web page that discsuesses techniques for this?
A good book?

/Martin


Have you tried optimizing the design?
Have you tried removing requirements?

Since this is an embedded system, have you tried searching
news:comp.arch.embedded?

When I optimize for space, I perform the following
steps, not necessarily in the order listed:

1. Remove or inline simple functions.
A simple function is a function that has code that
is smaller or equal to the calling overhead.
Inlining is to either convert the function to a
macro or just cut and paste wherever it is used.

2. Remove all instances of division and modulo.
Replace with shifts, adds, subtracts and masking.
My experience is that many of the math libraries
are huge and that most division on an embedded
system can be replaced by another process or
eliminated entirely.

3. Replace many "if" statements with boolean arithmetic.
Because the C language provides short-circuiting with
the logical boolean operators, these can be used
like "if" statements without having the second
expression evaluated:
unsigned char result;
result = /* evaluation of condition 1 */
result = result && /* eval. of condition 2 */

In the above line, condition 2 will only be evaluated
if the result is nonzero (i.e. true).

4. If it doesn't change, make it const.
Some embedded systems may have more read-only memory
than read/write memory. Some compilers may allow
constants to be relocated outside of the executable
code.

5. Simplify algebraic expressions.
If some statements evaluate an algebraic expression,
see if the algebraic expression can be simplified
before it is implemented. Same holds true for boolean
algebraic expressions.

6. Factor out duplication.
If the duplication is more than the overhead of a
function call then factor out the duplication into
a new function.

7. Remove deadwood.

8. Apply platform specific code crunching.
Replace functions with assembly language functions,
if and only if they can be coded more efficiently
than the compiler's version. Take advantage of
specialized processor instructions.

For more suggestions, try posting to news:comp.programming.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book

Nov 14 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Peter Salzman | last post by:
Hi all, Newish PHP programmer here. I wrote a form select class to help reduce code clutter, but I don't think the effort was worth it. I was hoping to post my attempt and get some ideas from...
5
by: Salvador I. Ducros | last post by:
Greetings all, I was hoping someone might be able to point me in the right direction. I'm currently using std::vector to maintain several lists whose objects are of unrelated types (classes)....
1
by: Hank Reed | last post by:
Hello, I have not seen this question addressed in older threads. I am wondering about the practical limit for code size in Access 2000 on a project I have been working on for five years that runs...
0
by: JollyK | last post by:
Looking for opinion on code size for presention tier asp.net application. I have created a user-control that has a fairly complex datagrid which includes localization, custom paging, sorting,...
8
by: Jeff | last post by:
I have a db that has a couple of times closed Access completely when Saving work. So I usually compact and decompile and this seems to fix the problem. But not his time. It has come back again....
3
by: Ignoramus3627 | last post by:
We have a library that has 179385 lines of code. With GCC, no debug information requested (no -g option) it creates a 31 meg long .a library file. I think that it is excessive. Any idea what I...
7
by: Artie | last post by:
The age-old question: does size really matter? I mean code size - get your minds out of the gutter! I have a table to store customer discounts (SQL 2000). The table has up to 10 possible...
5
by: tvnaidu | last post by:
Currently I am doing development using C language, I have all debugging symbols and all enabled, for final product, I don't want all debugging symbols and all, I want very less code size, can I do...
0
by: tvnaidu | last post by:
I need to calculate code size for my application. I created Three shared libs and one main application, each shared lib total size is 180K (ls -l for each file), but when I run size on each one, text...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.