Skip to content

allocate StateData and QList<StackSize> in the same memory segment to reduce memory footprint

Jonathan Poelen requested to merge jpoelen/syntax-highlighting:opti_state into master

Previously StateData was dynamically allocated and always contained a list with at least 1 element. Memory looked like:

StateData{
  QAtomicInt ref;
  defId;
  QList<Stack>{ D* d; ptr; size; };
                \-> D{ // variable sized class
                      QAtomicInt ref;
                      flags;
                      capacity;
                      Stack[capacity]; // variable sized
                    };
}

Several comments:

  • The 2 QAtomicInt have the same value since StateData is detached only when the stack changes.
  • capacity is useful information only during the StateData construction (in AbstractHighlighter::highlightLine). After that, the list doesn't change.
  • flags is useless.
  • ptr can be deduced.

By making StateData a variable sized class, the new memory layout looks like this:

StateData{
  QAtomicInt ref;
  stackLen;
  defId;
  Stack[capacity]; // variable sized
};

capacity a temporary information used in the class that manipulates StateData.

With the test.c file repeated 6561 times (636K lines), Kate's memory consumption drops from 361M to 333M (-7.5%).

highlighter_benchmark is slightly faster (1%)

Merge request reports