Add `Len()` to image store for info endpoint

In info, we only need the number of images, but `CountImages` was
getting the whole map of images and then grabbing the length from that.
This causes a lot of unnecessary CPU usage and memory allocations, which
increases with O(n) on the number of images.

Signed-off-by: Brian Goff <cpuguy83@gmail.com>
This commit is contained in:
Brian Goff 2018-03-13 11:58:05 -04:00
parent bc7424b443
commit f6a7763b6f
2 changed files with 8 additions and 1 deletions

View File

@ -77,7 +77,7 @@ type ImageService struct {
// CountImages returns the number of images stored by ImageService
// called from info.go
func (i *ImageService) CountImages() int {
return len(i.imageStore.Map())
return i.imageStore.Len()
}
// Children returns the children image.IDs for a parent image.

View File

@ -27,6 +27,7 @@ type Store interface {
Children(id ID) []ID
Map() map[ID]*Image
Heads() map[ID]*Image
Len() int
}
// LayerGetReleaser is a minimal interface for getting and releasing images.
@ -336,3 +337,9 @@ func (is *store) imagesMap(all bool) map[ID]*Image {
}
return images
}
func (is *store) Len() int {
is.RLock()
defer is.RUnlock()
return len(is.images)
}