Charles wrote:...
1) Run with -turbo and see if there is any improvement
Nope
Charles wrote:2) Change "kind == TokenKind.Namespace" to "kind is TokenKind.Namespace"
- Code: Select all
benchmark.cobra(125): warning: Both the left and right sides of "is" are value types ("TokenKind" and "TokenKind"), but
"is" applies to reference types. Use "==" instead.
Charles wrote:3) Change the if-else ladder to a "branch"
Note that regarding 3) some C compilers can do some fancy optimizations for switch() statements involving JMPing to the code location for the case. I don't know if .NET's JIT will do that or not.
You are on to something here...
- Code: Select all
Using '==' operator: 73 ms
Using 'is' operator: 25 ms
Using 'enum' comparison: 3 ms
Charles wrote:Also, without investigating further, I wonder if Cobra is generating a "CobraImp.Equals()" call for your enum comparisons instead of "==". If so, we can fix this. You can check with -kif. See cobra -help.
Bingo. Here's the generated C# for == on an enum comparison. It's boxing it. :
- Code: Select all
if (kind.Equals(global::TokenKind.Namespace)) {
After applying this quick patch to the compiler:
Index: BackEndClr/SharpGenerator.cobra
===================================================================
--- BackEndClr/SharpGenerator.cobra (revision 3047)
+++ BackEndClr/SharpGenerator.cobra (working copy)
@@ -4572,7 +4572,8 @@
if op=='EQ' or op=='NE'
if not left.type.isReference and not right.type.isReference
# struct/value types
- if not left.type inherits PrimitiveType and not right.type inherits PrimitiveType
+ if not left.type inherits PrimitiveType and not right.type inherits PrimitiveType _
+ and not left.type inherits EnumDecl and not right.type inherits EnumDecl
# user-defined structs
op = '.Equals('
else
I get the following C# code:
- Code: Select all
if (kind==(global::TokenKind.Namespace)) {
and these times:
- Code: Select all
Using '==' operator: 73 ms
Using 'is' operator: 25 ms
Using 'enum' comparison: 5 ms
Sweet.