Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

(null cookie; hope that's ok)


devel / comp.arch.embedded / Are packed structs slower AND safe?

SubjectAuthor
* Are packed structs slower AND safe?pozz
+* Re: Are packed structs slower AND safe?David Brown
|`* Re: Are packed structs slower AND safe?pozz
| `* Re: Are packed structs slower AND safe?David Brown
|  `- Re: Are packed structs slower AND safe?pozz
`- Re: Are packed structs slower AND safe?Brett

1
Are packed structs slower AND safe?

<sl8opi$at$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=697&group=comp.arch.embedded#697

  copy link   Newsgroups: comp.arch.embedded
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: pozzu...@gmail.com (pozz)
Newsgroups: comp.arch.embedded
Subject: Are packed structs slower AND safe?
Date: Tue, 26 Oct 2021 13:28:50 +0200
Organization: A noiseless patient Spider
Lines: 14
Message-ID: <sl8opi$at$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=iso-8859-15; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Tue, 26 Oct 2021 11:28:50 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="4112f4345c46e0088ddfd0b355766984";
logging-data="349"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18iPUhlxYrpKrUTMA39A1ikPmFQqbY0waw="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101
Thunderbird/78.14.0
Cancel-Lock: sha1:T1dMNZrUw7X18m+FGUp91hDCVME=
Content-Language: it
X-Mozilla-News-Host: news://news.eternal-september.org:119
 by: pozz - Tue, 26 Oct 2021 11:28 UTC

In one of my projects that run on a Cortex-M0+ MCU, I have a few arrays
of structs. Now I need to increase the size of the arrays, but I'm out
of RAM, so I'm searching for ways to save some space in RAM.

One simple way is to pack the structs, for example with

__attribute__((packed))

in gcc. I can save some padding bytes (that waste some memory) for each
element of the array, so the total amount of saved space could be enough.

I know the use of a packed struct forces the compiler to generate a
slower code, because of misaligned accesses of its members.
However, besides having a slower code, is the result correct in any case?

Re: Are packed structs slower AND safe?

<sl8qee$cp3$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=698&group=comp.arch.embedded#698

  copy link   Newsgroups: comp.arch.embedded
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: david.br...@hesbynett.no (David Brown)
Newsgroups: comp.arch.embedded
Subject: Re: Are packed structs slower AND safe?
Date: Tue, 26 Oct 2021 13:57:01 +0200
Organization: A noiseless patient Spider
Lines: 72
Message-ID: <sl8qee$cp3$1@dont-email.me>
References: <sl8opi$at$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=iso-8859-15
Content-Transfer-Encoding: 8bit
Injection-Date: Tue, 26 Oct 2021 11:57:02 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="fa9dd796f275214ccb741419f69175be";
logging-data="13091"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/hLHra8oHbCohjJ5lipjdfF7Jn+yttrqw="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101
Thunderbird/78.11.0
Cancel-Lock: sha1:xaJLtWKkHEFAANtvHrLllFiJ4NQ=
In-Reply-To: <sl8opi$at$1@dont-email.me>
Content-Language: en-GB
 by: David Brown - Tue, 26 Oct 2021 11:57 UTC

On 26/10/2021 13:28, pozz wrote:
> In one of my projects that run on a Cortex-M0+ MCU, I have a few arrays
> of structs. Now I need to increase the size of the arrays, but I'm out
> of RAM, so I'm searching for ways to save some space in RAM.
>
> One simple way is to pack the structs, for example with
>
>   __attribute__((packed))
>
> in gcc. I can save some padding bytes (that waste some memory) for each
> element of the array, so the total amount of saved space could be enough.
>
> I know the use of a packed struct forces the compiler to generate a
> slower code, because of misaligned accesses of its members.
> However, besides having a slower code, is the result correct in any case?

Maybe, maybe not - depending on your assumptions, and the details of the
code.

Many processors support unaligned access, meaning the code size will be
the same even if "packed" results in having unaligned accesses. In this
case, code size is the same but execution time may be lower for the
actual access. The Cortex-M4, for example, is fine with unaligned access.

The Cortex-M0+, on the other hand, does not support it. So instead of a
single 32-bit load, you can quickly end up with a mess:

#include <stdint.h>

typedef struct {
uint32_t b;
} su;

typedef struct {
uint32_t b;
} __attribute__((packed)) sp;

int foou(su * p) { return p-> b; }
int foop(sp * p) { return p-> b; }

foou:
ldr r0, [r0]
bx lr
foop:
ldrb r2, [r0, #1]
ldrb r1, [r0]
ldrb r3, [r0, #2]
lsls r2, r2, #8
ldrb r0, [r0, #3]
orrs r1, r2
lsls r3, r3, #16
orrs r3, r1
lsls r0, r0, #24
orrs r0, r3
bx lr

This is big, slow, and may be incorrect if you expected atomic loads and
stores.

I have very rarely seen "packed" used in a way that is actually useful.
Often when people think they need to pack a struct, they can get better
results with more careful re-arrangement of the fields, or perhaps
"manually" packing with bit-fields or other ways of combining fields.
Another alternative is that instead of having an array of structs, you
can split the data up into two or three arrays each containing part of
the data. (This is often done on big systems with cache, as it can lead
to massive speed increases.)

Throwing "packed" onto structs is one of the last places I would look
for some extra ram space, not one of the first.

Re: Are packed structs slower AND safe?

<sl8ve9$gl9$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=699&group=comp.arch.embedded#699

  copy link   Newsgroups: comp.arch.embedded
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: pozzu...@gmail.com (pozz)
Newsgroups: comp.arch.embedded
Subject: Re: Are packed structs slower AND safe?
Date: Tue, 26 Oct 2021 15:22:18 +0200
Organization: A noiseless patient Spider
Lines: 9
Message-ID: <sl8ve9$gl9$1@dont-email.me>
References: <sl8opi$at$1@dont-email.me> <sl8qee$cp3$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=iso-8859-15; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Tue, 26 Oct 2021 13:22:17 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="4112f4345c46e0088ddfd0b355766984";
logging-data="17065"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+ifY8oo0z52gkuB7C5+BQlpKQrEY1rwns="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101
Thunderbird/78.14.0
Cancel-Lock: sha1:PJXb3JDoitqEn+yq8sc3dO1ybuY=
In-Reply-To: <sl8qee$cp3$1@dont-email.me>
Content-Language: it
 by: pozz - Tue, 26 Oct 2021 13:22 UTC

Il 26/10/2021 13:57, David Brown ha scritto:
[...]
> Another alternative is that instead of having an array of structs, you
> can split the data up into two or three arrays each containing part of
> the data. (This is often done on big systems with cache, as it can lead
> to massive speed increases.)

Can you explain better this? Thanks

Re: Are packed structs slower AND safe?

<sl929l$7ps$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=700&group=comp.arch.embedded#700

  copy link   Newsgroups: comp.arch.embedded
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: david.br...@hesbynett.no (David Brown)
Newsgroups: comp.arch.embedded
Subject: Re: Are packed structs slower AND safe?
Date: Tue, 26 Oct 2021 16:11:00 +0200
Organization: A noiseless patient Spider
Lines: 45
Message-ID: <sl929l$7ps$1@dont-email.me>
References: <sl8opi$at$1@dont-email.me> <sl8qee$cp3$1@dont-email.me>
<sl8ve9$gl9$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=iso-8859-15
Content-Transfer-Encoding: 8bit
Injection-Date: Tue, 26 Oct 2021 14:11:01 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="fa9dd796f275214ccb741419f69175be";
logging-data="7996"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19CrTIxYJOFav6oUaLGnctQH8rzs/m9pTo="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101
Thunderbird/78.11.0
Cancel-Lock: sha1:OnTeNWL49ZnWltnpWDC3jQ4Jqlg=
In-Reply-To: <sl8ve9$gl9$1@dont-email.me>
Content-Language: en-GB
 by: David Brown - Tue, 26 Oct 2021 14:11 UTC

On 26/10/2021 15:22, pozz wrote:
> Il 26/10/2021 13:57, David Brown ha scritto:
> [...]
>> Another alternative is that instead of having an array of structs, you
>> can split the data up into two or three arrays each containing part of
>> the data.  (This is often done on big systems with cache, as it can lead
>> to massive speed increases.)
>
> Can you explain better this? Thanks
>

Change:

typedef struct {
uint32_t counter;
bool valid;
} counted_thing;

counted_thing things[1000];

into:

uint32_t thing_counters[1000];
bool thing_valids[1000];

That turns an 8000 byte array into two arrays of 4000 bytes and 1000
bytes respectively.

No padding, inefficient packing, or wasted space. Indeed, with a bit
more effort the thing_valids[] array could perhaps be packed into 125 bits.

This is a big issue in game programming - there has been a move away
from nice C++ objects that are held in an array, to integrating the
array handling with the object handling so that the data can be arranged
in a more cache-friendly manner. It's especially useful when your
objects have critical data that is accessed a lot (such as position) and
less critical data that is more rarely used (such as cost or name) -
separate arrays means you can run through the entire array of object
positions without filling up your caches with low-priority name data.

But in your case, you can use it to save space in ram (and possibly make
stepping through the data a little more efficient as the stride sizes
are more likely to be powers of two).

Re: Are packed structs slower AND safe?

<sl92ua$ahh$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=701&group=comp.arch.embedded#701

  copy link   Newsgroups: comp.arch.embedded
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: pozzu...@gmail.com (pozz)
Newsgroups: comp.arch.embedded
Subject: Re: Are packed structs slower AND safe?
Date: Tue, 26 Oct 2021 16:22:03 +0200
Organization: A noiseless patient Spider
Lines: 48
Message-ID: <sl92ua$ahh$1@dont-email.me>
References: <sl8opi$at$1@dont-email.me> <sl8qee$cp3$1@dont-email.me>
<sl8ve9$gl9$1@dont-email.me> <sl929l$7ps$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=iso-8859-15; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Tue, 26 Oct 2021 14:22:03 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="4112f4345c46e0088ddfd0b355766984";
logging-data="10801"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+rMnCW81k6KRa4jzu2e14Pa8xqz1nUGpg="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101
Thunderbird/78.14.0
Cancel-Lock: sha1:jHWzfup6QxAaDxRnudgCKFtJGk8=
In-Reply-To: <sl929l$7ps$1@dont-email.me>
Content-Language: it
 by: pozz - Tue, 26 Oct 2021 14:22 UTC

Il 26/10/2021 16:11, David Brown ha scritto:
> On 26/10/2021 15:22, pozz wrote:
>> Il 26/10/2021 13:57, David Brown ha scritto:
>> [...]
>>> Another alternative is that instead of having an array of structs, you
>>> can split the data up into two or three arrays each containing part of
>>> the data.  (This is often done on big systems with cache, as it can lead
>>> to massive speed increases.)
>>
>> Can you explain better this? Thanks
>>
>
> Change:
>
> typedef struct {
> uint32_t counter;
> bool valid;
> } counted_thing;
>
> counted_thing things[1000];
>
> into:
>
> uint32_t thing_counters[1000];
> bool thing_valids[1000];
>
> That turns an 8000 byte array into two arrays of 4000 bytes and 1000
> bytes respectively.

Now I got your point.

> No padding, inefficient packing, or wasted space. Indeed, with a bit
> more effort the thing_valids[] array could perhaps be packed into 125 bits.
>
> This is a big issue in game programming - there has been a move away
> from nice C++ objects that are held in an array, to integrating the
> array handling with the object handling so that the data can be arranged
> in a more cache-friendly manner. It's especially useful when your
> objects have critical data that is accessed a lot (such as position) and
> less critical data that is more rarely used (such as cost or name) -
> separate arrays means you can run through the entire array of object
> positions without filling up your caches with low-priority name data.
>
> But in your case, you can use it to save space in ram (and possibly make
> stepping through the data a little more efficient as the stride sizes
> are more likely to be powers of two).

Re: Are packed structs slower AND safe?

<slae6v$km7$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=704&group=comp.arch.embedded#704

  copy link   Newsgroups: comp.arch.embedded
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: ggt...@yahoo.com (Brett)
Newsgroups: comp.arch.embedded
Subject: Re: Are packed structs slower AND safe?
Date: Wed, 27 Oct 2021 02:40:31 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 20
Message-ID: <slae6v$km7$1@dont-email.me>
References: <sl8opi$at$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Wed, 27 Oct 2021 02:40:31 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="505b54dd667b57e892b4fea83dacac0a";
logging-data="21191"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/OgdIOexSo1ddCWMR8gzni"
User-Agent: NewsTap/5.5 (iPad)
Cancel-Lock: sha1:YPqICVVSPyggR/gXyZoA+y1/z7s=
sha1:nituT852tArFnD3k2fpLvsJ5I7E=
 by: Brett - Wed, 27 Oct 2021 02:40 UTC

pozz <pozzugno@gmail.com> wrote:
> In one of my projects that run on a Cortex-M0+ MCU, I have a few arrays
> of structs. Now I need to increase the size of the arrays, but I'm out
> of RAM, so I'm searching for ways to save some space in RAM.
>
> One simple way is to pack the structs, for example with
>
> __attribute__((packed))
>
> in gcc. I can save some padding bytes (that waste some memory) for each
> element of the array, so the total amount of saved space could be enough.
>
> I know the use of a packed struct forces the compiler to generate a
> slower code, because of misaligned accesses of its members.
> However, besides having a slower code, is the result correct in any case?
>

Post the structure with the variable names changed to Greek gods.
Add comments to each line with the range of values stored in each variable.

1
server_pubkey.txt

rocksolid light 0.9.8
clearnet tor