Linear time list operations#10
Conversation
Signed-off-by: Harmen Stoppels <me@harmenstoppels.nl>
Signed-off-by: Harmen Stoppels <me@harmenstoppels.nl>
Signed-off-by: Harmen Stoppels <me@harmenstoppels.nl>
sethrj
left a comment
There was a problem hiding this comment.
Very nice! Good testing and clever scripting.
| setsep "$_dst"; _dst_sep="$sep" | ||
|
|
||
| if [ -z "$_prefix" ]; then | ||
| # Fast concatenation when no prefix is needed | ||
| IFS="$_dst_sep"; _ext_str="$*"; unset IFS | ||
| else | ||
| _ext_str="${_prefix}$1" | ||
| shift | ||
| for elt; do | ||
| _ext_str="${_ext_str}${_dst_sep}${_prefix}${elt}" | ||
| done | ||
| fi |
There was a problem hiding this comment.
Is there a nontrivial performance penalty for merging these and the similar lines at 135 into a single function, e.g. one that uses printf and is called with _ext_str="$(merge "$_dst" "$sep" "$_prefix" "$@")"? Hiding the symbol-filled code (or at least making clear that it's an exact duplicate) woudl be good.
There was a problem hiding this comment.
Yeah, this is a bit dense and repetitive... So far we've tried to avoid printf, or rather subshells "$(printf ...)". I'm not sure if it can be improved without subshells.
kwryankrattiger
left a comment
There was a problem hiding this comment.
Looks good, the expected/result are consistent for all of the tests look correct.
I ran all of the test and the benchmark before and after these changes and found one bug in the old preextend command that was double prepending the PREFIX. The speed up for the benchmark as about 4x which is pretty good.
Avoids string concatenation overhead in extend and preextend
"tricks" used:
set --and"$*"with differentIFSfor fast concatenation, presumably linear complexity.In the included benchmark, runtime goes from 100ms to 21ms on my macbook.