You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The definition of append is much friendlier to my case:
If the capacity of s is not large enough to fit the additional values, append allocates a new, sufficiently large underlying array that fits both the existing slice elements and the additional values. Otherwise, append re-uses the underlying array.
There are negligible performance implication between the two examples, the main issue I have is with the extra complexity of writing the optimization pass.
Proposal
Change the wording on the spec to require from implementations to return a slice of at least cap capacity when calling make([]T, len, cap), instead of precisely cap capacity.
Potential break
If some code relied on a make of a precise capacity for a following piece of code (likely append) to be forced to copy, like this:
That breaks, previously sCopy[10:] = []int{4, 5, 6} and s[10:] = []int{1, 2, 3}.
After maybe that the second append overwrite the data from the first resulting in sCopy[10:] = []int{1, 2, 3} and s[10:] = []int{1, 2, 3}.
However I have never seen code like that, the patterns I've seen are either by creating a new slice and copying into it.
Or append(append(s[:0:0], s...), 4, 5, 6) which neither break because they take care of copying s away in all cases.
The text was updated successfully, but these errors were encountered:
There are negligible performance implication between the two examples, the main issue I have is with the extra complexity of writing the optimization pass.
Are wrong.
The current wording of spec, allows you to assume that appending to a slice made by makemust copy if the capacity isn't there.
So if the appended slice (s after s = tempSplice in the code I've quoted) overwrite indexes covered by the initial slice, this is also a breaking change and can't be compiled as is.
That has big repercussions on performance mainly now to do this optimization the compiler needs to prove that after appending the lower shared part of the slice isn't overwrote by either one, which can be broken trivially if any one of them escapes for example.
That also has repercussions on compilation performance, it replace what can be a simple dependency analysis with a complex escape analysis and proving that the shared part is safe.
Jorropo
changed the title
proposal: runtime: allows makeslice to return more capacity than asked for
proposal: spec&runtime: allows makeslice to return more capacity than asked for
Apr 3, 2022
Context
I am implementing a
opt slice
optimization pass. And It's made more complex and less effective because the spec definesmake([]any, a, b)
as:My code fuse straight slice operations, so for example I want to turn this code:
Into:
But I cannot because that not up to the spec since calling
cap(s)
indoStuff
would result in13
not10
like asked.So I do this instead:
The definition of
append
is much friendlier to my case:There are negligible performance implication between the two examples, the main issue I have is with the extra complexity of writing the optimization pass.
Proposal
Change the wording on the spec to require from implementations to return a slice of at least
cap
capacity when callingmake([]T, len, cap)
, instead of preciselycap
capacity.Potential break
If some code relied on a
make
of a precise capacity for a following piece of code (likelyappend
) to be forced to copy, like this:That breaks, previously
sCopy[10:] = []int{4, 5, 6}
ands[10:] = []int{1, 2, 3}
.After maybe that the second
append
overwrite the data from the first resulting insCopy[10:] = []int{1, 2, 3}
ands[10:] = []int{1, 2, 3}
.However I have never seen code like that, the patterns I've seen are either by creating a new slice and
copy
ing into it.Or
append(append(s[:0:0], s...), 4, 5, 6)
which neither break because they take care of copyings
away in all cases.The text was updated successfully, but these errors were encountered: