Browse Source

Merge pull request #236 from sebastienros/perf/jsvalue

Compacting JsValue fields
Sébastien Ros 9 years ago
parent
commit
64d63ad72e
1 changed files with 24 additions and 43 deletions
  1. 24 43
      Jint/Native/JsValue.cs

+ 24 - 43
Jint/Native/JsValue.cs

@@ -1,5 +1,4 @@
 using System;
 using System;
-using System.Collections;
 using System.Collections.Generic;
 using System.Collections.Generic;
 using System.Diagnostics;
 using System.Diagnostics;
 using System.Diagnostics.Contracts;
 using System.Diagnostics.Contracts;
@@ -14,7 +13,6 @@ using Jint.Native.RegExp;
 using Jint.Native.String;
 using Jint.Native.String;
 using Jint.Runtime;
 using Jint.Runtime;
 using Jint.Runtime.Interop;
 using Jint.Runtime.Interop;
-using System.Runtime.InteropServices;
 
 
 namespace Jint.Native
 namespace Jint.Native
 {
 {
@@ -28,19 +26,14 @@ namespace Jint.Native
 
 
         public JsValue(bool value)
         public JsValue(bool value)
         {
         {
-            _double = double.NaN;
+            _double = value ? 1.0 : 0.0;
             _object = null;
             _object = null;
-            _string = null;
             _type = Types.Boolean;
             _type = Types.Boolean;
-
-            _bool = value; //Set value last because of 'FieldOffset' constraints
         }
         }
 
 
         public JsValue(double value)
         public JsValue(double value)
         {
         {
-            _bool = false;
             _object = null;
             _object = null;
-            _string = null;
             _type = Types.Number;
             _type = Types.Number;
 
 
             _double = value;
             _double = value;
@@ -48,19 +41,14 @@ namespace Jint.Native
 
 
         public JsValue(string value)
         public JsValue(string value)
         {
         {
-            _bool = false;
             _double = double.NaN;
             _double = double.NaN;
-            _object = null;
+            _object = value;
             _type = Types.String;
             _type = Types.String;
-
-            _string = value;
         }
         }
 
 
         public JsValue(ObjectInstance value)
         public JsValue(ObjectInstance value)
         {
         {
-            _bool = false;
             _double = double.NaN;
             _double = double.NaN;
-            _string = null;
             _type = Types.Object;
             _type = Types.Object;
 
 
             _object = value;
             _object = value;
@@ -68,20 +56,14 @@ namespace Jint.Native
 
 
         private JsValue(Types type)
         private JsValue(Types type)
         {
         {
-            _bool = false;
             _double = double.NaN;
             _double = double.NaN;
             _object = null;
             _object = null;
-            _string = null;
             _type = type;
             _type = type;
         }
         }
 
 
-        private readonly bool _bool;
-
         private readonly double _double;
         private readonly double _double;
 
 
-        private readonly ObjectInstance _object;
-
-        private readonly string _string;
+        private readonly object _object;
 
 
         private readonly Types _type;
         private readonly Types _type;
 
 
@@ -153,7 +135,7 @@ namespace Jint.Native
                 throw new ArgumentException("The value is not an object");
                 throw new ArgumentException("The value is not an object");
             }
             }
 
 
-            return _object;
+            return _object as ObjectInstance;
         }
         }
 
 
         [Pure]
         [Pure]
@@ -163,7 +145,8 @@ namespace Jint.Native
             {
             {
                 throw new ArgumentException("The value is not an array");
                 throw new ArgumentException("The value is not an array");
             }
             }
-            return AsObject() as ArrayInstance;
+
+            return _object as ArrayInstance;
         }
         }
 
 
         [Pure]
         [Pure]
@@ -173,7 +156,8 @@ namespace Jint.Native
             {
             {
                 throw new ArgumentException("The value is not a date");
                 throw new ArgumentException("The value is not a date");
             }
             }
-            return AsObject() as DateInstance;
+
+            return _object as DateInstance;
         }
         }
 
 
         [Pure]
         [Pure]
@@ -183,7 +167,8 @@ namespace Jint.Native
             {
             {
                 throw new ArgumentException("The value is not a date");
                 throw new ArgumentException("The value is not a date");
             }
             }
-            return AsObject() as RegExpInstance;
+
+            return _object as RegExpInstance;
         }
         }
 
 
         [Pure]
         [Pure]
@@ -225,7 +210,7 @@ namespace Jint.Native
                 throw new ArgumentException("The value is not a boolean");
                 throw new ArgumentException("The value is not a boolean");
             }
             }
 
 
-            return _bool;
+            return _double != 0;
         }
         }
 
 
         [Pure]
         [Pure]
@@ -236,12 +221,12 @@ namespace Jint.Native
                 throw new ArgumentException("The value is not a string");
                 throw new ArgumentException("The value is not a string");
             }
             }
 
 
-            if (_string == null)
+            if (_object == null)
             {
             {
                 throw new ArgumentException("The value is not defined");
                 throw new ArgumentException("The value is not defined");
             }
             }
 
 
-            return _string;
+            return _object as string;
         }
         }
 
 
         [Pure]
         [Pure]
@@ -271,11 +256,9 @@ namespace Jint.Native
                 case Types.Null:
                 case Types.Null:
                     return true;
                     return true;
                 case Types.Boolean:
                 case Types.Boolean:
-                    return _bool == other._bool;
-                case Types.String:
-                    return _string == other._string;
                 case Types.Number:
                 case Types.Number:
                     return _double == other._double;
                     return _double == other._double;
+                case Types.String:
                 case Types.Object:
                 case Types.Object:
                     return _object == other._object;
                     return _object == other._object;
                 default:
                 default:
@@ -416,10 +399,10 @@ namespace Jint.Native
                 case Types.Undefined:
                 case Types.Undefined:
                 case Types.Null:
                 case Types.Null:
                     return null;
                     return null;
-                case Types.Boolean:
-                    return _bool;
                 case Types.String:
                 case Types.String:
-                    return _string;
+                    return _object;
+                case Types.Boolean:
+                    return _double != 0;
                 case Types.Number:
                 case Types.Number:
                     return _double;
                     return _double;
                 case Types.Object:
                 case Types.Object:
@@ -429,7 +412,7 @@ namespace Jint.Native
                         return wrapper.Target;
                         return wrapper.Target;
                     }
                     }
 
 
-                    switch (_object.Class)
+                    switch ((_object as ObjectInstance).Class)
                     {
                     {
                         case "Array":
                         case "Array":
                             var arrayInstance = _object as ArrayInstance;
                             var arrayInstance = _object as ArrayInstance;
@@ -516,14 +499,14 @@ namespace Jint.Native
                             IDictionary<string, object> o = new ExpandoObject();
                             IDictionary<string, object> o = new ExpandoObject();
 #endif
 #endif
 
 
-                            foreach (var p in _object.GetOwnProperties())
+                            foreach (var p in (_object as ObjectInstance).GetOwnProperties())
                             {
                             {
                                 if (!p.Value.Enumerable.HasValue || p.Value.Enumerable.Value == false)
                                 if (!p.Value.Enumerable.HasValue || p.Value.Enumerable.Value == false)
                                 {
                                 {
                                     continue;
                                     continue;
                                 }
                                 }
 
 
-                                o.Add(p.Key, _object.Get(p.Key).ToObject());
+                                o.Add(p.Key, (_object as ObjectInstance).Get(p.Key).ToObject());
                             }
                             }
 
 
                             return o;
                             return o;
@@ -575,11 +558,10 @@ namespace Jint.Native
                 case Types.Null:
                 case Types.Null:
                     return "null";
                     return "null";
                 case Types.Boolean:
                 case Types.Boolean:
-                    return _bool.ToString();
-                case Types.String:
-                    return _string;
+                    return _double != 0 ? bool.TrueString : bool.FalseString;
                 case Types.Number:
                 case Types.Number:
                     return _double.ToString();
                     return _double.ToString();
+                case Types.String:
                 case Types.Object:
                 case Types.Object:
                     return _object.ToString();
                     return _object.ToString();
                 default:
                 default:
@@ -662,13 +644,12 @@ namespace Jint.Native
         {
         {
             unchecked
             unchecked
             {
             {
-                var hashCode = _bool.GetHashCode();
+                var hashCode = 0;
                 hashCode = (hashCode * 397) ^ _double.GetHashCode();
                 hashCode = (hashCode * 397) ^ _double.GetHashCode();
                 hashCode = (hashCode * 397) ^ (_object != null ? _object.GetHashCode() : 0);
                 hashCode = (hashCode * 397) ^ (_object != null ? _object.GetHashCode() : 0);
-                hashCode = (hashCode * 397) ^ (_string != null ? _string.GetHashCode() : 0);
                 hashCode = (hashCode * 397) ^ (int)_type;
                 hashCode = (hashCode * 397) ^ (int)_type;
                 return hashCode;
                 return hashCode;
             }
             }
         }
         }
     }
     }
-}
+}