benhoyt / goawk

A POSIX-compliant AWK interpreter written in Go, with CSV support
https://benhoyt.com/writings/goawk/
MIT License
1.95k stars 84 forks source link

Optimize constant integer array indexes to avoid toString() at runtime #169

Closed benhoyt closed 1 year ago

benhoyt commented 1 year ago

When @xonixx was adding code coverage, I asked if he could make the coverage AST output COVER["1"] instead of COVER[1] to give a slight optimization. However, it's better to do that as an optimization that all users can benefit from instead.

The speedup for the ArrayOperations benchmark is about 11%, so I think it's worth it:

ArrayOperations-16 312ns ± 3% 276ns ± 3% -11.54% (p=0.008 n=5+5)

This commit adds the compiler optimization, but also makes the cover.go output __COVER[1] now that these expressions are optimized.

Fixes #157