Skip to content

Optimize string literal lookup in mwscript

Here is profile for a script which accesses global variables a lot and therefore does a lot of string literal lookups:

openmw_strlen

The script has 1339 string literals (cell names) and one global variable. To get global variable value mwscript uses string literal too and there is a separate literal for each mentioning. String literals are duplicated. They are stored in a single array separated by \0. To access N-th string interpreter has to skip N-1 string literals. For this it calls strlen for each of them. So basically the time complexity of mwscript execution is O(number of string literals * average string literal length * number of instructions fetching string literals) not counting other factors. In the mentioned script there are 4025 of them so feching a value of a global variable can take up to 4024 strlen calls and that's for each instruction executed.