Possible CharHashSet.Add inefficiency

Jun 18, 2013 at 9:14 AM
At the time of this posting, Irony.CharHashSet.Add performs the following logic
base.Add(ch.ToString().ToLowerInvariant()[0]);
base.Add(ch.ToString().ToUpperInvariant()[0]);
Where it would probably be more efficient to perform the the same logic as such:
base.Add(char.ToLowerInvariant(ch));
base.Add(char.ToUpperInvariant(ch));
Where we're using the System.Char class methods to perform the invariant casing instead of needlessly allocating a new string and using the string's implementation of ToCASE which itself may allocate a new string for the result.

It's a "possible inefficiency" because I haven't done any performance tests to back it up. The actual implementation for char and string's ToCASE methods lead to external implementations, so I can't gauge how inefficient one is over the other. Best case scenario, you nix 2*2 needless string allocations and execute in fewer cycles (if you're into counting pennies). Worst case scenario, the ultimate (and framework dependent) ToCASE implementation is optimized for strings. But one would think the string implementation uses the char implementation for each char in the actual string.

On the topic of utilities, isn't it kind of silly to use a try/catch block like you do in Irony.StringList.LongerFirst, instead of actually checking for null?
if(x != null && y != null &&
    x.Length > y.Length)
    return -1;
Coordinator
Jun 21, 2013 at 2:27 AM
the idea of using try/catch is that try/catch is free (no perf penalty) if code executes without exception. But checking costs a few cycles, every time. Yeah, it might look silly, and probably not worth doing it in such simple cases
Coordinator
Jun 21, 2013 at 4:18 AM
about char.To...Invariant - thanks for the tip, missed this static method somehow.. will put it there