InsideDarkWeb.com

Breaking up conditionals in order to reduce DRAM usage

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?

Stack Overflow Asked by Saxpy on November 15, 2021

3 Answers

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!

Related Questions

handling multiple useState inside conditional ternary

2  Asked on January 27, 2021 by kachi-cheong

     

react-native existing app crash on Android 4.1

0  Asked on January 27, 2021 by s-leg3ndz

     

HTML table widths set proportionally

1  Asked on January 27, 2021 by mayur-arora

   

Alsa issues on Raspberry pi

1  Asked on January 27, 2021 by suraj-hebbar-shankar

 

How to merge dictionaries with the same key and value in Python

1  Asked on January 26, 2021 by arbin-bulaybulay

 

AJAX switching HTML

1  Asked on January 26, 2021 by mimi

       

Using request to return a json value

1  Asked on January 26, 2021 by gta-sprx

 

Convert Array to string in Java/Groovy

5  Asked on January 26, 2021 by maaz

     

List of tuples remove duplicates by first index

3  Asked on January 26, 2021 by rankinstudio

   

Joining based on Sum of amount

1  Asked on January 26, 2021 by rohit-pandey

   

ARKit and Recording / Replaying Image+Depth with time lapse

0  Asked on January 26, 2021 by bsharer-app-share-books

   

Ask a Question

Get help from others!

© 2021 InsideDarkWeb.com. All rights reserved.