Let’s say “tail” and “block” are byte slices. The code below appends bytes from “block”, adds it to “tail” and then copies over that slice into “block”.
block = append(tail, block...)
Before this line of code, it is possible that “tail” has enough capacity to hold all bytes from “block”. In this case, no new memory will get allocated and after this line of code, both “tail” and “block” will point to the same underlying array.
If “tail” does not have enough capacity to hold all bytes from “block”, new memory will get allocated and “block” will point to that. In this case, both “tail” and “block” will point to different underlying array.
The same function can be called again and we might append bytes to “tail”. We have to nil out “tail” to make sure we do not overwrite “block” above.
block = append(tail, block...)
tail = nil
If “tail” and “block” were pointing to same locations, the underlying array is still referenced by “block” and would not get garbage collected.
If “tail” and “block” were pointing to different locations, niling out “tail” means the underlying array would get garbage collected. The next time this function gets called and we append bytes to “tail”, we would allocate new memory location. We should be able to optimize this.
block = append(tail, block...)
if &tail[0] == &block[0] {
// same location
tail = nil
} else {
// different location
tail = tail[:0]
}
The above code makes sure memory for the underlying array for “tail” is not freed unnecessarily. The “if” check above has to be accurate, hence this question.
In the code snippet you have byte slices “a” and “b” share the same underlying array, however “b” is using a subset to array for “a”. This is not what I am looking for, however I also would like to know if we can programatically find out if “a” and “b” share the same underlying array. This is for some other use case.