Forums

Naming convention for constants

General discussion about Cobra. Releases and general news will also be posted here.
Feel free to ask questions or just say "Hello".

Re: Naming convention for constants

Postby nerdzero » Thu Oct 03, 2013 8:42 am

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.
Not much faster. This is definitely boxing it though.

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.
nerdzero
 
Posts: 286
Location: Chicago, IL

Re: Naming convention for constants

Postby Charles » Thu Oct 03, 2013 5:29 pm

Both of your timings used the same labels:

Code: Select all
Using '==' operator: 73 ms
Using 'is' operator: 25 ms
Using 'enum' comparison: 3 ms


Did you mean to call the 3rd line "Using branch" in the first set of timings?
Charles
 
Posts: 2510
Location: Los Angeles, CA

Re: Naming convention for constants

Postby nerdzero » Thu Oct 03, 2013 7:24 pm

Yeah, I just didn't change the printed string when I was testing it. The first set was using a branch on the enum and the second set was using == after the patch.
nerdzero
 
Posts: 286
Location: Chicago, IL

Previous

Return to Discussion

Who is online

Users browsing this forum: No registered users and 4 guests

cron