[ZODB-Dev] Re: SVN: ZODB/branches/anguenot-ordering-beforecommitsubscribers/src/transaction/_transaction.py Implement the sort and insert using bisect.insort()

Florent Guillaume fg at nuxeo.com
Tue Aug 9 11:53:05 EDT 2005


>      def beforeCommitHookOrdered(self, hook, order, *args, **kws):
>          if not isinstance(order, IntType):
>              raise ValueError("An integer value is required "
>                               "for the order argument")
> -        index = 0
> -        for o, h, a, k in self._before_commit:
> -            if order < o:
> -                break
> -            index += 1
> -        self._before_commit.insert(index, (order, hook, args, kws))
> +        # `index` goes up by 1 on each append.  Then no two tuples can
> +        # compare equal, and indeed no more than the `order` and
> +        # `index` fields ever get compared when the tuples are compared
> +        # (because no two `index` fields are equal).
> +        index = len([x[1] for x in self._before_commit if x[1] == order])
> +        bisect.insort(self._before_commit, (order, index, hook, args, kws))

Frankly I don't see the point of using bisect if you do a linear pass on the
list first. The original code was clearer and was faster.

If you had a "natural" way of getting the index, then why not. But anyway
this kind of hook list will have only a few elements in it. Optimize later.

Florent

-- 
Florent Guillaume, Nuxeo (Paris, France)   CTO, Director of R&D
+33 1 40 33 71 59   http://nuxeo.com   fg at nuxeo.com



More information about the ZODB-Dev mailing list