/* The following codes are compiled into library(test.a) */
typedef struct {
short x[5];
} weight_t;
typedef struct {
weight_t wgt;
char *ptr;
} tbl_t;
/* huge static array and 70% of entries are empty/zero */
static tbl_t tbl[] ={
{ { 0x0102, 0x0102, 0x0102, 0x0102, 0x0102, }, 0 }, /* 0000 */
{ { 0x0103, 0x0103, 0x0103, 0x0103, 0x0103, }, 0 }, /* 0001 */
{ { 0x0104, 0x0104, 0x0104, 0x0104, 0x0104, }, 0 }, /* 0002 */
{ { 0x0105, 0x0105, 0x0105, 0x0105, 0x0105, }, 0 }, /* 0003 */
{ { 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, }, 0 }, /* 3134c */
{ { 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, }, 0 }, /* 3134d */
......
......
.....
}; /* more than one million entries */
Since array1 contains more than one million entries(even though 70% of them are empty/zero entries), the size of library file(test.a) is quite big.
I know using hash table to only include non-empty entries can reduce the size of array1 but is there any way to compress or do size optimization for library file(test.a) without changing array1
?
There are tons of places in other libraries accessing the value of x
in struct weight_t
, there could be huge effort involved to change current static array tbl
implementation.
/* tbl_t obj ==> tbl_t_obj */
val = tbl_t_obj.wgt.x[1];
To reduce the size of the executable, follow @user3386109 idea: Load the table via an external file. Although the executable file is smaller, the total size of executable plus external file is not reduced.
Code could compress that external file and then uncompressed it as part of the
tbl[]
assignment early in the run of the executable. Depending on compression time, that may make the external file 10x smaller.