L1과 L2 같은 캐시는 물리 코어에 종속되어 있어 모든 논리 코어가 공유할 수 없음

→ 이런 특성은 동시성과 거짓 공유 등에 영향을 끼침

type Input struct {
    a int64
    b int64
}

type Result struct {
    sumA int64
    sumB int64
}

// Input 배열을 받아서 Input.a의 합을 Result.sumA에, Input.b의 합을 Result.sumB에 저장하는 기능
func count(inputs []Input) Result {
    wg := sync.WaitGroup{}
    wg.Add(2)

    result := Result{}
    
    // 두 개의 고루틴을 사용해 각각 sumA와 sumB를 계산
    go func() {
        for i := 0; i < len(inputs); i++ {
            result.sumA += inputs[i].a
        }
        wg.Done()
    }()

    go func() {
        for i := 0; i < len(inputs); i++ {
            result.sumB += inputs[i].b
        }
        wg.Done()
    }()

    wg.Wait()
    return result
}

거짓 공유란?

해결 방법

  1. 패딩추가

    type Result struct {
        sumA int64
        _    [56]byte // Padding
        sumB int64
    }=[-
    
  2. 채널을 사용한 구조 변경