Paste: patch

Author: malumalu
Mode: patch
Date: Fri, 22 May 2009 21:35:34
Plain Text |
From 21d2ebf7f57839b74361b156e19aca72a3e3ea07 Mon Sep 17 00:00:00 2001
From: Maximilian Lupke <simply.malu@googlemail.com>
Date: Fri, 22 May 2009 23:29:32 +0200
Subject: [PATCH] Optimize memoized words without inputs.

Memoized words without inputs are now treated like constants.
---
 basis/memoize/memoize.factor |   17 +++++++++++++++--
 1 files changed, 15 insertions(+), 2 deletions(-)

diff --git a/basis/memoize/memoize.factor b/basis/memoize/memoize.factor
index 74ca07c..32bcd8d 100644
--- a/basis/memoize/memoize.factor
+++ b/basis/memoize/memoize.factor
@@ -32,14 +32,27 @@ M: too-many-arguments summary
     [ unpack/pack '[ _ _ cache ] ] keep
     pack/unpack ;
 
-PRIVATE>
+: could-be-constant? ( effect -- ? )
+    in>> length 0 = ; inline
 
-: define-memoized ( word quot effect -- )
+: (define-memoized) ( word quot effect -- )
     [ drop "memo-quot" set-word-prop ]
     [ 2drop H{ } clone "memoize" set-word-prop ]
     [ [ [ dup "memoize" word-prop ] 2dip make-memoizer ] keep define-declared ]
     3tri ;
 
+: (define-constant) ( word quot effect -- )
+    [ drop "constant" set-word-prop ]
+    [ define-inline ] 3bi ;
+
+PRIVATE>
+
+: define-memoized ( word quot effect -- )
+    dup could-be-constant?
+    [ (define-constant) ]
+    [ (define-memoized) ]
+    if ;
+
 SYNTAX: MEMO: (:) define-memoized ;
 
 PREDICATE: memoized < word "memoize" word-prop ;
-- 
1.5.6.3

New Annotation

Summary:
Author:
Mode:
Body: