473,806 Members | 2,277 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 13394
In <e6************ *************@p osting.google.c om> d8*****@dtek.ch almers.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.progr amming.

--
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.l earn.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
2029
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 more advanced users on how to implement a select form using less lines of code. First the select form class which implements a select widget. I'll post only the relevent parts:
5
4419
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). I've seen mentioned here and there a technique that uses 'void *' and something called 'template specialization' that would reduce the amount of code generated for the
1
1671
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 a factory here. It contains all the expected functions like Employees, Time Clock, Inventory, POs, Invoicing, Job Scheduling and Tracking and much more. No, I am not interested in selling it, so that is not the reason for my post. ;-) ...
0
1110
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, filtering, searching, caching, options to add, edit, and delete record, and then enabling and disabling links inside the datagrid based on different conditions. Everything is working fine now. I have hand-coded everything and the total length of my...
8
2190
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. But my query is this. How come I can keep reducing file size? I decompiled, recompiled and compacted the db 3 times and each time the file size reduced. First time from 13.5 MB to 7.5 MB. Second time down a few
3
5747
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 can do to reduce the size? i
7
1465
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 levels of minimum qty and discount. Then there are 8 possible discount types. I created a proc to look for the best discount available. It has to go though each of the 8 types and 10 levels of discounts per type. The 8 types have different where...
5
1919
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 it?. also is there anyway I can do some performance tuning for C code?. any tools?. Thanks.
0
1227
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 segment shows 40K, for all Three the text segment total sizes are 120K, can I say total code size for all shared libs is 120K rather than total sizes all Three. $ size --format=Berkeley libtgMgr.so.1.0 text data bss dec hex...
0
9719
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10618
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10371
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10110
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9187
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6877
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5546
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4329
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3008
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.