How to reduce Zero Initialised region.

A

Ajai Jose

Hi ,
I work on an ARM processor based embedded system. Code is mainly in
C language. The project has a huge source base. We are supposed to
optimise it. Most datastructures are declared as static and which
directly go into the Zero Initialised region. We need to cut the size
of this ZI region by at least 30%.
The one way i see of doing this is by removing these static arrays
and passing a pointer to the data structure whenever required. but
since these global arrays are used through out the code. A re-write
seems inevitable!

two questions I had.
1. Am I right in doing this(passing pointer instead of making the data
structure static) ?
2. Is there any alternative to this ?


Thanks in Advance,
Ajai.
 
C

Christopher Layne

Ajai said:
two questions I had.
1. Am I right in doing this(passing pointer instead of making the data
structure static) ?

Yes. Better design, IMO.
2. Is there any alternative to this ?

Hard work yields better rewards.
 
J

jacob navia

Ajai Jose a écrit :
Hi ,
I work on an ARM processor based embedded system. Code is mainly in
C language. The project has a huge source base. We are supposed to
optimise it. Most datastructures are declared as static and which
directly go into the Zero Initialised region. We need to cut the size
of this ZI region by at least 30%.
The one way i see of doing this is by removing these static arrays
and passing a pointer to the data structure whenever required. but
since these global arrays are used through out the code. A re-write
seems inevitable!

two questions I had.
1. Am I right in doing this(passing pointer instead of making the data
structure static) ?
2. Is there any alternative to this ?


Thanks in Advance,
Ajai.

Why do you think that this is the problem?

Did you measure the program's performance to see where the
hot spots are? What data leads you to your conclusion that
the static data is the problem?

If you pass pointers around it can be a better design but it will
be slightly slower, since global data needs nothing to be passed, it is
always there.

Before doing anything MEASURE FIRST!

jacob
 
C

Christopher Layne

jacob said:
Why do you think that this is the problem?

Did you measure the program's performance to see where the
hot spots are? What data leads you to your conclusion that
the static data is the problem?

If you pass pointers around it can be a better design but it will
be slightly slower, since global data needs nothing to be passed, it is
always there.

He did mention he was on an embedded system. This could be a space rather than
speed issue.
 
S

Samuel Stearley

1. Am I right in doing this (passing pointer instead of making the data structure static) ?

That will make the executable larger. How much increase can be
tolerated?

2. Is there any alternative to this ?
In my experience people use UINT32s everywhere because they don't want
to look old fashioned. I suggest studying the values assigned to
datums in these structures. Maybe a UINT16 would work equally well.
But don't go around sticking UINT8s in the middle of a data structure.


-Samuel S
 
A

Ajai Jose

He did mention he was on an embedded system. This could be a space rather than
speed issue.

Yes Chrisopher is rgt. The problem we have is real estate we do not
have enough RAM and performance is not an issue.

Thanks,
Ajai.
 
A

Ajai Jose

That will make the executable larger. How much increase can be
tolerated?


I mean passing an extra parameter to a function might increase code
size(text segment). Some additional code for sending the parameter
during function prolog. that is all the penalty right ?
but it will free some space of the ZI region when it comes to big
arrays.
In my experience people use UINT32s everywhere because they don't want
to look old fashioned. I suggest studying the values assigned to
datums in these structures. Maybe a UINT16 would work equally well.
But don't go around sticking UINT8s in the middle of a data structure.

-Samuel S

I understand what you are pointing at but I am particularly looking at
some huge global arrays which eat into the ZI region in a big way.
Small structures I have seen that the original developer has done a
good job.

Thx,
Ajai.
 
S

Samuel Stearley

I mean passing an extra parameter to a function might increase code
size(text segment). Some additional code for sending the parameter
during function prolog. that is all the penalty right ?
but it will free some space of the ZI region when it comes to big
arrays.

Where else would the large array of structures be stored? In the
stack frame of caller so many levels up? How large is you stack?
 
E

Eric Sosman

Ajai said:
Hi ,
I work on an ARM processor based embedded system. Code is mainly in
C language. The project has a huge source base. We are supposed to
optimise it. Most datastructures are declared as static and which
directly go into the Zero Initialised region. We need to cut the size
of this ZI region by at least 30%.
The one way i see of doing this is by removing these static arrays
and passing a pointer to the data structure whenever required. but
since these global arrays are used through out the code. A re-write
seems inevitable!

two questions I had.
1. Am I right in doing this(passing pointer instead of making the data
structure static) ?

Yes. Or maybe No. We don't know enough about the
constraints and circumstances of your situation to be able
to say with certainty.
2. Is there any alternative to this ?

You might consider a sort of intermediate strategy: Make
the pointer(s) global and static, and initialize them to
point to dynamically-zeroed memory when the program starts.
That is, if the program as it stands now has

int BigArray[LARGE]; /* static and zeroed */

you might change it to

int *BigArray; /* only the pointer is static */
...
void initialize(void) {
BigArray = calloc(LARGE, sizeof *BigArray);
if (BigArray == NULL)
die_horribly();
}

.... and arrange to call initialize() before BigArray is needed
elsewhere in the code.
 
C

CBFalconer

Ajai said:
.... snip ...

Yes Chrisopher is rgt. The problem we have is real estate we do
not have enough RAM and performance is not an issue.

rgt? red/green/turquoise? rabid grovelling twerp? rapacious
grown teenager? redundant governmental tax? Please spell out your
insults.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>

"A man who is right every time is not likely to do very much."
-- Francis Crick, co-discover of DNA
"There is nothing more amazing than stupidity in action."
-- Thomas Matthews
 
J

Jack Klein

Hi ,
I work on an ARM processor based embedded system. Code is mainly in
C language. The project has a huge source base. We are supposed to
optimise it. Most datastructures are declared as static and which
directly go into the Zero Initialised region. We need to cut the size
of this ZI region by at least 30%.
The one way i see of doing this is by removing these static arrays
and passing a pointer to the data structure whenever required. but
since these global arrays are used through out the code. A re-write
seems inevitable!

two questions I had.
1. Am I right in doing this(passing pointer instead of making the data
structure static) ?

If you pass a pointer to the data structure, the structure itself
still has to exist somewhere. If the values in the structure need to
persist for some time, in between function calls, they probably need
to have static storage duration anyway.
2. Is there any alternative to this ?


Thanks in Advance,
Ajai.

The issue is not so much whether a function accesses a data structure
by a name with external linkage, but rather how long the structure
must exist.

If you have 100 structures and each of them must retain its data from
the time it is initialized for as long as the program runs, then they
pretty much need to have static storage duration.

On the other hand, if your program needs 50 structures when doing one
type of operation, and a different set of 50 structures when doing
another operation, and in both cases does not need the data anymore
once the operation is finished, you can allocate them automatically in
a function or dynamically, and not have them all taking up memory all
the time.
 
C

Chris Dollin

Thad said:
Why do you want to reduce the size of only zero-initialized RAM?

Because that's what they're running out of. It's an embedded system:
some of them have Funny Specifications. I /suspect/, given that it's
an ARM, that there's some zero-initialised memory that's directly
addressable from a dedicated register and hence is only 4K wide.
(If you don't have a dedicated register you need to load the
address into a spare register before addressing, which means either
loading a pointer you /can/ directly address, which, since we've
eliminated "directly addressed from a dedicated register", means
"directly addressed via the PC", which means you may have multiple
copies of the constant lying around, bad news for embedded; or you
have to assemble the constant in-line, taking at least one and
maybe more instructions, ditto.) But that's just an off-topic guess.
 
T

Thad Smith

Ajai said:
I mean passing an extra parameter to a function might increase code
size(text segment). Some additional code for sending the parameter
during function prolog. that is all the penalty right ?
but it will free some space of the ZI region when it comes to big
arrays.

Why do you want to reduce the size of only zero-initialized RAM?

I assume that code and data are in separate memory spaces. Usually,
though, the zero-initialized static data area, data area allocated to
automatic variables (normally on a stack), and the heap all share the
same physical memory, which is divided according to the program usage
(although there are cases in which some memory is battery-backed, which
other is not).

You can reduce RAM requirements by replacing local static variables with
automatic variables if the value does not need to be carried from one
invocation to another. That allows the memory to be shared among
several functions that execute at different times. There is no need to
pass pointers if there is no data involved.
 
D

David T. Ashley

Ajai Jose said:
Hi ,
I work on an ARM processor based embedded

<SNIP>

Try comp.arch.embedded.

The people on this list are embedded system users, not embedded system
authors.

They will surely give crackpot advice that stresses superior structure at
the expense of speed, FLASH consumption, and RAM consumption. That is
appropriate for larger systems, but not for the embedded system you
described.
 
A

Ajai Jose

Ajai Jose wrote:

... snip ...


rgt? red/green/turquoise? rabid grovelling twerp? rapacious
grown teenager? redundant governmental tax? Please spell out your
insults.
Really sorry for that....(its probably just me while winding up from
work, hasty update ) in any case its not an insult.
rgt => right.

Thx,
Ajai
 
C

CBFalconer

David T. Ashley said:
<SNIP>

Try comp.arch.embedded.

The people on this list are embedded system users, not embedded
system authors.

They will surely give crackpot advice that stresses superior
structure at the expense of speed, FLASH consumption, and RAM
consumption. That is appropriate for larger systems, but not for
the embedded system you described.

This is not a 'list', and embedded programmers also use C. If they
are wise they stick as closely as possible to standard C, and
isolate system dependent things. They are welcome here, but should
not propose or discuss system dependencies.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>

"A man who is right every time is not likely to do very much."
-- Francis Crick, co-discover of DNA
"There is nothing more amazing than stupidity in action."
-- Thomas Matthews
 
M

Malcolm McLean

CBFalconer said:
rgt? red/green/turquoise? rabid grovelling twerp? rapacious
grown teenager? redundant governmental tax? Please spell out your
insults.
Obviously his news reader is running on the embedded system, which is
chronically short of RAM. Leaving out vowels is a reasonable strategy.
 
M

Malcolm McLean

Ajai Jose said:
I mean passing an extra parameter to a function might increase code
size(text segment). Some additional code for sending the parameter
during function prolog. that is all the penalty right ?
but it will free some space of the ZI region when it comes to big
arrays.
Adding extra parameters will almost always increase code size. If the
calling convention is to pass the first few parameters in registers, and the
function only takes one or two parameters, this will simply be a matter of a
few instructions. If parameters are passed on the stack, there will be
rather more overhead.
I understand what you are pointing at but I am particularly looking at
some huge global arrays which eat into the ZI region in a big way.
Small structures I have seen that the original developer has done a
good job.
You can only save memory if you either
Use the same space for two of these big arrays
or
Compress the arrays and expand on demand.

Option one is by far the easiest, but it is only possible if workspace is
not needed simulataneously. .
 
S

Samuel Stearley

Really sorry for that....(its probably just me while winding up from
work, hasty update ) in any case its not an insult.
rgt => right.

Its not that hard to understand that you meant 'right' and were not
intending any insult.
 
C

Christopher Layne

Samuel said:
Its not that hard to understand that you meant 'right' and were not
intending any insult.

It's just more fun for the comp.lang.c pedants to spin their wheels on. Don't
they have anything productive to do?

Serious fear of collapse of everything structured - as if not replying in a
snarky manner to the guy's lack of vowels will result in everyone doing it
tomorrow.

Telling people how to act doesn't do a damn thing - demonstrating usually
yields better results.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,021
Latest member
AkilahJaim

Latest Threads

Top