Skip to content

Linear time list operations#10

Merged
haampie merged 3 commits into
mainfrom
hs/fix/speed
May 19, 2026
Merged

Linear time list operations#10
haampie merged 3 commits into
mainfrom
hs/fix/speed

Conversation

@haampie
Copy link
Copy Markdown
Member

@haampie haampie commented May 14, 2026

Avoids string concatenation overhead in extend and preextend

"tricks" used:

  • Build a local temporary string instead of extending a big string one item at a time (quadratic complexity)
  • When prefix not set: use set -- and "$*" with different IFS for fast concatenation, presumably linear complexity.
  • When prefix is set: shift the first element, followed by a tight loop (still quadratic complexity).
  • Eliminate the array reversal in preextend

In the included benchmark, runtime goes from 100ms to 21ms on my macbook.

haampie added 2 commits May 15, 2026 11:11
Signed-off-by: Harmen Stoppels <me@harmenstoppels.nl>
Signed-off-by: Harmen Stoppels <me@harmenstoppels.nl>
Signed-off-by: Harmen Stoppels <me@harmenstoppels.nl>
Copy link
Copy Markdown

@sethrj sethrj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nice! Good testing and clever scripting.

Comment thread cc.sh
Comment thread cc.sh
Copy link
Copy Markdown
Collaborator

@kwryankrattiger kwryankrattiger left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@haampie
Copy link
Copy Markdown
Member Author

haampie commented May 19, 2026

You're right. I think preextend was over-engineered, cause the bug case is never hit (no prefix is every passed).

@haampie haampie merged commit 25b8a3c into main May 19, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants