Make singleton datatypes more efficient in Go #1394
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.
Fixes #1214. I'm not very serious about this implementation, I just wanted to show what I managed to do with a little hacking.
Currently, a simple "record" inductive like
datatype Astruct = A(x: uint64, y: bool)
gets compiled to a Go struct with an interface that only ever has one implementing struct. This creates an unnecessary indirection. This PR replaces that interface with the single constructor's concrete struct type.The change here is designed to minimize changes to the compiler and share with the case where the datatype has more than one constructor as much as possible. More aggressively for such records we could get rid of the interface type entirely, or also simplify the
String()
andEquals()
implementations. I've benchmarked this change and it does seem to fix the performance problems I had with the normal Dafny output. It's not huge but I do observe a difference, maybe 5% faster for a regular benchmark that shouldn't be bottlenecked on creating structs; an artificial benchmark that creates a lot of structs goes from 83ns to do a functional update to a struct down to 3ns with this PR.