Optimize DafnySequence<Character> to String#819
Closed
olivergillespie wants to merge 2 commits intosmithy-lang:main-1.xfrom
Closed
Optimize DafnySequence<Character> to String#819olivergillespie wants to merge 2 commits intosmithy-lang:main-1.xfrom
olivergillespie wants to merge 2 commits intosmithy-lang:main-1.xfrom
Conversation
texastony
pushed a commit
that referenced
this pull request
Oct 9, 2025
(Re-cutting #819 on [olivergillespie](https://github.com/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. --------- Co-authored-by: Oli Gillespie <ogillesp@amazon.com>
Contributor
Author
|
Merged in #820, thanks! |
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 callingCharacter.toStringon every element, which creates a wholeStringobject and backingbyte[]. TheStreamis wasteful too.Move to a
StringBuilderand for loop. A very rough benchmark shows about 80% reduction in allocations and total time taken.Small string (4 chars):
Larger string (88 chars):
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.