[ZPT] Re: re# IMPORTANT NOTE # # Getting the length of a BTree, TreeSet, or output of keys, # values, or items of same is expensive. If you need to get the # length, you need to maintain this separately. # Please make "total" lazy!

Steve Alexander steve@cat-box.net
Fri, 15 Mar 2002 17:31:22 +0000


Chris,

I just noticed your receit enhancement to ZPT.

There is a problem: you are calling len(sequence) for every batch, 
whether the client code wants to know the total or not.

Please consider reworking this so that len(sequence) is only calculated 
when required.

There are certain sequences where finding out their length is very 
expensive.

For example, see Interfaces.py in BTrees:

# IMPORTANT NOTE
#
# Getting the length of a BTree, TreeSet, or output of keys,
# values, or items of same is expensive. If you need to get the
# length, you need to maintain this separately.
#


=== Zope/lib/python/ZTUtils/Batch.py 1.7 => 1.8 ===
          0-based index.  "length" is the actual number of elements in
          the batch.
+
+        "total" is the length of the original, unbatched, sequence
          '''

          start = start + 1
@@ -68,6 +70,7 @@
          self.overlap = overlap
          self.first = max(start - 1, 0)
          self.length = self.end - self.first
+        self.total = len(sequence)
          if self.first == 0:
              self.previous = None



--
Steve Alexander