Improve performance of array creation

This commit is contained in:
Alexey Andreev 2019-04-02 17:04:51 +03:00
parent d18d50a823
commit fe68bf700b
3 changed files with 54 additions and 34 deletions

View File

@ -387,6 +387,7 @@ public class JavaScriptTarget implements TeaVMTarget, TeaVMJavaScriptHost {
if (!renderer.render(clsNodes)) { if (!renderer.render(clsNodes)) {
return; return;
} }
runtimeRenderer.renderHandWrittenRuntime("array.js");
renderer.renderStringPool(); renderer.renderStringPool();
renderer.renderStringConstants(); renderer.renderStringConstants();
renderer.renderCompatibilityStubs(); renderer.renderCompatibilityStubs();

View File

@ -0,0 +1,47 @@
/*
* Copyright 2019 Alexey Andreev.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
function $rt_array(cls, data) {
this.type = cls;
this.data = data;
this.$id$ = 0;
this.constructor = $rt_arraycls(cls);
}
$rt_array.prototype = Object.create($rt_objcls().prototype);
$rt_array.prototype.toString = function() {
var str = "[";
for (var i = 0; i < this.data.length; ++i) {
if (i > 0) {
str += ", ";
}
str += this.data[i].toString();
}
str += "]";
return str;
};
$rt_setCloneMethod($rt_array.prototype, function () {
var dataCopy;
if ('slice' in this.data) {
dataCopy = this.data.slice();
} else {
dataCopy = new this.data.constructor(this.data.length);
for (var i = 0; i < dataCopy.length; ++i) {
dataCopy[i] = this.data[i];
}
}
return new $rt_array(this.type, dataCopy);
});

View File

@ -43,7 +43,7 @@ function $rt_isAssignable(from, to) {
} }
function $rt_createArray(cls, sz) { function $rt_createArray(cls, sz) {
var data = new Array(sz); var data = new Array(sz);
var arr = new ($rt_arraycls(cls))(data); var arr = new $rt_array(cls, data);
if (sz > 0) { if (sz > 0) {
var i = 0; var i = 0;
do { do {
@ -54,21 +54,21 @@ function $rt_createArray(cls, sz) {
return arr; return arr;
} }
function $rt_wrapArray(cls, data) { function $rt_wrapArray(cls, data) {
return new ($rt_arraycls(cls))(data); return new $rt_array(cls, data);
} }
function $rt_createUnfilledArray(cls, sz) { function $rt_createUnfilledArray(cls, sz) {
return new ($rt_arraycls(cls))(new Array(sz)); return new $rt_array(cls, new Array(sz));
} }
function $rt_createLongArray(sz) { function $rt_createLongArray(sz) {
var data = new Array(sz); var data = new Array(sz);
var arr = new ($rt_arraycls($rt_longcls()))(data); var arr = new $rt_array($rt_longcls(), data);
for (var i = 0; i < sz; i = (i + 1) | 0) { for (var i = 0; i < sz; i = (i + 1) | 0) {
data[i] = Long_ZERO; data[i] = Long_ZERO;
} }
return arr; return arr;
} }
function $rt_createNumericArray(cls, nativeArray) { function $rt_createNumericArray(cls, nativeArray) {
return new ($rt_arraycls(cls))(nativeArray); return new $rt_array(cls, nativeArray);
} }
function $rt_createCharArray(sz) { function $rt_createCharArray(sz) {
return $rt_createNumericArray($rt_charcls(), new Uint16Array(sz)); return $rt_createNumericArray($rt_charcls(), new Uint16Array(sz));
@ -95,35 +95,7 @@ function $rt_createDoubleArray(sz) {
function $rt_arraycls(cls) { function $rt_arraycls(cls) {
var result = cls.$array; var result = cls.$array;
if (result === null) { if (result === null) {
var arraycls = function(data) { var arraycls = {};
this.data = data;
this.$id$ = 0;
};
arraycls.prototype = new ($rt_objcls())();
arraycls.prototype.constructor = arraycls;
arraycls.prototype.toString = function() {
var str = "[";
for (var i = 0; i < this.data.length; ++i) {
if (i > 0) {
str += ", ";
}
str += this.data[i].toString();
}
str += "]";
return str;
};
$rt_setCloneMethod(arraycls.prototype, function () {
var dataCopy;
if ('slice' in this.data) {
dataCopy = this.data.slice();
} else {
dataCopy = new this.data.constructor(this.data.length);
for (var i = 0; i < dataCopy.length; ++i) {
dataCopy[i] = this.data[i];
}
}
return new arraycls(dataCopy);
});
var name = "[" + cls.$meta.binaryName; var name = "[" + cls.$meta.binaryName;
arraycls.$meta = { item : cls, supertypes : [$rt_objcls()], primitive : false, superclass : $rt_objcls(), arraycls.$meta = { item : cls, supertypes : [$rt_objcls()], primitive : false, superclass : $rt_objcls(),
name : name, binaryName : name, enum : false }; name : name, binaryName : name, enum : false };