Repository: kubernetes/kubernetes
File: staging/src/k8s.io/apiserver/pkg/storage/cacher/delegator.go (Go)

Code before:

	cacheDigest, cacheResourceVersion, err := c.calculateStoreDigest(ctx, c.cacher, "0", 0, func(obj metav1.Object) {
		// collect items from cacher's list
		trackingList = append(trackingList, namespaceNameRV{Namespace: obj.GetNamespace(), Name: obj.GetName(), RV: obj.GetResourceVersion()})
	})
	if err != nil {
		return nil, fmt.Errorf("failed calculating cache digest: %w", err)
	}
	etcdDigest, etcdResourceVersion, err := c.calculateStoreDigest(ctx, c.etcd, cacheResourceVersion, storageWatchListPageSize, func(obj metav1.Object) {
		// compare the item in etcd's list against trackingList which is cacher's list
		if foundDiff {
			return
		}
		dd.index += 1
		item := namespaceNameRV{Namespace: obj.GetNamespace(), Name: obj.GetName(), RV: obj.GetResourceVersion()}
		if len(trackingList) == 0 {
			foundDiff = true
			dd.etcdItem = &item
		} else {
			if trackingList[0] != item {
				foundDiff = true
				dd.cacheItem, dd.etcdItem = &trackingList[0], &item
			}
			// This is cheap since it slices existing slice with no copying.
			trackingList = trackingList[1:]
		}
	})
	if err != nil {
		return nil, fmt.Errorf("failed calculating etcd digest: %w", err)
	}
	if len(trackingList) > 0 {
		dd.index += 1
		dd.cacheItem = &trackingList[0]
	}
	if cacheResourceVersion != etcdResourceVersion {
		return nil, fmt.Errorf("etcd returned different resource version then expected, cache: %q, etcd: %q", cacheResourceVersion, etcdResourceVersion)
	}
	sd := &storageDigest{
		ResourceVersion: cacheResourceVersion,
		CacheDigest:     cacheDigest,
		EtcdDigest:      etcdDigest,
	}
	if dd.cacheItem != nil || dd.etcdItem != nil {
		sd.diffDetail = dd
	}
	return sd, nil
}

type namespaceNameRV struct {
	Namespace string
	Name      string

Review comment (performance):
if we track the etcd item index specifically, we can make identified mismatches set the foundDiff item directly like this:

```go
		etcdItem := namespaceNameRV{Namespace: obj.GetNamespace(), Name: obj.GetName(), RV: obj.GetResourceVersion()}
		if len(cacheItems) <= etcdIndex {
			foundDiff = &diffDetail{index: etcdIndex, etcdItem: &etcdItem}
			cacheItems = nil // don't need this any more
			return
		}
		if cacheItem := cacheItems[etcdIndex]; cacheItem != etcdItem {
			foundDiff = &diffDetail{index: etcdIndex, etcdItem: &etcdItem, cacheItem: &cacheItem}
			cacheItems = nil // don't need this any more
			return
		}
		etcdIndex += 1
```

avoid taking the address of trackingList[...], since that retains the entire slice in memory even after this function returns

Code after:

	cacheDigest, cacheResourceVersion, err := c.calculateStoreDigest(ctx, c.cacher, "0", 0, func(obj metav1.Object) {
		// collect items from cacher's list
		cacheItems = append(cacheItems, namespaceNameRV{Namespace: obj.GetNamespace(), Name: obj.GetName(), RV: obj.GetResourceVersion()})
	})
	if err != nil {
		return nil, fmt.Errorf("failed calculating cache digest: %w", err)
	}
	etcdDigest, etcdResourceVersion, err := c.calculateStoreDigest(ctx, c.etcd, cacheResourceVersion, storageWatchListPageSize, func(obj metav1.Object) {
		// compare the item in etcd's list against cacheItems which is cacher's list
		if foundDiff != nil {
			return
		}
		etcdItem := namespaceNameRV{Namespace: obj.GetNamespace(), Name: obj.GetName(), RV: obj.GetResourceVersion()}
		if len(cacheItems) <= etcdIndex {
			foundDiff = &diffDetail{Index: etcdIndex, EtcdItem: &etcdItem}
			cacheItems = nil // don't need it any more and nil it to allow GC to collect it
			return
		}
		if cacheItem := cacheItems[etcdIndex]; cacheItem != etcdItem {
			foundDiff = &diffDetail{Index: etcdIndex, EtcdItem: &etcdItem, CacheItem: &cacheItem}
			cacheItems = nil // don't need it any more and nil it to allow GC to collect it
			return
		}
		etcdIndex += 1
	})
	if err != nil {
		return nil, fmt.Errorf("failed calculating etcd digest: %w", err)
	}
	if len(cacheItems) > etcdIndex {
		cacheItem := cacheItems[etcdIndex]
		foundDiff = &diffDetail{Index: etcdIndex, CacheItem: &cacheItem}
	}
	if cacheResourceVersion != etcdResourceVersion {
		return nil, fmt.Errorf("etcd returned different resource version then expected, cache: %q, etcd: %q", cacheResourceVersion, etcdResourceVersion)
	}
	return &storageDigest{
		ResourceVersion: cacheResourceVersion,
		CacheDigest:     cacheDigest,
		EtcdDigest:      etcdDigest,
		DiffDetail:      foundDiff,
	}, nil
}

type namespaceNameRV struct {
	Namespace string
	Name      string
	RV        string
}

type diffDetail struct {