[corlib] Optimize System.Nullable<T>.GetValueOrDefault().
authorJonathan Pryor <jonpryor@vt.edu>
Thu, 20 Dec 2012 17:09:05 +0000 (12:09 -0500)
committerJonathan Pryor <jonpryor@vt.edu>
Thu, 20 Dec 2012 17:09:05 +0000 (12:09 -0500)
Optimize System.Nullable<T>.GetValueOrDefault().

See discussion at:
http://ericlippert.com/2012/12/20/nullable-micro-optimizations-part-one/

If a variable of nullable value type is initialized with the
default constructor then the [has_value] field will be its
default value, false, and the value field will be default(T).

Since we know that Nullable<T>.value will be the default value when
the default constructor has executed, we can return it directly
instead of checking the has_value field.

external/ikvm
mcs/class/corlib/System/Nullable.cs

index 08448c355b296bebff12b1210fc498594e6dabda..3db612102e06332eebebd128cd06be68b03c2f0e 160000 (submodule)
@@ -1 +1 @@
-Subproject commit 08448c355b296bebff12b1210fc498594e6dabda
+Subproject commit 3db612102e06332eebebd128cd06be68b03c2f0e
index b8ac671d69978cf8fbb0eb06db8344be9e803a99..86a7bd7d4a4df5a183681ddc2479a6c973790983 100644 (file)
@@ -140,7 +140,7 @@ namespace System
 
                public T GetValueOrDefault ()
                {
-                       return has_value ? value : default (T);
+                       return value;
                }
 
                public T GetValueOrDefault (T defaultValue)