Skip to content

Optimize DafnySequence<Character> to String#820

Merged
texastony merged 2 commits intomain-1.xfrom
reduce-to-string-allocs
Oct 9, 2025
Merged

Optimize DafnySequence<Character> to String#820
texastony merged 2 commits intomain-1.xfrom
reduce-to-string-allocs

Conversation

@robin-aws
Copy link
Contributor

(Re-cutting #819 on olivergillespie's behalf)

Optimize performance of ToNative$Simple.String. I saw in an allocation profile of my application that this code is creating a lot of garbage, principally from calling Character.toString on every element, which creates a whole String object and backing byte[]. The Stream is wasteful too.

Move to a StringBuilder and for loop. A very rough benchmark shows about 80% reduction in allocations and total time taken.

Small string (4 chars):

Time Allocation
Baseline 159 ns/op 608 bytes/op
Optimized 37 ns/op 96 bytes/op

Larger string (88 chars):

Time Allocation
Baseline 2079 ns/op 5360 bytes/op
Optimized 419 ns/op 816 bytes/op

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

@robin-aws robin-aws requested a review from a team as a code owner October 9, 2025 20:35
@texastony texastony merged commit 19f6319 into main-1.x Oct 9, 2025
121 of 172 checks 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.

4 participants