Proposal: improve performance of sequences by making slices lazy #2313
Labels
area: performance
Performance issues
lang: c#
Dafny's C# transpiler and its runtime
part: language definition
Relating to the Dafny language definition itself
status: needs-decision
The cause of this issue is clear; the team needs to decide whether we want to work on it
Currently, the following function has quadratic complexity when transpiled to C#:
This is surprising to users. It happens because
s[1..]
creates a copy of the subsequence1 ..
ofs
.Workarounds exist (iterating using an index, keeping this as the spec by using a
by method
, etc.), but they all boil down to "don't use slices" in executable code, which is too bad.I propose to change the runtime so that:
s[x..y]
creates a lazy "view" into the original sequence (not a copy).s[..]
creates a copy, allowing the underlying memory to be freed.I argue that this is not a breaking change, since the runtime performance of sequence is not specified. However, it could cause an increase in memory usage for programs that create large sequences and then keep references (through slices) to only small portions of that data.
This solution is in line with what Go does with slices. It is fast and convenient, but it has one downside: a small slice can keep the whole sequence alive in memory. This is a well-know caveat in go. In fact, the manual says:
The text was updated successfully, but these errors were encountered: