From f6a7763b6f3256bed9a7352021745189d0ca8dc9 Mon Sep 17 00:00:00 2001 From: Brian Goff Date: Tue, 13 Mar 2018 11:58:05 -0400 Subject: [PATCH] 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 --- daemon/images/service.go | 2 +- image/store.go | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/daemon/images/service.go b/daemon/images/service.go index 70a8bf4455..4af48959bf 100644 --- a/daemon/images/service.go +++ b/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. diff --git a/image/store.go b/image/store.go index a8f8cee5b2..9fd7d7dcf3 100644 --- a/image/store.go +++ b/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) +}