-
Notifications
You must be signed in to change notification settings - Fork 18k
Proposal: add a "merge" built-in function to join several slices. #23905
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Append already reuses the array if the bytes fit in the destination. |
Duplicate of #18605, I believe. |
@as |
@josharian |
Ah, yes. |
But, with the manner |
There’s no reason I see that it would have to be implemented that way. |
Ok, it would be great if that change can satisfy this need. |
Or we just do this in library code once we have generics (#15292). |
@bradfitz generic is not helpful for this problem. |
So why not a |
@andlabs |
But I think that sometimes we really need a |
You don't need a language change for that. You just need a compiler optimization that infers regions of slices that are unconditionally overwritten, which should be fairly straightforward for the simple case of appending a sequence of slices. |
It would be great that a compiler optimization can achieve the goal of a merge function. |
This can be done either via #18605, or via generics, or via a compiler optimization. We're not going to accept this as a builtin function directly. |
ok, I hope there is a tangible solution in planning to resolve this inefficiency problem, |
I do have to wonder, and I forget if anyone said this at all in this thread, if the cost of zero-initializing is really significant enough to optimize it away. (This means actual experiments and benchmarks to confirm or deny this.) |
The current compiler spends about 15% of its execution time zero initializing new allocations. I doubt that much of that can be optimized away, but the answer is yes: It matters. |
The problem
Sometime, we need to insert a slice (call it x) into another (call it y) at the index i of y.
If the capacity of y is large enough to accept all elements of x, then things would simple.
However, the capacity of y is not large enough to accept all elements of x,
we must use make to allocate a new slice which is large enough to accept all elements of x and y.
The problem here is that the make function will clear all allocated bytes,
which is not essential for this case.
The proposal
So I propose a merge (or join, or concat) built-in function to merge several slices.
so that we can call
which will be more efficient than the insert2 function.
The text was updated successfully, but these errors were encountered: