Re: Memory management strategy



In article <23am91lnhhu5afenncqo7lq166vma7tn0l@xxxxxxx>,
Paul Mesken <usurper@xxxxxxxxxx> wrote:
>On 30 May 2005 07:09:41 -0700, ira2402@xxxxxxxxx wrote:

>>We are developing sw for a small embedded OS and we have limited
>>memory.
>>We are looking for algorithms, links, and articles about this.

>Well, as far as C goes, I could think of a couple of tips :

Generally good recommendations, but a few of them do not always work.

>- Don't use speed optimizations of your compiler. These might involve
>trading memory for speed (loop unrolling, for example).

Inlining of short routines can -sometimes- take less memory than
calling the routine, as the registers can sometimes be used directly
"as-is", without having to save important values and generate the
call and have the return stack.

Some compilers allow control over when unrolling is done -- e.g.,
may allow you to place an upper limit on the complexity of the code
to be unrolled.

Thus I would not say "don't use" the optimizations: I would say
"investigate the available options and their effects" -- and be
prepared for the possibility that the next compiler update down the
road, the tradeoffs might change.


>- Use the minimum sized datatype you need. All too often, int's are
>used automatically by the programmer where short's and char's could
>have been used. You might even be able to pack some values with
>bitfields.

That can depend upon how often the data is used and how much
of one type there is, and upon the architecture. Some systems need
longer instructions to access smaller data than they were optimized for.
Particularily for bitfields.


>- Avoid recursive functions, if you can. Although recursive functions
>often reduce code size, they might turn out to be very memory hungry.

Rewriting a recursive function into its iterative form (which always
possible) can require more memory and lower efficiency than leaving
it recursive -- in the iterative form, you have to do dynamic memory
management, linked lists or growing arrays in place, with all the code
and memory overhead that represents.


>- Using global variables might reduce memory usage (in passing less
>parameters) in some cases but this is considered a bit ugly and is a
>bit of a micro optimization :-)

Using global variables can also increase code size, depending on the
architecture. Sometimes there are shorter instructions for referring
to objects "near" a base register (e.g., within 16 bits offset)
whereas a full address may be required for a global. On the other
hand, some architectures have short instructions for referring to
objects in "low memory" (e.g., first 64 Kb), and on those architectures
a global that can be located within that "low memory" block might have
the smallest instruction. You really need to know your architecture
to get this kind of thing right.
--
Oh, to be a Blobel!
.



Relevant Pages

  • Re: Superstitious learning in Computer Architecture
    ... Without a LOT of logic or some other better approach, re-executing the instructions requires re-decoding and it ties up the cache memory bus transferring more data as instructions than the instructions are working on. ... There is most of an order of magnitude in speed sacrificed by even HAVING a cache in a single ALU system, and more than an order of magnitude in multiple-ALU systems! ...
    (comp.arch.arithmetic)
  • Re: Iyonix instruction timings and RAM speed results
    ... I get 166/127 for main RAM and 5.7/62 for PCI Video memory on my original ... unrolled LDM/STM instructions for these, ... "add floating point" instruction takes almost 200 clock cycles, ... MOV R0,R0,LSL #1 ...
    (comp.sys.acorn.misc)
  • PART 3. Why it seems difficult to make an OOO VAX competitive (really long)
    ... implementations, compared to high-performance RISCs, but also to IA-32. ... "The VAX is so tied to microcode we predict it will be impossible to ... most frequently-used instructions can be converted to a small number ... given the increasing relative latency to memory. ...
    (comp.arch)
  • Re: Cost of calling a standard library function
    ... > sense, since push Allocates memory, and pop deallocates it. ... Hence, all the CPU does is, basically: ... so forth...it's even possible to get "free" instructions (effectively ... what else is an ASM coder's job? ...
    (alt.lang.asm)
  • Re: [SLE] For or against ..Hyperthreading.
    ... > Let's say for mail or Database server... ... Opteron and Athlon-64 also have a different memory access archetecture so ... - 64-bit Address registers means more memory is directly addressable, ... do a wider range of operations as primitive instructions (for example, fetch, ...
    (SuSE)