This is the mail archive of the kawa@sourceware.org mailing list for the Kawa project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: call-with-values inliner




On 12/08/2014 11:04 AM, Jamison Hope wrote:
Here's an optimization for call-with-values when the producer and
consumer are both lambda expressions and the consumer lambda takes a
fixed number of arguments.

Great minds think alike - I've also been working on this problem.
(An earlier draft of the "conditional bindings" from my prior email
used call/cc with call-with-values, so it needed good optimization.
However, I ended up using BlockExp instead, which is simpler - though
I did find some bugs in the BlockExp implementation.)

I'll have to look at your suggestions and patch later, though
at first glance it looks reasonable.

I've also been pondering how to handle multiple value types.
One way is using parameterised types - see attached patch.

Another way is with a subclass of OccurrenceType, with a fixed set of
item types.  That can be useful for representing a low-overhead tuple
- e.g. within a method one can push multiple values of the JVM stack.
You can't return multiple values from a method with way, but if a multi-valued
function method is inlined (for example floor/ or values itself) then the
values can be pushed on the JVM stack - and then call-values-values
and related functions can cheaply pass them along.

Still pondering how to get the pieces together -including how to relate the
"boxed" type (i.e. Chained or some other Values) and the "unboxed" type
(just a series of values).
--
	--Per Bothner
per@bothner.com   http://per.bothner.com/
Index: gnu/mapping/Values.java
===================================================================
--- gnu/mapping/Values.java	(revision 8211)
+++ gnu/mapping/Values.java	(working copy)
@@ -13,9 +13,9 @@
     extends AbstractSequence<E> 
     implements Consumable, Externalizable                                    
 {
-    public static final Object[] noArgs = new Object[0];
+    public static final Void[] noArgs = new Void[0];
 
-    public static final Values empty = FromArray.empty;
+    public static final Values empty = Empty.empty;
 
     protected Values () {
     }
@@ -213,9 +213,6 @@
             this.data = data;
         }
 
-        public static final FromArray<Object> empty
-            = new FromArray<Object>(noArgs);
-
         @Override
         public int size() {
             return data.length;
@@ -239,7 +236,6 @@
                 arr[i] = (E) in.readObject();
             data = arr;
         }
-
     }
 
     /** An implementation of Values that uses a java.util.List.
@@ -276,6 +272,57 @@
         }
     }
 
+    public static class Empty<E> extends FromArray<E> {
+        Empty() {
+            super((E[]) noArgs);
+        }
+
+        public static final Empty<?> empty = new Empty<Void>();
+
+    }
+
+    /**
+     * Multiple values implemented using a linked list - with sttaic typing.
+     * For example you can declare a Values varriable consistening of
+     * a {@code Integer} followged by a {@code Double}:
+     * {@code Chained<Number, Integer, Chained<Number, Double, Empty<Number>>> nums2;}
+     * and initialize it like this:
+     * {@code nums2 = new Chained(12.3, new Chained(3.4, empty));}
+     */
+
+    public static class Chained<E,Tfirst extends E,Trest extends Values<E>> extends Values<E> {
+        Tfirst first;
+        Trest rest;
+
+        public Chained(Tfirst first, Trest rest) {
+            this.first = first;
+            this.rest = rest;
+        }
+
+        @Override
+        public int size() {
+            return rest == null ? 1 : 1 + rest.size();
+        }
+
+        @Override
+        public E get(int index) {
+            return index == 0 ? first : rest.get(index-1);
+        }
+
+        @Override
+        public void writeExternal(ObjectOutput out) throws IOException {
+            out.writeObject(first);
+            out.writeObject(rest);
+        }
+
+        @Override
+        public void readExternal(ObjectInput in)
+            throws IOException, ClassNotFoundException {
+            first =  (Tfirst) in.readObject();
+            rest = (Trest) in.readObject();
+        }
+    }
+
     /** A specialization of Values for exactly 2 values.
      */
     public static class Values2<E, V1 extends E, V2 extends E> extends Values<E> {

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]