udem-dlteam / pnut

🥜 A Self-Compiling C Transpiler Targeting Human-Readable POSIX Shell
https://pnut.sh
BSD 2-Clause "Simplified" License
424 stars 14 forks source link

Faster text primitives #77

Closed laurenthuberdeau closed 2 months ago

laurenthuberdeau commented 2 months ago

Context

We added casts some time ago which allows us to change text_pool from int[] to void*[]. This makes it possible to store pointers in the text_pool, including pointers to other strings. Previously, to concatenate a string to a text, wrap_str needed to copy the whole string in the text_pool, taking time and more importantly memory.

Since almost all calls to wrap_str were with string literals which are constant, this copy operation is not necessary. This PR changes the type of text_pool to void*[] and adds wrap_str_const a function that can be used for constant strings (either literals or strings from the string pool which we know are constants).

Being avoiding many copies, about the peak text allocation is 25% lower and the cumulative text allocation is 30% lower.

This PR:

  string_pool_alloc=44171 heap_alloc=106896 max_text_alloc=14240 cumul_text_alloc=199883

Main:

  string_pool_alloc=44159 heap_alloc=106847 max_text_alloc=18263 cumul_text_alloc=280550

For bootstrapping pnut.sh, this PR reduces the execution time by 1.5s for ksh, 3s for dash, 4s for bash. Other shells haven't been benchmarked but are likely also faster.