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

Code before:
	}
	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
	RV        string
}

// diffDetail contains the detailed info about consistency checker diff.
type diffDetail struct {
	index     int
	cacheItem *namespaceNameRV

Review comment (performance):
```suggestion
	if len(cacheItems) > etcdIndex {
		cacheItem := cacheItems[etcdIndex]
		foundDiff = &diffDetail{index: etcdIndex, cacheItem: &cacheItem}
	}
```

Code after:
	}
	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 {
	Index     int
	CacheItem *namespaceNameRV
	EtcdItem  *namespaceNameRV
}

type storageDigest struct {
	ResourceVersion string