speed up string functions

This commit is contained in:
John Alanbrook 2024-07-25 16:14:37 -05:00
parent 0a7f5a5cdd
commit 6047452b62
8 changed files with 62 additions and 79 deletions

View file

@ -409,7 +409,7 @@ Object.dainty_assign = function(target, source)
Object.isObject = function(o) Object.isObject = function(o)
{ {
return (typeof o === 'object' && !Array.isArray(o)); return (o instanceof Object && !(o instanceof Array));
} }
Object.setter_assign = function(target, source) Object.setter_assign = function(target, source)
@ -860,25 +860,6 @@ Object.defineProperty(String.prototype, 'trimchr', {
} }
}); });
Object.defineProperty(String.prototype, 'startswith', {
value: function(val) {
if (!val) return false;
return this.startsWith(val);
}
});
Object.defineProperty(String.prototype, 'endswith', {
value: function(val) {
if (!val) return false;
return this.endsWith(val);
}
});
Object.defineProperty(String.prototype, 'pct', {
value: function(val) {
}
});
Object.defineProperty(String.prototype, 'uc', { value: function() { return this.toUpperCase(); } }); Object.defineProperty(String.prototype, 'uc', { value: function() { return this.toUpperCase(); } });
Object.defineProperty(String.prototype, 'lc', {value:function() { return this.toLowerCase(); }}); Object.defineProperty(String.prototype, 'lc', {value:function() { return this.toLowerCase(); }});
@ -900,6 +881,18 @@ Object.defineProperty(Array.prototype, 'copy', {
} }
}); });
Object.defineProperty(Array.prototype, 'forFrom', {
value: function(n, fn) {
for (var i = n; i < this.length; i++) fn(this[i]);
}
});
Object.defineProperty(Array.prototype, 'forTo', {
value: function(n, fn) {
for (var i = 0; i < n; i++) fn(this[i]);
}
});
Object.defineProperty(Array.prototype, 'dofilter', { Object.defineProperty(Array.prototype, 'dofilter', {
value: function(fn) { value: function(fn) {
for (let i = 0; i < this.length; i++) { for (let i = 0; i < this.length; i++) {
@ -1033,19 +1026,6 @@ value: function(b) {
return c; return c;
}}); }});
/*Object.defineProperty(Array.prototype, 'concat', {
value: function(b) {
var result = [];
for (var i = 0; i < this.length; i++)
result.push(this[i]);
for (var i = 0; i < b.length; i++)
result.push(b[i]);
return result;
}
});*/
Object.defineProperty(Array.prototype, 'normalized', { Object.defineProperty(Array.prototype, 'normalized', {
value: function() { value: function() {
var c = this.slice(); var c = this.slice();

View file

@ -79,9 +79,7 @@ var sprite = {
return; return;
} }
if (p === this.path) return; if (p === this.path) return;
sprite_rmbucket(this);
this._p = p; this._p = p;
sprite_addbucket(this);
this.del_anim?.(); this.del_anim?.();
this.texture = game.texture(p); this.texture = game.texture(p);
@ -101,7 +99,6 @@ var sprite = {
return this._p; return this._p;
}, },
kill() { kill() {
sprite_rmbucket(this);
this.del_anim?.(); this.del_anim?.();
this.anim = undefined; this.anim = undefined;
this.gameobject = undefined; this.gameobject = undefined;
@ -175,7 +172,6 @@ component.sprite = function(obj) {
sp.transform = obj.transform; sp.transform = obj.transform;
sp.guid = prosperon.guid(); sp.guid = prosperon.guid();
allsprites[sp.guid] = sp; allsprites[sp.guid] = sp;
sprite_addbucket(sp);
if (component.sprite.make_hook) component.sprite.make_hook(sp); if (component.sprite.make_hook) component.sprite.make_hook(sp);
return sp; return sp;
} }

View file

@ -569,7 +569,7 @@ var editor = {
obj.ur = sub; obj.ur = sub;
return; return;
} else if (!sub.startswith(obj.ur)) { } else if (!sub.startsWith(obj.ur)) {
console.warn(`Cannot make an ur of type ${sub} from an object with the ur ${obj.ur}`); console.warn(`Cannot make an ur of type ${sub} from an object with the ur ${obj.ur}`);
return; return;
} }
@ -1433,7 +1433,7 @@ replpanel.inputs.tab = function() {
var stub = this.value.fromlast('.'); var stub = this.value.fromlast('.');
var replobj = (editor.selectlist.length === 1) ? "editor.selectlist[0]" : "editor.edit_level"; var replobj = (editor.selectlist.length === 1) ? "editor.selectlist[0]" : "editor.edit_level";
if (this.value.startswith("this.")) if (this.value.startsWith("this."))
keyobj = keyobj.replace("this", replobj); keyobj = keyobj.replace("this", replobj);
if (!this.value.includes('.')) keys.push("this"); if (!this.value.includes('.')) keys.push("this");
@ -1461,7 +1461,7 @@ replpanel.inputs.tab = function() {
if (stub) if (stub)
this.value = o + '.' + comp; this.value = o + '.' + comp;
else if (this.value.endswith('.')) else if (this.value.endsWith('.'))
this.value = o + '.' + comp; this.value = o + '.' + comp;
else else
this.value = comp; this.value = comp;
@ -1677,7 +1677,7 @@ var openlevelpanel = Object.copy(inputpanel, {
keycb() { keycb() {
if(this.value) if(this.value)
this.assets = this.allassets.filter(x => x.startswith(this.value)); this.assets = this.allassets.filter(x => x.startsWith(this.value));
else else
this.assets = this.allassets.slice(); this.assets = this.allassets.slice();
for (var m in this.mumlist) for (var m in this.mumlist)

View file

@ -25,6 +25,14 @@ Object.defineProperty(String.prototype, "folder", {
globalThis.Resources = {}; globalThis.Resources = {};
Resources.rm_fn = function(fnstr, text)
{
while (text.match(fnstr)) {
}
}
Resources.rm_fn.doc = "Remove calls to a given function from a given text script.";
Resources.replpath = function (str, path) { Resources.replpath = function (str, path) {
if (!str) return str; if (!str) return str;
if (str[0] === "/") return str.rm(0); if (str[0] === "/") return str.rm(0);
@ -49,6 +57,10 @@ Resources.replstrs = function (path) {
var regexp = /"[^"\s]*?\.[^"\s]+?"/g; var regexp = /"[^"\s]*?\.[^"\s]+?"/g;
var stem = path.dir(); var stem = path.dir();
// remove console statements
//script = script.replace(/console\.(.*?)\(.*?\)/g, '');
//script = script.replace(/assert\(.*?\)/g, '');
script = script.replace(regexp, function (str) { script = script.replace(regexp, function (str) {
var newstr = Resources.replpath(str.trimchr('"'), path); var newstr = Resources.replpath(str.trimchr('"'), path);
return `"${newstr}"`; return `"${newstr}"`;
@ -222,7 +234,7 @@ global.check_registers = function (obj) {
} }
for (var k in obj) { for (var k in obj) {
if (!k.startswith("on_")) continue; if (!k.startsWith("on_")) continue;
var signal = k.fromfirst("on_"); var signal = k.fromfirst("on_");
Event.observe(signal, obj, obj[k]); Event.observe(signal, obj, obj[k]);
} }
@ -728,7 +740,7 @@ var Register = {
var fns = []; var fns = [];
n.register = function (fn, oname) { n.register = function (fn, oname) {
if (typeof fn !== "function") return; if (!(fn instanceof Function)) return;
var dofn = function(...args) { var dofn = function(...args) {
var st = profile.now(); var st = profile.now();

View file

@ -61,8 +61,8 @@ var entity = {
}, },
sync() { sync() {
this.components.forEach(function(x) { x.sync?.(); }); for (var c of Object.values(this.components)) c.sync?.();
this.objects.forEach(function(x) { x.sync(); }); for (var o of Object.values(this.objects)) o.sync();
}, },
delay(fn, seconds) { delay(fn, seconds) {
@ -150,7 +150,7 @@ var entity = {
if (!text) if (!text)
ent.ur = emptyur; ent.ur = emptyur;
else if (typeof text === 'object' && text) {// assume it's an ur else if (text instanceof Object) {// assume it's an ur
ent.ur = text; ent.ur = text;
text = ent.ur.text; text = ent.ur.text;
config = [ent.ur.data, config].filter(x => x).flat(); config = [ent.ur.data, config].filter(x => x).flat();
@ -164,19 +164,19 @@ var entity = {
if (typeof text === 'string') if (typeof text === 'string')
use(text, ent); use(text, ent);
else if (Array.isArray(text)) else if (Array.isArray(text))
text.forEach(path => use(path,ent)); for (var path of text) use(path,ent);
if (typeof config === 'string') if (typeof config === 'string')
Object.merge(ent, json.decode(Resources.replstrs(config))); Object.merge(ent, json.decode(Resources.replstrs(config)));
else if (Array.isArray(config)) else if (Array.isArray(config))
config.forEach(function(path) { for (var path of config) {
if (typeof path === 'string') { if (typeof path === 'string') {
console.info(`ingesting ${path} ...`); console.info(`ingesting ${path} ...`);
Object.merge(ent, json.decode(Resources.replstrs(path))); Object.merge(ent, json.decode(Resources.replstrs(path)));
} }
else if (typeof path === 'object') else if (path instanceof Object)
Object.merge(ent,path); Object.merge(ent,path);
}); };
ent.reparent(this); ent.reparent(this);
@ -191,9 +191,9 @@ var entity = {
check_registers(ent); check_registers(ent);
if (typeof ent.load === 'function') ent.load(); if (ent.load instanceof Function) ent.load();
if (sim.playing()) if (sim.playing())
if (typeof ent.start === 'function') ent.start(); if (ent.start instanceof Function) ent.start();
Object.hide(ent, 'ur', 'components', 'objects', 'timers', 'guid', 'master'); Object.hide(ent, 'ur', 'components', 'objects', 'timers', 'guid', 'master');
@ -235,8 +235,8 @@ var entity = {
return ent; return ent;
}, },
disable() { this.components.forEach(function(x) { x.disable(); }); }, disable() { for (var x of this.components) x.disable(); },
enable() { this.components.forEach(function(x) { x.enable(); }); }, enable() { for (var x of this.components) x.enable(); },
this2screen(pos) { return game.camera.world2view(this.this2world(pos)); }, this2screen(pos) { return game.camera.world2view(this.this2world(pos)); },
screen2this(pos) { return this.world2this(game.camera.view2world(pos)); }, screen2this(pos) { return this.world2this(game.camera.view2world(pos)); },
@ -280,7 +280,7 @@ var entity = {
var bb = boxes.shift(); var bb = boxes.shift();
boxes.forEach(function(x) { bb = bbox.expand(bb, x); }); for (var x of boxes) bb = bbox.expand(bb, x);
bb = bbox.move(bb, this.pos); bb = bbox.move(bb, this.pos);
@ -338,7 +338,7 @@ dup(diff) {
this.__kill = true; this.__kill = true;
console.spam(`Killing entity of type ${this.ur}`); console.spam(`Killing entity of type ${this.ur}`);
this.timers.forEach(t => t()); for (var t of this.timers) t();
this.timers = []; this.timers = [];
Event.rm_obj(this); Event.rm_obj(this);
input.do_uncontrol(this); input.do_uncontrol(this);
@ -358,13 +358,12 @@ dup(diff) {
delete this.components; delete this.components;
this.clear(); this.clear();
if (typeof this.stop === 'function') this.stop(); if (this.stop instanceof Function) this.stop();
game.tag_clear_guid(this.guid); game.tag_clear_guid(this.guid);
for (var i in this) { for (var i in this) {
if (typeof this[i] === 'object') delete this[i]; if (this[i] instanceof Object || this[i] instanceof Function) delete this[i];
if (typeof this[i] === 'function') delete this[i];
} }
}, },
@ -482,17 +481,17 @@ var gameobject = {
var newpos = relative.this2world(x); var newpos = relative.this2world(x);
var move = newpos.sub(this.pos); var move = newpos.sub(this.pos);
this.rpos = newpos; this.rpos = newpos;
this.objects.forEach(x => x.move(move)); for (var o of this.objects) o.move(move);
}, },
set_angle(x, relative = world) { set_angle(x, relative = world) {
var newangle = relative.angle + x; var newangle = relative.angle + x;
var diff = newangle - this.angle; var diff = newangle - this.angle;
this.rangle = newangle; this.rangle = newangle;
this.objects.forEach(obj => { for (var obj of this.objects) {
obj.rotate(diff); obj.rotate(diff);
obj.set_pos(Vector.rotate(obj.get_pos(obj.master), diff), obj.master); obj.set_pos(Vector.rotate(obj.get_pos(obj.master), diff), obj.master);
}); }
}, },
set_scale(x, relative = world) { set_scale(x, relative = world) {
@ -500,10 +499,10 @@ var gameobject = {
var newscale = relative.scale.map((s,i) => x[i]*s); var newscale = relative.scale.map((s,i) => x[i]*s);
var pct = this.scale.map((s,i) => newscale[i]/s); var pct = this.scale.map((s,i) => newscale[i]/s);
this.rscale = newscale; this.rscale = newscale;
this.objects.forEach(obj => { for (var obj of this.objects) {
obj.grow(pct); obj.grow(pct);
obj.set_pos(obj.get_pos(obj.master).map((x,i) => x*pct[i]), obj.master); obj.set_pos(obj.get_pos(obj.master).map((x,i) => x*pct[i]), obj.master);
}); };
}, },
get_pos(relative = world) { get_pos(relative = world) {
@ -650,17 +649,17 @@ function apply_ur(u, ent) {
if (typeof text === 'string') if (typeof text === 'string')
use(text, ent); use(text, ent);
else if (Array.isArray(text)) else if (Array.isArray(text))
text.forEach(path => use(path,ent)); for (var path of text) use(path,ent);
if (typeof data === 'string') if (typeof data === 'string')
Object.merge(ent, json.decode(Resources.replstrs(data))); Object.merge(ent, json.decode(Resources.replstrs(data)));
else if (Array.isArray(data)) { else if (Array.isArray(data)) {
data.forEach(function(path) { for (var path of data) {
if (typeof path === 'string') if (typeof path === 'string')
Object.merge(ent, json.decode(Resources.replstrs(data))); Object.merge(ent, json.decode(Resources.replstrs(data)));
else if (typeof path === 'object') else if (path instanceof Object)
Object.merge(ent,path); Object.merge(ent,path);
}); };
} }
} }
} }

View file

@ -126,7 +126,7 @@ profile.endframe = function()
var print_frame = function(frame, indent, title) var print_frame = function(frame, indent, title)
{ {
var avg = frame._times.reduce((sum, e) => sum += e)/frame._times.length; var avg = frame._times.reduce((sum, e) => sum += e)/frame._times.length;
say(indent + `${title} ::::: ${profile.best_t(avg)}`); say(indent + `${title} ::::: ${profile.best_t(avg)} (${frame._times.length} hits)`);
for (var i in frame) { for (var i in frame) {
if (i === '_times') continue; if (i === '_times') continue;

View file

@ -660,11 +660,7 @@ render.box = function(pos, wh, color = Color.white) {
check_flush(render.flush_poly); check_flush(render.flush_poly);
}; };
render.window = function(pos, wh, color) { render.window = function(pos, wh, color) { render.box(pos.add(wh.scale(0.5)),wh,color); };
var p = pos.slice();
p = p.add(wh.scale(0.5));
render.box(p,wh,color);
};
render.text_bb = function(str, size = 1, wrap = -1, pos = [0,0]) render.text_bb = function(str, size = 1, wrap = -1, pos = [0,0])
{ {

View file

@ -171,7 +171,7 @@ var listpanel = Object.copy(inputpanel, {
keycb() { keycb() {
if(this.value) if(this.value)
this.assets = this.allassets.filter(x => x.startswith(this.value)); this.assets = this.allassets.filter(x => x.startsWith(this.value));
else else
this.assets = this.allassets.slice(); this.assets = this.allassets.slice();
for (var m in this.mumlist) for (var m in this.mumlist)