From 1c28d326b1f80059b03c19c4f4c5c6a2abaab629 Mon Sep 17 00:00:00 2001 From: Jon Harper Date: Thu, 10 Jan 2019 21:50:50 +0100 Subject: [PATCH] heaps: add comments referring to pypy's heapq.py --- basis/heaps/heaps.factor | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/basis/heaps/heaps.factor b/basis/heaps/heaps.factor index 192b50e94a..75406d7660 100644 --- a/basis/heaps/heaps.factor +++ b/basis/heaps/heaps.factor @@ -6,6 +6,9 @@ kernel.private locals math math.order math.private sequences sequences.private summary vectors ; IN: heaps +! names and optimizations copied from pypy's heapq.py, +! refer to it from in depth explanations of the optimizations. + GENERIC: heap-push* ( value key heap -- entry ) GENERIC: heap-peek ( heap -- value key ) GENERIC: heap-pop* ( heap -- ) @@ -86,6 +89,7 @@ M: heap heap-peek ( heap -- value key ) > :> data to data data-nth :> tmp @@ -115,6 +119,11 @@ M: heap heap-push* We *could* break out of the loop as soon as we find a pos where newitem <= +! > both its children, but turns out that's not a good idea [...] +! Indeed the code is 33% slower if we remove this optmization. :: sift-up ( heap n -- ) heap data>> :> data data length :> end