public inbox for kawa@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] further optimization for call-with-values
@ 2015-03-25 23:13 Jamison Hope
  2015-03-27  4:41 ` Per Bothner
  0 siblings, 1 reply; 3+ messages in thread
From: Jamison Hope @ 2015-03-25 23:13 UTC (permalink / raw)
  To: Kawa mailing list

[-- Attachment #1: Type: text/plain, Size: 352 bytes --]

The attached patch optimizes the call-with-values case where the
consumer is a single-argument procedure to rewrite
(call-with-values producer consumer) as (consumer (producer)).

It works when the consumer is a LambdaExp and also when it is
a ReferenceExp whose value is (known to be) a Procedure.

--
Jamison Hope
The PTR Group
www.theptrgroup.com



[-- Attachment #2: apply-with-values-single-arg.patch --]
[-- Type: application/octet-stream, Size: 2809 bytes --]

Index: gnu/kawa/functions/ChangeLog
===================================================================
--- gnu/kawa/functions/ChangeLog	(revision 8374)
+++ gnu/kawa/functions/ChangeLog	(working copy)
@@ -1,3 +1,8 @@
+2015-03-25  Jamison Hope  <jrh@theptrgroup.com>
+
+	* CompileValues.java (validateApplyWithValues): Optimize case
+	consumer is single-argument procedure.
+
 2015-03-22  Per Bothner  <per@bothner.com>
 
 	* CompilationHelpers.java (validateSetter): Optimize case when
Index: gnu/kawa/functions/CompileValues.java
===================================================================
--- gnu/kawa/functions/CompileValues.java	(revision 8374)
+++ gnu/kawa/functions/CompileValues.java	(working copy)
@@ -33,6 +33,7 @@
             Expression producer = args[0];
             Expression consumer = args[1];
             Type prequired = null;
+            boolean singleArgConsumer = false;
             if (consumer instanceof LambdaExp) {
                 LambdaExp lconsumer = (LambdaExp) consumer;
                 if (lconsumer.min_args == lconsumer.max_args) {
@@ -48,9 +49,35 @@
                         types[i++] = type;
                     }
                     prequired = MultValuesType.create(types);
+                    singleArgConsumer = (lconsumer.min_args == 1);
                 }
+            } else if (consumer instanceof ReferenceExp) {
+                Object rconsumer = ((ReferenceExp)consumer).valueIfConstant();
+                if (rconsumer instanceof Procedure) {
+                    Procedure pconsumer = (Procedure) rconsumer;
+                    if (pconsumer.minArgs() == pconsumer.maxArgs()) {
+                        Type[] types = new Type[pconsumer.minArgs()];
+                        if (pconsumer instanceof MethodProc) {
+                            MethodProc mpconsumer = (MethodProc) pconsumer;
+                            for (int i = 0; i < types.length; ++i) {
+                                types[i] = mpconsumer.getParameterType(i);
+                            }
+                        } else {
+                            for (int i = 0; i < types.length; ++i) {
+                                types[i] = Type.objectType;
+                            }
+                        }
+                        prequired = MultValuesType.create(types);
+                        singleArgConsumer = (pconsumer.minArgs() == 1);
+                    }
+                }
             }
             producer = visitor.visit(producer, prequired);
+            if (singleArgConsumer) {
+                ApplyExp ae = new ApplyExp(consumer, producer);
+                ae.setLine(exp);
+                return visitor.visit(ae, required);
+            }
             if (prequired == null)
                 prequired = producer.getType();
 

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] further optimization for call-with-values
  2015-03-25 23:13 [PATCH] further optimization for call-with-values Jamison Hope
@ 2015-03-27  4:41 ` Per Bothner
  2015-03-27 14:09   ` Jamison Hope
  0 siblings, 1 reply; 3+ messages in thread
From: Per Bothner @ 2015-03-27  4:41 UTC (permalink / raw)
  To: kawa



On 03/25/2015 04:12 PM, Jamison Hope wrote:
> The attached patch optimizes the call-with-values case where the
> consumer is a single-argument procedure to rewrite
> (call-with-values producer consumer) as (consumer (producer)).

Thanks - I checked this in.

> It works when the consumer is a LambdaExp and also when it is
> a ReferenceExp whose value is (known to be) a Procedure.

You "missed" one case: Where the consumer is a ReferenceExp
that is bound to a LambdaExp - i.e. when the consumer is a named
procedure that hasn't been compiled yet.  I tweaked your patch
to handle this.

I also added a test testsuite/values2.scm, and added it
to the check-inlining test to make sure it is optimized and
stays optimized.
-- 
	--Per Bothner
per@bothner.com   http://per.bothner.com/

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] further optimization for call-with-values
  2015-03-27  4:41 ` Per Bothner
@ 2015-03-27 14:09   ` Jamison Hope
  0 siblings, 0 replies; 3+ messages in thread
From: Jamison Hope @ 2015-03-27 14:09 UTC (permalink / raw)
  To: kawa

On Mar 27, 2015, at 12:40 AM, Per Bothner <per@bothner.com> wrote:

> On 03/25/2015 04:12 PM, Jamison Hope wrote:
>> The attached patch optimizes the call-with-values case where the
>> consumer is a single-argument procedure to rewrite
>> (call-with-values producer consumer) as (consumer (producer)).
> 
> Thanks - I checked this in.
> 
>> It works when the consumer is a LambdaExp and also when it is
>> a ReferenceExp whose value is (known to be) a Procedure.
> 
> You "missed" one case: Where the consumer is a ReferenceExp
> that is bound to a LambdaExp - i.e. when the consumer is a named
> procedure that hasn't been compiled yet.  I tweaked your patch
> to handle this.

Ah, so I did.  Good catch.

> I also added a test testsuite/values2.scm, and added it
> to the check-inlining test to make sure it is optimized and
> stays optimized.

Thanks!

--
Jamison Hope
The PTR Group
www.theptrgroup.com



^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2015-03-27 14:09 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-03-25 23:13 [PATCH] further optimization for call-with-values Jamison Hope
2015-03-27  4:41 ` Per Bothner
2015-03-27 14:09   ` Jamison Hope

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).