--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/21a5a2b5-2eaf-45d6-a538-5a882fafd0a6n%40googlegroups.com.
The problem of the current algorithm is it is not monotonically increasing:
x1 := make([]int, 897)x2 := make([]int, 1024)
y := make([]int, 100)
println(cap(append(x1, y...))) // 2048
println(cap(append(x2, y...))) // 1280And it is not symmetrical:
x := make([]int, 98)y := make([]int, 666)
println(cap(append(x, y...))) // 768
println(cap(append(y, x...))) // 1360And it depends on the capacity of the first argument,which is some logical but often not for many cases:x := make([]byte, 100, 500)
y := make([]byte, 500)
println(cap(append(x, y...))) // 1024
println(cap(append(x[:len(x):len(x)], y...))) // 640On Saturday, September 4, 2021 at 1:14:30 PM UTC-4 Miraddo wrote:Hey Guys,We know slices grow by doubling until size 1024, the capacity will grow at 25%. The question that I had was why after 1024 elements? Why didn't the developers chose another number like 2048 or other numbers?Thanks,Milad
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/45753fb8-429b-4407-9728-411b75a98484n%40googlegroups.com.
The problem of the current algorithm is it is not monotonically increasing:Please explain why that is a problem. Also, I think you are misusing "monotonically increasing". The behavior up to length 1024 is not "monotonically increasing". It is exponential growth.
x1 := make([]int, 897)x2 := make([]int, 1024)
y := make([]int, 100)
println(cap(append(x1, y...))) // 2048
println(cap(append(x2, y...))) // 1280And it is not symmetrical:Again, please explain why this is a problem.
On Saturday, September 4, 2021 at 11:50:17 PM UTC-4 Kurtis Rader wrote:The problem of the current algorithm is it is not monotonically increasing:Please explain why that is a problem. Also, I think you are misusing "monotonically increasing". The behavior up to length 1024 is not "monotonically increasing". It is exponential growth.I mean the whole length range. Why would you seect [0, 1023]?I never deny it is exponential growth.
x1 := make([]int, 897)x2 := make([]int, 1024)
y := make([]int, 100)
println(cap(append(x1, y...))) // 2048
println(cap(append(x2, y...))) // 1280And it is not symmetrical:Again, please explain why this is a problem.When I merge two slices, I expect the argument orders are not important.
This is what you're describing, right?
https://play.golang.org/p/RJbEkmFsPKM
The code that does this is here
https://github.com/golang/go/blob/9133245be7365c23fcd60e3bb60ebb614970cdab/src/runtime/slice.go#L183-L242
. Note that there are cap adjustments to optimise block sizes and
alignments. This explains why we see what we do.
On Saturday, September 4, 2021 at 11:50:17 PM UTC-4 Kurtis Rader wrote:The problem of the current algorithm is it is not monotonically increasing:Please explain why that is a problem. Also, I think you are misusing "monotonically increasing". The behavior up to length 1024 is not "monotonically increasing". It is exponential growth.I mean the whole length range. Why would you seect [0, 1023]?I never deny it is exponential growth.Okay, I agree that technically your use of "monotonically increasing" does not preclude exponential growth. However, your contrasting the behavior around the pivot point of 1024 implies that exponential growth is excluded since the growth is exponential on each side of the pivot point and you stated it was "monotonically increasing" on one side but not the other.
x1 := make([]int, 897)x2 := make([]int, 1024)
y := make([]int, 100)
println(cap(append(x1, y...))) // 2048
println(cap(append(x2, y...))) // 1280And it is not symmetrical:Again, please explain why this is a problem.When I merge two slices, I expect the argument orders are not important.Why? That is an undocumented implementation detail. Furthermore, the length of "x1" and "x2" at the time when they are appended to, in combination with the value being appended, are reasonable predictors of the capacity of the new slice. It is up to you to prove that a different heuristic performs better.
Why? That is an undocumented implementation detail. Furthermore, the length of "x1" and "x2" at the time when they are appended to, in combination with the value being appended, are reasonable predictors of the capacity of the new slice. It is up to you to prove that a different heuristic performs better.Are there some evidences to prove the current algorithm is better?
On Sat, Sep 4, 2021 at 9:38 PM tapi...@gmail.com <tapi...@gmail.com> wrote:Why? That is an undocumented implementation detail. Furthermore, the length of "x1" and "x2" at the time when they are appended to, in combination with the value being appended, are reasonable predictors of the capacity of the new slice. It is up to you to prove that a different heuristic performs better.Are there some evidences to prove the current algorithm is better?It is your responsibility to show that a different heuristic (not "algorithm"). is better.
Hey Guys,We know slices grow by doubling until size 1024, the capacity will grow at 25%. The question that I had was why after 1024 elements? Why didn't the developers chose another number like 2048 or other numbers?
Thanks,Milad--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/21a5a2b5-2eaf-45d6-a538-5a882fafd0a6n%40googlegroups.com.
On Sunday, September 5, 2021 at 12:52:06 AM UTC-4 Kurtis Rader wrote:On Sat, Sep 4, 2021 at 9:38 PM tapi...@gmail.com <tapi...@gmail.com> wrote:Why? That is an undocumented implementation detail. Furthermore, the length of "x1" and "x2" at the time when they are appended to, in combination with the value being appended, are reasonable predictors of the capacity of the new slice. It is up to you to prove that a different heuristic performs better.Are there some evidences to prove the current algorithm is better?
It is your responsibility to show that a different heuristic (not "algorithm"). is better.I have shown some examples above, which outputs will be more in line with exceptionif the heuristic will only depends on the sum length of the two arguments.
Clarification: I don't think the exponential growth algorithm is bad.I just think it should make the capacities increase monotonically.--Kurtis RaderCaretaker of the exceptional canines Junior and Hank
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/8892db72-1392-4ee3-b336-5c64c6ef7f1cn%40googlegroups.com.
On Sunday, September 5, 2021 at 12:52:06 AM UTC-4 Kurtis Rader wrote:On Sat, Sep 4, 2021 at 9:38 PM tapi...@gmail.com <tapi...@gmail.com> wrote:Why? That is an undocumented implementation detail. Furthermore, the length of "x1" and "x2" at the time when they are appended to, in combination with the value being appended, are reasonable predictors of the capacity of the new slice. It is up to you to prove that a different heuristic performs better.Are there some evidences to prove the current algorithm is better?There are two ways in which the current algorithm may be "better" than the alternative:• If the alternative is to always double, then the current algorithm saves significant memory for large slices, which is better.• The alternative is to use 1.25 starting at a different threshold (you can't *always* use 1.25, as that would fail for n<4). Finding evidence of whether this is better is easy: change the code, run the benchmarks, see how they change.It is your responsibility to show that a different heuristic (not "algorithm"). is better.I have shown some examples above, which outputs will be more in line with exceptionif the heuristic will only depends on the sum length of the two arguments.Okay. Your expectation is wrong. What's the problem with that?I have never cared about how much `append` grows a slice. Not even once. Even if I had cared to make a prediction and that prediction turned out wrong, it would never have mattered. Why do you think it does?
There is a reason for choosing a factor less than 2; released blocks
during reallocation are able to be reused in later allocations (for the
same slice when grown — something that cannot happen for the case of 2
or greater).
Interestingly, Go uses the 1.25× factor while others
appear to use values in the range of 1.5× to less than 2×, with a claim
that phi is the optimum.
There is no discussion in the introduction of append for the choice of
the growth factor: https://codereview.appspot.com/2757042
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/bcf24b8e53f3bc6aafb186ced57b614b4ecf4891.camel%40kortschak.io.
On Sun, 2021-09-05 at 13:09 +0200, 'Axel Wagner' via golang-nuts wrote:
> This sounds interesting, but I don't understand it. Would you be
> willing to expand on this?
It's a consequence of the sum of successive powers of two. To have
reached an allocation of 2^n slots assuming a doubling n times you must
have deallocated and moved 2^n-1 total slots \sum(2^0, 2^1, 2^2, ...,
2^(n-1)). This is less than the amount of space that you are using now,
by one slot, so by definition you will not be able to use any of that
space.
Note that here "slots" is any arbitrary number of contiguous
bytes.
This is pessimistic and only deals with allocations associated with a
single growing array, discounting the possibility of recovering from
unused allocations elsewhere.
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/ba45f293129926415fa8e83c53d680f53bb009c7.camel%40kortschak.io.
In the example given (https://play.golang.org/p/RJbEkmFsPKM), the capacities are "monotonically increasing", as no number in the second column is smaller than the one before it.
On Sunday, September 5, 2021 at 7:02:43 AM UTC-4 kortschak wrote:On Sun, 2021-09-05 at 03:51 -0700, Brian Candler wrote:
> I'm not sure you're clear about what "monotonically increasing"
> means.
>
> Are you saying that there are some cases where append() results in
> the allocated size of a slice *shrinking*? If so, please
> demonstrate.
I think he means that the cap of the appended slice is not a
monotonically increasing function of the cap of the input slice.
https://play.golang.org/p/RJbEkmFsPKM
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/912453d5-2f2f-43b2-b65f-ce27e95752e9n%40googlegroups.com.
Like Brian, I think part of he problem is possibly a miscommunication based on "monotonically increasing". The term means that each point is greater than, or equal to, the previous one. https://en.wikipedia.org/wiki/Monotonic_function. In other words, it never decreases. In the example given (https://play.golang.org/p/RJbEkmFsPKM), the capacities are "monotonically increasing", as no number in the second column is smaller than the one before it.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/fa360f3d-e23c-4c07-9505-9f89bd155bb8n%40googlegroups.com.