duangao8359 2017-09-21 14:04
浏览 63
已采纳

扩大切片容量的最简单方法是什么?

I have an program that uses a buffer pool to reduce allocations in a few performance-sensitive sections of the code.

Something like this: play link

// some file or any data source
var r io.Reader = bytes.NewReader([]byte{1,2,3})

// initialize slice to max expected capacity
dat := make([]byte, 20)

// read some data into it. Trim to length.
n, err := r.Read(dat)
handle(err)
dat = dat[:n]

// now I want to reuse it: 
for len(dat) < cap(dat) {
        dat = append(dat, 0) 
}

log.Println(len(dat))
// add it to free list for reuse later
// bufferPool.Put(dat)

I always allocate fixed length slices, which is guaranteed larger than the maximum size needed. I need to reduce size to the actual data length to use the buffer, but I also need it to be the maximum size again to read into it the next time I need it.

The only way I know of to expand a slice is with append, so that is what I am using. The loop feels super dirty though. And potentially inefficient. My benchmarks show it isn't horrible, but I feel like there has to be a better way.

I know only a bit about the internal representation of slices, but if I could only somehow override the length value without actually adding data, it would be real nice. I don't really need to zero it out or anything.

Is there a better way to accomplish this?

  • 写回答

2条回答 默认 最新

  • dousi1906 2017-09-21 14:11
    关注

    "Extending" a slice to its capacity is simply a slice expression, and specify the capacity as the high index. The high index does not need to be less than the length. The restriction is:

    For arrays or strings, the indices are in range if 0 <= low <= high <= len(a), otherwise they are out of range. For slices, the upper index bound is the slice capacity cap(a) rather than the length.

    Example:

    b := make([]byte, 10, 20)
    fmt.Println(len(b), cap(b), b)
    
    b = b[:cap(b)]
    fmt.Println(len(b), cap(b), b)
    

    Output (try it on the Go Playground):

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

报告相同问题?

悬赏问题

  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
  • ¥15 前端echarts坐标轴问题
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题