wtysos11 / blogWiki

Use to store public paper and organize them.
18 stars 4 forks source link

golang的内存逃逸问题与什么时候应该返回指针 #236

Open wtysos11 opened 3 years ago

wtysos11 commented 3 years ago

我印象中,函数返回值的过程中,对于不是特别大(8KB)或者没有修改必要的数据,都应该直接传值,这样数据会被分配到栈上。这个需要做实验来验证一下。

func XXX((*[]filterElement, *map[int][]int, *map[int][]int, error) {
    redisCtx, _ := context.WithCancel(context.Background())

    filterElements := make([]filterElement, 0, deleteRequest.CurrentNum)
    timeMap := make(map[int][]int, 1<<constdef.FingerprintTimevarBits)
    counterMap := make(map[int][]int, 1<<constdef.FingerprintCounterBits)
        if cursor == 0 {
            return &filterElements, &timeMap, &counterMap, nil
        }
    }
}