lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


There are situations where the processor can work with packed data but
you need to use prebuilt foreign libs. In my case the prebuilt libs are
compiled unpacked and hence i have to compile my code unpacked too.
The Cortex-µP i am using can only run images completely packed or
completely unpacked.

Am 11.10.2021 um 18:39 schrieb Roberto Ierusalimschy:
Hugo Gualandi came with the idea of using a packed structure to store
Lua values. Intel CPUs (and it seems ARMs too) can work with unaligned
data (or aligned with weaker boundaries) and, at least for some
architectures, with very small (or even none) performance penalties.

As a very fast check, I simply changed the following line in lobject.h:

-typedef struct TValue {
+typedef struct __attribute__((packed)) TValue {
    TValuefields;
  } TValue;

This is valid in gcc and clang.  (It gives one warning in ltable.c which
for now I am ignoring. It is a trivial change to correct that: pass the
second parameter of 'mainposition' by value instead of by reference.)

I quickly tested that in two Intel i7. As expected, memory use
by arrays is cut by almost half (9/16). Maybe unexpected, I did
not see any relevant performance penalties at all. (In a few
benchmarks, performance even improved, probably because there
is less memory trafic.)

It would be good to know how this change works in other architectures.

-- Roberto