Explorar el Código

Optimize some hot paths (#464)

Marko Lahma hace 7 años
padre
commit
b0a379b328

+ 11 - 0
Jint/Native/JsString.cs

@@ -64,6 +64,11 @@ namespace Jint.Native
             return new ConcatenatedString(_value, capacity);
         }
 
+        internal virtual bool IsNullOrEmpty()
+        {
+            return string.IsNullOrEmpty(_value);
+        }
+
         internal static JsString Create(string value)
         {
             if (value.Length <= 1)
@@ -183,6 +188,12 @@ namespace Jint.Native
                 return this;
             }
 
+            internal override bool IsNullOrEmpty()
+            {
+                return _stringBuilder == null && string.IsNullOrEmpty(_value)
+                    || _stringBuilder != null && _stringBuilder.Length == 0;
+            }
+
             public override bool Equals(JsValue other)
             {
                 if (other is ConcatenatedString cs)

+ 2 - 2
Jint/Native/Object/ObjectInstance.cs

@@ -18,7 +18,7 @@ namespace Jint.Native.Object
 {
     public class ObjectInstance : JsValue, IEquatable<ObjectInstance>
     {
-        private Dictionary<string, PropertyDescriptor> _intrinsicProperties;
+        private MruPropertyCache2<PropertyDescriptor> _intrinsicProperties;
         private MruPropertyCache2<PropertyDescriptor> _properties;
 
         public ObjectInstance(Engine engine) : base(Types.Object)
@@ -54,7 +54,7 @@ namespace Jint.Native.Object
         {
             if (_intrinsicProperties == null)
             {
-                _intrinsicProperties = new Dictionary<string, PropertyDescriptor>();
+                _intrinsicProperties = new MruPropertyCache2<PropertyDescriptor>();
             }
 
             _intrinsicProperties[symbol.AsSymbol()] = new PropertyDescriptor(value, writable, enumerable, configurable);

+ 4 - 4
Jint/Runtime/ExpressionIntepreter.cs

@@ -56,8 +56,8 @@ namespace Jint.Runtime
             if (assignmentExpression.Operator == AssignmentOperator.Assign) // "="
             {
 
-                if(lref.IsStrict() 
-                   && !ReferenceEquals(lref.GetBase().TryCast<EnvironmentRecord>(), null) 
+                if(lref.IsStrict()
+                   && lref.GetBase() is EnvironmentRecord
                    && (lref.GetReferencedName() == "eval" || lref.GetReferencedName() == "arguments"))
                 {
                     throw new JavaScriptException(_engine.SyntaxError);
@@ -967,7 +967,7 @@ namespace Jint.Runtime
                     r = value as Reference;
                     if (r != null
                         && r.IsStrict()
-                        && (!ReferenceEquals(r.GetBase().TryCast<EnvironmentRecord>(), null))
+                        && r.GetBase() is EnvironmentRecord
                         && ("eval" == r.GetReferencedName() || "arguments" == r.GetReferencedName()))
                     {
                         throw new JavaScriptException(_engine.SyntaxError);
@@ -984,7 +984,7 @@ namespace Jint.Runtime
                     r = value as Reference;
                     if (r != null
                         && r.IsStrict()
-                        && (!ReferenceEquals(r.GetBase().TryCast<EnvironmentRecord>(), null))
+                        && r.GetBase() is EnvironmentRecord
                         && ("eval" == r.GetReferencedName() || "arguments" == r.GetReferencedName()))
                     {
                         throw new JavaScriptException(_engine.SyntaxError);

+ 2 - 2
Jint/Runtime/StatementInterpreter.cs

@@ -539,8 +539,8 @@ namespace Jint.Runtime
                         throw new ArgumentException();
                     }
 
-                    if (lhs.IsStrict() 
-                        && !ReferenceEquals(lhs.GetBase().TryCast<EnvironmentRecord>(), null) 
+                    if (lhs.IsStrict()
+                        && lhs.GetBase() is EnvironmentRecord
                         && (lhs.GetReferencedName() == "eval" || lhs.GetReferencedName() == "arguments"))
                     {
                         throw new JavaScriptException(_engine.SyntaxError);

+ 1 - 7
Jint/Runtime/TypeConverter.cs

@@ -106,13 +106,7 @@ namespace Jint.Runtime
 
             if (type == Types.String)
             {
-                var s = o.AsString();
-                if (string.IsNullOrEmpty(s))
-                {
-                    return false;
-                }
-
-                return true;
+                return !((JsString) o).IsNullOrEmpty();
             }
 
             return true;