From 1d06905fc5c3a4c14a645e4a40c5ea76f3201884 Mon Sep 17 00:00:00 2001 From: Ne_Eo Date: Tue, 3 Sep 2024 14:12:11 +0200 Subject: [PATCH] Optimizations --- hscript/Expr.hx | 17 ++++++++++++----- hscript/Interp.hx | 30 ++++++++++++++++++++++++++---- hscript/Parser.hx | 25 ++++++++++++++++--------- 3 files changed, 54 insertions(+), 18 deletions(-) diff --git a/hscript/Expr.hx b/hscript/Expr.hx index e3e7c00..7ac2921 100644 --- a/hscript/Expr.hx +++ b/hscript/Expr.hx @@ -75,7 +75,7 @@ enum Expr { ENew( cl : String, params : Array ); EThrow( e : Expr ); ETry( e : Expr, v : String, t : Null, ecatch : Expr ); - EObject( fl : Array<{ name : String, e : Expr }> ); + EObject( fl : Array ); ETernary( cond : Expr, e1 : Expr, e2 : Expr ); ESwitch( e : Expr, cases : Array, ?defaultExpr : Expr ); EDoWhile( cond : Expr, e : Expr); @@ -86,6 +86,12 @@ enum Expr { EClass( name:String, fields:Array, ?extend:String, interfaces:Array, ?isFinal:Bool, ?isPrivate:Bool ); } +@:structInit +final class ObjectField { + public var name : String; + public var e : Expr; +} + @:structInit final class SwitchCase { public var values : Array; @@ -200,10 +206,11 @@ enum FieldKind { KVar( v : VarDecl ); } -typedef FunctionDecl = { - var args : Array; - var body : Expr; - var ret : Null; +@:structInit +final class FunctionDecl { + public var args : Array; + public var body : Expr; + public var ret : Null; } typedef VarDecl = { diff --git a/hscript/Interp.hx b/hscript/Interp.hx index 425a376..149177e 100644 --- a/hscript/Interp.hx +++ b/hscript/Interp.hx @@ -74,8 +74,9 @@ class Interp { __instanceFields = Type.getInstanceFields(c); if(v is IHScriptCustomClassBehaviour) { var v = cast(v, IHScriptCustomClassBehaviour); - if(v.__class__fields != null) - __instanceFields = __instanceFields.concat(v.__class__fields); + var classFields = v.__class__fields; + if(classFields != null) + __instanceFields = __instanceFields.concat(classFields); _scriptObjectType = SCustomClass; } else if(v is IHScriptCustomBehaviour) { _scriptObjectType = SBehaviourClass; @@ -274,6 +275,7 @@ class Interp { if (_scriptObjectType == SObject && instanceHasField) { UnsafeReflect.setField(scriptObject, id, v); + return v; } else if (_scriptObjectType == SCustomClass && instanceHasField) { var obj = cast(scriptObject, IHScriptCustomClassBehaviour); if(isBypassAccessor) { @@ -511,12 +513,20 @@ class Interp { public function duplicate(h:#if haxe3 Map #else Hash #end) { #if haxe3 var h2 = new Map(); + var keys = h.keys(); + var _hasNext = keys.hasNext; + var _next = keys.next; + while (_hasNext()) { + var k = _next(); + h2.set(k, h.get(k)); + } + return h2; #else var h2 = new Hash(); - #end for (k in h.keys()) h2.set(k, h.get(k)); return h2; + #end } function restore(old:Int):Void { @@ -775,12 +785,24 @@ class Interp { case EReturn(e): returnValue = e == null ? null : expr(e); throw SReturn; - case EFunction(params, fexpr, name, _, isPublic, isStatic, isOverride): + case EFunction(params, fexpr, name, _, isPublic, isStatic, isOverride, isPrivate, isFinal, isInline): var __capturedLocals = duplicate(locals); var capturedLocals:Map = []; + #if haxe3 + var keys = __capturedLocals.keys(); + var _hasNext = keys.hasNext; + var _next = keys.next; + while (_hasNext()) { + var k = _next(); + var e = __capturedLocals.get(k); + if (e != null && e.depth > 0) + capturedLocals.set(k, e); + } + #else for(k=>e in __capturedLocals) if (e != null && e.depth > 0) capturedLocals.set(k, e); + #end var me = this; var hasOpt = false, minParams = 0; diff --git a/hscript/Parser.hx b/hscript/Parser.hx index 379a40a..fa5fcf7 100644 --- a/hscript/Parser.hx +++ b/hscript/Parser.hx @@ -44,6 +44,13 @@ enum Token { TPrepro( s : String ); } +@:structInit +final class TokenInfo { + public var min : Int; + public var max : Int; + public var t : Token; +} + class Parser { // config / variables @@ -100,7 +107,7 @@ class Parser { var tokenMax : Int; var oldTokenMin : Int; var oldTokenMax : Int; - var tokens : List<{ min : Int, max : Int, t : Token }>; + var tokens : List; #else static inline var p1 = 0; static inline var tokenMin = 0; @@ -319,7 +326,7 @@ class Parser { function parseObject(p1:Int):Expr { // parse object - var fl = []; + var fl:Array = []; while( true ) { var tk = token(); var id = null; @@ -2033,7 +2040,7 @@ class Parser { return preprocesorValues.get(id); } - var preprocStack : Array<{ r : Bool }>; + var preprocStack : Array; function parsePreproCond():Expr { var tk = token(); @@ -2096,20 +2103,20 @@ class Parser { case "if": var e = parsePreproCond(); if( evalPreproCond(e) ) { - preprocStack.push({ r : true }); + preprocStack.push(true); return token(); } - preprocStack.push({ r : false }); + preprocStack.push(false); skipTokens(); return token(); case "else", "elseif" if( preprocStack.length > 0 ): - if( preprocStack[preprocStack.length - 1].r ) { - preprocStack[preprocStack.length - 1].r = false; + if( preprocStack[preprocStack.length - 1] ) { + preprocStack[preprocStack.length - 1] = false; skipTokens(); return token(); } else if( id == "else" ) { preprocStack.pop(); - preprocStack.push({ r : true }); + preprocStack.push(true); return token(); } else { // elseif @@ -2216,4 +2223,4 @@ class Parser { } } -} +} \ No newline at end of file