[Dev-luatex] Dump sharing - redundant copying/allocations
lahcim8 at gmail.com
Tue Jul 20 18:01:27 CEST 2021
when NO_DUMP_SHARE is not defined and the system is Little endian, then
for the possibility of sharing format files between architectures all
dumped multi-byte valules are byte swapped by the function "swap_items"
The origin of the function is in Web2C's "texmfmp.c" and it seems that
it was added to LuaTeX in the version 1.08 when the source was converted
from CWeb (or am I reading the history wrong?).
However, there are two differences between Web2C's and LuaTeX's
1) LuaTeX also supports 12 byte values.
and more importantly:
2) LuaTeX essentially does this:
- allocate temporary array
- copy input to temporary array
- [common code with web2c]
- copy temporary array back to input to serve as output
- free temporary array
This all seems redundant and causes many small allocations (~350K
allocations for a ~800K format file), because most allocations are of
only 4 bytes.
Curiously the gcc -O2 optimizer doesn't catch this even though it is a
static function (and changing xmalloc/xfree to the "intrinsic"
malloc/free doesn't help it). Maybe the possible unsigned int overflow
prevents the optimization? Or am I missing some side effect/purpose of
See my proposal below.
(Note that in the LuaTeX repository --disable-dump-share is the default,
while it isn't in TeX Live, I think.)
@@ -1125,13 +1125,9 @@ static gzFile gz_fmtfile = NULL;
-static void swap_items(char *pp, int nitems, int size)
+static void swap_items(char *p, int nitems, int size)
- unsigned total = (unsigned) (nitems * size);
- char *q = xmalloc(total);
- char *p = q;
Since `size' does not change, we can write a while loop for each case,
@@ -1201,8 +1197,6 @@ static void swap_items(char *pp, int nitems, int size)
FATAL1("Can't swap a %d-byte item for (un)dumping", size);
More information about the dev-luatex