go icon indicating copy to clipboard operation
go copied to clipboard

Use unlimited list instead slice of array

Open cofyc opened this issue 9 years ago • 2 comments

tv[i] = append(tv[i], t)
t.vec = tv[i]
t.index = len(tv[i]) - 1

以前默认创建的 128 大小的 []_timer 数组,如果超过 128 大小后,append 内部会生成新的 []_timer 数组,导致之前 _timer 引用的 []_timer 中 index 位置不一定为正确的 *timer 。

我这是使用 container.list.List 对象来实现同样功能,不会因为里面对象的变更导致对列表的引用失效。

暂未做很严格以及性能测试,但前面描述的 bug 是很明确的。在我做压力测试因为会一瞬间建立比较多连接,就暴露出来了。

cofyc avatar Jul 21 '15 12:07 cofyc

多谢,应该是这句导致的

tv[i] = append(tv[i], t)

那如果是这样,我觉得可以在append的时候判断是不是capability不够了需要扩容,如果扩容了,可以干的事情是先重新分配一个两倍的slice,然后将原先的东西copy过去,在重新append,这样应该数据就是正确地了。

不过我现在还没测试,不知道是否可行。

siddontang avatar Jul 22 '15 03:07 siddontang

index拿出不正确的timer不是因为append。 是因为https://github.com/siddontang/go/issues/12 有数据竞争, w.tv1[index] = vec[0:0:defaultTimerSize]以后再 tv[i] = append(tv[i], t) 和 for _, t := range vec { 引用的是相同底层数据,但for语句又没有上锁

wllenyj avatar May 29 '18 06:05 wllenyj