TransWikia.com

Breaking up conditionals in order to reduce DRAM usage

Stack Overflow Asked by Saxpy on November 15, 2021

I work in firmware and came across this piece of code (obfuscated key words for privacy reasons). I find this kind of hard to believe, but we are working with a fairly old compiler.

// For optimization purposes, the following "if" statements are broken up as opposed to
// being AND'ed together to ensure the compiler accesses the DRAM as little as possible.
if (searchFooNum == p_entry->fooNum)
{
    Foo_t const fooStart = p_entry->fooOffset;

    // Check if this entry contains any FOOs of interest.
    if (fooStart <= lastFooOffset)
    {
        FooOffset_t const entryLastFoo = fooStart + ConvertBarCountToFooCount(p_entry->barCount) - 1;

        if (p_searchInfo->fooZoneOffset <= entryLastFoo)
        {
            FooOffset_t const fooOffset = (p_searchInfo->fooZoneOffset > entryStartFoo) ? (p_searchInfo->fooZoneOffset - entryStartFoo): 0;
            QuxZoneMapping_t*       p_QuxZoneMapping;

            if ( isFullZoneMapping )
            {
                FooOffset_t const curFooOffset = p_entry->zoneOffset + fooOffset;

                if (p_searchInfo->bazIndexMappingTable == TRUE)
                {
                    p_LbaZoneMapping = GetPointerToBazIndexZoneMapping( p_searchInfo, curFooOffset );
                }
            ...

In other words, how would nesting if statements reduce dram usage? AFAIK functions will allocate a set amount stack for the function to execute?

3 Answers

it was GCC 3.4.5 targeting ARM cortex M4

how would nesting if statements reduce dram usage?

It wouldn't, it's unrelated. Optimizations kicks in and the code doesn't really matter that much - what matters is what the code represents.

functions will allocate a set amount stack for the function to execute?

That of course depends on your compiler. But yer. Because your compiler is smart, it will optimize multiple push-stack-pointer-instructions within one function to a single one on top of the function.

Answered by KamilCuk on November 15, 2021

I guess the comments are misleading. More likely, the optimization is to use local variables, e.g.:

Foo_t const fooStart = p_entry->fooOffset;

if (fooStart <= lastFooOffset)
{
    FooOffset_t const entryLastFoo = fooStart + ConvertBarCountToFooCount(p_entry->barCount) - 1;

    if (p_searchInfo->fooZoneOffset <= entryLastFoo)
    {

instead of:

if (p_entry->fooOffset <= lastFooOffset)
{
    if (p_searchInfo->fooZoneOffset <= p_entry->fooOffset + ConvertBarCountToFooCount(p_entry->barCount) - 1)
    {

which could be AND'ed together to:

if (p_entry->fooOffset <= lastFooOffset
        && p_searchInfo->fooZoneOffset <= p_entry->fooOffset + ConvertBarCountToFooCount(p_entry->barCount) - 1)
{

Using local variables to prevent repeated access to the same memory can indeed speed up the code. Even a good C/C++ compiler usually cannot optimize this automatically due to a C/C++ specific problem called pointer aliasing.

Answered by Codo on November 15, 2021

None of the known by me implementations (compilers) do it this way. All of them calculate the total usage of the stack in the function and allocate it acordingly. I believe it is because otherwise it will make advanced code optimizations difficult or even impossible (when the compiler aggressively optimize the code, the order of execution does not have to be the sane as in your C code - only the observable behaviour has to be the same)

Answered by 0___________ on November 15, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP