Reduce memory writes and short-lived allocations. #17
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
I noticed that hot parts of the program (allocation and evaluation) were doing more work than is strictly necessary, so I've removed some of it.
Removed the memset from tcl_append_string, since all but one byte of it is immediately overwritten by strncpy. Just zeroed the terminating byte instead.
Loosened the return type of tcl_string to char* (not const), since many uses of it mutate the returned buffer.
Replaced the initializing copy in tcl_append_string with memcpy, which is slightly faster than strncpy in cases like this where the length is known in advance. (This is about a 5% improvement in my tests.)
Altered tcl_append to not free the right-hand argument, after noticing that almost every use of it created an otherwise-unused copy.
In TCMD handling, rely on tcl_list_at to detect an empty command by returning NULL. This avoids a second tokenization pass over the string in tcl_list_length.
Avoid making a short-lived copy of the value in tcl_var, just take ownership of the pointer instead.
On a small collection of microbenchmarks, this improved performance by about 25-30% on my machine.