expr: split expression types and allocate them separately
authorRan Benita <ran234@gmail.com>
Sun, 1 Dec 2013 10:08:47 +0000 (12:08 +0200)
committerRan Benita <ran234@gmail.com>
Sun, 1 Dec 2013 10:58:01 +0000 (12:58 +0200)
commit972395b8569bd80930048c9fc594a5b5fbf74b4a
treecb11fa23326e7abcfeb2f9b79056d839a74faac3
parent068016e4dd9a7463e5a74d0cec7612c2a9bdd6c0
expr: split expression types and allocate them separately

Currently, we have one ExprDef type, which contains a tagged union with
the value of all expression types. Turns out, this union is quite
wasteful memory-wise. Instead, create separate types for all expressions
(e.g ExprBinary, ExprInteger) which embed the common fields
(ExprCommon), and malloc them per their size; ExprDef then becomes a
union of all these types, but is just used as a generic pointer.

[Instead of making ExprDef a union, another option is to use
ExprCommon as the generic pointer type and then do up-castings, like we
do with ParseCommon. But this makes the code much uglier.]

The diff is mostly straightforward mechanical adaptations. It could have
been much smaller with the help of C11 anonymous structs (which were
previously a gnu extension). This will have saved all of the 'op' ->
'expr->op', etc changes. But if we can be a bit more portable for a
little effort, we should.

Before (./test/rulescomp, x86 32 bit, -O2):
==12974==   total heap usage: 145,217 allocs, 145,217 frees, 10,476,238 bytes allocated

After:
==11145==   total heap usage: 145,217 allocs, 145,217 frees, 8,270,358 bytes allocated

Signed-off-by: Ran Benita <ran234@gmail.com>
src/xkbcomp/action.c
src/xkbcomp/ast-build.c
src/xkbcomp/ast.h
src/xkbcomp/compat.c
src/xkbcomp/expr.c
src/xkbcomp/parser.y
src/xkbcomp/symbols.c