[Kde-bindings] Optimisation in smoke method call...

Ruth Ivimey-Cook ruth at ivimey.org
Fri Feb 8 00:01:20 UTC 2013


Folks,

In profiling the code I have been working on, trying to understand why 
it was running very slowly, I have come across what I expect is a 
significant optimisation opportunity. In smokeinvocation.cs, the 
function Invoke starts:

             if (signature.StartsWith("operator==")) {
                 if (args[1] == null && args[3] == null)
                     return true;
                 else if (args[1] == null || args[3] == null)
                     return false;
             }
             ModuleIndex methodId;

If you look into it, the function string.StartsWith is a hugely 
complicated tree of error checks and nested calls, at least 5 levels 
deep, and from what I can tell this call is made for each and every 
smoke-invoked call.

I would suggest, as a minimum, changing it to read:

             if (signature[0] == 'o' && 
signature.StartsWith("operator==")) {
                 if (args[1] == null && args[3] == null)
                     return true;
                 else if (args[1] == null || args[3] == null)
                     return false;
             }
             ModuleIndex methodId;

so as to limit StartsWith calls to only those with a function name 
starting with an 'o'. Better, in my view, would be to replace 
string.StartsWith (here and possibly elsewhere) with a static function:

         public static bool HasPrefix(string str, string pfx)
         {
             if (str.Length < pfx.Length) return false;
             for (int i = 0; i < pfx.Length; i++)
             {
                 if (pfx[i] != str[i])
                     return false;
             }
             return true;
         }

Which should be significantly faster, by dint of not considering issues 
of "culture" and so on. I assume that a null or empty "signature" arg is 
not a situation the code should have to cope with gracefully...


I do have a question related to this, though: why use literal strings 
for function identification at all? It seems to my limited understanding 
of the subject a very odd choice.

Hope this helps,
Ruth

-- 
Software Manager & Engineer
Tel: 01223 414180
Blog: http://www.ivimey.org/blog
LinkedIn: http://uk.linkedin.com/in/ruthivimeycook/

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.kde.org/pipermail/kde-bindings/attachments/20130208/ed437f69/attachment.html>


More information about the Kde-bindings mailing list