forked from mvdan/sh
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
expand: redesign variables to remove forced allocs
We were fitting string and []string into the Value interface{}, which always meant allocations. This is because neither type was a pointer, and interface values can only fit directly if they are pointers. We also tried using an interface with implementations that were exclusively pointers, but that was clumsy. For one, strings and slices already contain pointers, so it added double indirection. It also required quite a bit more boilerplate code for the user. In the end, a Kind field and three flat fields is the best of both worlds. With no interface and no double pointers, the data is always one pointer away, and setting a value never requires an allocation. This results in a very noticeable drop in allocs/op, particularly for looping operations like Environ.Each. There's a small bump in alloc/op, since Variable is now a bit heavier. We'll recover part of that in a follow-up commit by merging NameRef into Kind, since that will too make nameref variables simpler. Overall, using expand.Variable is a tiny bit more verbose and perhaps less idiomatic, but since it's a fairly low-level and integral part of the expand and interp packages, we can't have it be inefficient. name old time/op new time/op delta Run-8 1.46ms ± 1% 1.45ms ± 0% ~ (p=0.093 n=6+6) name old alloc/op new alloc/op delta Run-8 79.4kB ± 0% 81.2kB ± 0% +2.30% (p=0.002 n=6+6) name old allocs/op new allocs/op delta Run-8 1.03k ± 0% 0.88k ± 0% -14.52% (p=0.008 n=5+5)
- Loading branch information
Showing
7 changed files
with
146 additions
and
131 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.