Browse Source

optimize placing record/array in array

Vladislav Folts 9 years ago
parent
commit
258a597812

BIN
bin/compiled.zip


+ 3 - 3
src/eberon/EberonContextInPlace.ob

@@ -45,11 +45,11 @@ BEGIN
 
         IF type IS EberonRecord.PRecord THEN
             EberonRecord.ensureCanBeInstantiated(SELF, type, EberonRecord.instantiateForCopy);
-            IF e.designator() # NIL THEN
+            IF Expression.isTemporary(e^) THEN
+                SELF.code := SELF.code + e.code();
+            ELSE
                 l <- SELF.root().language();
                 SELF.code := SELF.code + l.rtl.clone(e.code(), l.types.typeInfo(type), "undefined");
-            ELSE (* do not clone if it is temporary, e.g. constructor call *)
-                SELF.code := SELF.code + e.code();
             END;
         ELSE
             l <- SELF.root().language();

+ 10 - 2
src/eberon/EberonDynamicArray.ob

@@ -124,9 +124,17 @@ BEGIN
     
     t <- e.type();
     IF t IS Types.PArray THEN
-        SELF.code := Cast.cloneArray(t, SELF.code, SELF.cx);
+        IF Expression.isTemporary(e^) THEN
+            SELF.code := e.code();
+        ELSE
+            SELF.code := Cast.cloneArray(t, SELF.code, SELF.cx);
+        END;
     ELSIF t IS Record.PType THEN
-        SELF.code := SELF.cx.language.rtl.clone(SELF.code, EberonOperator.generateTypeInfo(t), Record.constructor(SELF.cx.cx^, t^));
+        IF Expression.isTemporary(e^) THEN
+            SELF.code := e.code();
+        ELSE
+            SELF.code := SELF.cx.language.rtl.clone(SELF.code, EberonOperator.generateTypeInfo(t), Record.constructor(SELF.cx.cx^, t^));
+        END;
     END;
 END AddCallGenerator.handleArgument;
 

+ 4 - 0
src/ob/Expression.ob

@@ -105,4 +105,8 @@ BEGIN
     RETURN result
 END;
 
+PROCEDURE isTemporary*(e: Type): BOOLEAN;
+    RETURN e.designator() = NIL;
+END;
+
 END Expression.

+ 18 - 0
test/expected/eberon/dynamic_array.js

@@ -42,6 +42,24 @@ function passArrayOfArraysByRef(a/*VAR ARRAY *, 3 OF INTEGER*/){
 	var result = [];
 	RTL$.copy(result, a, {array: {array: null}});
 }
+
+function arrayOfRecords(){
+	var $scope1 = $scope + ".arrayOfRecords";
+	function T(){
+	}
+	var a = [];
+	a.push(new T());
+}
+
+function arrayOfArrays(){
+	var aa = [];
+	
+	function f(){
+		var a = [];
+		return a.slice();
+	}
+	aa.push(f());
+}
 dynamicInt.push(3);
 dynamicInt.push(i);
 dynamicInt.push(byte);

+ 25 - 0
test/input/eberon/dynamic_array.ob

@@ -56,6 +56,31 @@ BEGIN
     a := result;
 END passArrayOfArraysByRef;
 
+PROCEDURE arrayOfRecords();
+TYPE
+    T = RECORD END;
+VAR
+    a: ARRAY * OF T;
+BEGIN
+    a.add(T());
+END;
+
+PROCEDURE arrayOfArrays();
+TYPE
+    A = ARRAY * OF INTEGER;
+VAR
+    aa: ARRAY * OF A;
+
+    PROCEDURE f(): A;
+    VAR
+        a: A;
+    BEGIN
+        RETURN a;
+    END;
+BEGIN
+    aa.add(f());
+END;
+
 BEGIN
     dynamicInt.add(3);
     dynamicInt.add(i);