Browse Source

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>
Brian Goff 7 years ago
parent
commit
f6a7763b6f
2 changed files with 8 additions and 1 deletions
  1. 1 1
      daemon/images/service.go
  2. 7 0
      image/store.go

+ 1 - 1
daemon/images/service.go

@@ -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.

+ 7 - 0
image/store.go

@@ -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)
+}