dongtun3259 2017-01-28 20:16
浏览 149
已采纳

在Go中使用添加前缀的机制是什么?

Suppose I have a slice slice of type int. While declaring, I set the third argument to size, which I believe reserves memory for at least size ints by setting the cap parameter of the slice.

slice:=make([]int,0,size)

Now, suppose I have an integer variable value. To add it to the slice at the end, I use

slice=append(slice,value)

If the number of elements currently in the slice is less than size, then there will be no need to copy the entire underlying array to a new location in order to add the new element.

Further, if I want to prepend value to slice, as suggested here and here, I use

slice=append([]int{value},slice...)

My question is, what happens in this case? If the number of elements is still less than size, how are the elements stored in the memory? Assuming a contiguous allocation when the make() function was invoked, are all existing elements right shifted to free the first space for value? Or is memory reallocated and all elements copied?

The reason for asking is that I would like my program to be as fast as possible, and would like to know if this is a possible cause for slowing it down. If it is so, is there any alternative way of prepending that would be more time efficient?

  • 写回答

2条回答 默认 最新

  • dongxian0421 2017-01-29 08:01
    关注

    With reslicing and copying

    The builtin append() always appends elements to a slice. You cannot use it (alone) to prepend elements.

    Having said that, if you have a slice capacity bigger than length (has "free" space after its elements) to which you want to prepend an element, you may reslice the original slice, copy all elements to an index one higher to make room for the new element, then set the element to the 0th index. This will require no new allocation. This is how it could look like:

    func prepend(dest []int, value int) []int {
        if cap(dest) > len(dest) {
            dest = dest[:len(dest)+1]
            copy(dest[1:], dest)
            dest[0] = value
            return dest
        }
    
        // No room, new slice need to be allocated:
        // Use some extra space for future:
        res := make([]int, len(dest)+1, len(dest)+5)
        res[0] = value
        copy(res[1:], dest)
        return res
    }
    

    Testing it:

    s := make([]int, 0, 5)
    s = append(s, 1, 2, 3, 4)
    fmt.Println(s)
    s = prepend(s, 9)
    fmt.Println(s)
    s = prepend(s, 8)
    fmt.Println(s)
    

    Output (try it on the Go Playground):

    [1 2 3 4]
    [9 1 2 3 4]
    [8 9 1 2 3 4]
    

    Note: if no room for the new element, since performance does matter now, we didn't just do:

    res := append([]int{value}, dest...)
    

    Because it does more allocations and copying than needed: allocates a slice for the literal ([]int{value}), then append() allocates a new when appending dest to it.

    Instead our solution allocates just one new array (by make(), even reserving some space for future growth), then just set value as the first element and copy dest (the previous elements).

    With linked list

    If you need to prepend many times, a normal slice may not be the right choice. A faster alternative would be to use a linked list, to which prepending an element requires no allocations of slices / arrays and copying, you just create a new node element, and you designate it to be the root by pointing it to the old root (first element).

    The standard library provides a general implementation in the container/list package.

    With manually managing a larger backing array

    Sticking to normal slices and arrays, there is another solution.

    If you're willing to manage a larger backing array (or slice) yourself, you can do so by leaving free space before the slice you use. When prepending, you can create a new slice value from the backing larger array or slice which starts at an index that leaves room for 1 element to be prepended.

    Without completeness, just for demonstration:

    var backing = make([]int, 15) // 15 elements
    var start int
    
    func prepend(dest []int, value int) []int {
        if start == 0 {
            // No more room for new value, must allocate bigger backing array:
            newbacking := make([]int, len(backing)+5)
            start = 5
            copy(newbacking[5:], backing)
            backing = newbacking
        }
    
        start--
        dest = backing[start : start+len(dest)+1]
        dest[0] = value
        return dest
    }
    

    Testing / using it:

    start = 5
    s := backing[start:start] // empty slice, starting at idx=5
    s = append(s, 1, 2, 3, 4)
    fmt.Println(s)
    s = prepend(s, 9)
    fmt.Println(s)
    s = prepend(s, 8)
    fmt.Println(s)
    
    // Prepend more to test reallocation:
    for i := 10; i < 15; i++ {
        s = prepend(s, i)
    }
    fmt.Println(s)
    

    Output (try it on the Go Playground):

    [1 2 3 4]
    [9 1 2 3 4]
    [8 9 1 2 3 4]
    [14 13 12 11 10 8 9 1 2 3 4]
    

    Analysis: this solution makes no allocations and no copying when there is room in the backing slice to prepend the value! All that happens is it creates a new slice from the backing slice that covers the destination +1 space for the value to be prepended, sets it and returns this slice value. You can't really get better than this.

    If there is no room, then it allocates a larger backing slice, copies over the content of the old, and then does the "normal" prepending.

    With tricky slice usage

    Idea: imagine that you always store elements in a slice in backward order.

    Storing your elements in backward order in a slice means a prepand becomes append!

    So to "prepand" an element, you can simply use append(s, value). And that's all.

    Yes, this has its limited uses (e.g. append to a slice stored in reverse order has the same issues and complexity as a "normal" slice and prepand operation), and you lose many conveniences (ability to list elements using for range just to name one), but performance wise nothing beats prepanding a value just by using append().

    Note: iterating over the elements that stores elements in backward order has to use a downward loop, e.g.:

    for i := len(s) - 1; i >= 0; i-- {
        // do something with s[i]
    }
    

    Final note: all these solutions can easily be extended to prepend a slice instead of just a value. Generally the additional space when reslicing is not +1 but +len(values), and not simply setting dst[0] = value but instead a call to copy(dst, values).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 关于用python写支付宝扫码付异步通知收不到的问题
  • ¥50 vue组件中无法正确接收并处理axios请求
  • ¥15 隐藏系统界面pdf的打印、下载按钮
  • ¥15 MATLAB联合adams仿真卡死如何解决(代码模型无问题)
  • ¥15 基于pso参数优化的LightGBM分类模型
  • ¥15 安装Paddleocr时报错无法解决
  • ¥15 python中transformers可以正常下载,但是没有办法使用pipeline
  • ¥50 分布式追踪trace异常问题
  • ¥15 人在外地出差,速帮一点点
  • ¥15 如何使用canvas在图片上进行如下的标注,以下代码不起作用,如何修改