From 7bd631e8c9addb88fc883e1f151c1df363ec0d45 Mon Sep 17 00:00:00 2001 From: Mauro Amico Date: Thu, 9 Oct 2014 17:27:17 +0200 Subject: [PATCH] [BACKPORT] Avoid potentially expensive `len()` call in value index estimation. backport: https://github.com/zopefoundation/Products.ZCatalog/commit/3770bc998a2656ecdcfc494d5996e6a89fbc2365 Avoid potentially expensive `len()` call in value index estimation. Instead iterate over unique values and break if we can get more than `MAX_DISTINCT_VALUES` values. Also correctly identify empty indexes like the DateRangeIndex, which only returns values when the name argument is provided to the uniqueValues method with either the `since` or `until` field. --- src/Products/ZCatalog/plan.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/Products/ZCatalog/plan.py b/src/Products/ZCatalog/plan.py index a1b187a8..95e0e755 100644 --- a/src/Products/ZCatalog/plan.py +++ b/src/Products/ZCatalog/plan.py @@ -201,7 +201,15 @@ def valueindexes(self): for name, index in indexes.items(): if IUniqueValueIndex.providedBy(index): values = index.uniqueValues() - if values and len(list(values)) < MAX_DISTINCT_VALUES: + i = 0 + for value in values: + # the total number of unique values might be large and + # expensive to load, so we only check if we can get + # more than MAX_DISTINCT_VALUES + if i >= MAX_DISTINCT_VALUES: + break + i += 1 + if i > 0 and i < MAX_DISTINCT_VALUES: # Only consider indexes which actually return a number # greater than zero value_indexes.add(name)