{"id":221,"date":"2007-05-18T20:30:17","date_gmt":"2007-05-19T00:30:17","guid":{"rendered":"http:\/\/www.rakkar.org\/blog\/?p=221"},"modified":"2007-05-18T20:30:17","modified_gmt":"2007-05-19T00:30:17","slug":"keymapper","status":"publish","type":"post","link":"https:\/\/rakkar.org\/blog\/index.php\/2007\/05\/18\/keymapper\/","title":{"rendered":"Botched Keymapper = wasted half day"},"content":{"rendered":"<p>\t\t\t\tI did some performance analysis today and found that the freaking keymapper was taking 28% of the total CPU time!  It even took longer than the render cycle.<\/p>\n<p>The keymapper was one of the first tasks I gave out, about 6 months ago, to a junior programmer.  It&#8217;s supposed to load an XML of strings of actions, and bindings.  It would map the strings to numbers, and create an association between the two.  So that given a list of pressed keys I can get the corresponding actions, etc.<\/p>\n<p>The main problems are dealing with multiple keys to trigger a single action, actions that take multiple keys to trigger, and how to handle the situation where more than one action is possible given multiple keys.<\/p>\n<p>The original programmer loaded the XML files and just kept them in memory.  He then did string lookups only, not using the numerical equivalents like I told him to.  Worse, these were XML based string lookups, so it would have to parse the whole XML file to look up a particular binding, then a particular string.  Worse, in order to handle the case with duplicate mappings, he would scan the whole list again.  So it&#8217;s slow * slow * slow.<\/p>\n<p>On top of that he didn&#8217;t handle the situation with multiple bindings for a given action, so you couldn&#8217;t do left control OR right control to fire.  Only one or the other.  And he didn&#8217;t correctly handle the situation with multiple keys to multiple actions, so if I pressed a key combo to trigger an action, and that combo included other actions, those other actions would trigger too.<\/p>\n<p>Basically he made an O(1)  operation (if you&#8217;re smart about it), or an O(n) operation (if you&#8217;re not), into an O(n^3) operation.<\/p>\n<p>I rewrote it.  I got each call from 160.8 nanoseconds to 4.5 nanoseconds, has the two features his didn&#8217;t, and I did it in half a day, where he took 3 days.<\/p>\n<p>Incidentally, it was another bad programmer that wrote the XML lookup to begin with.  He also didn&#8217;t follow my instructions on how to do it efficiently.  Sort of bad programmer squared.\t\t<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I did some performance analysis today and found that the freaking keymapper was taking 28% of the total CPU time! It even took longer than the render cycle. The keymapper was one of the first tasks I gave out, about 6 months ago, to a junior programmer. It&#8217;s supposed to load an XML of strings [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[3],"tags":[],"_links":{"self":[{"href":"https:\/\/rakkar.org\/blog\/index.php\/wp-json\/wp\/v2\/posts\/221"}],"collection":[{"href":"https:\/\/rakkar.org\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/rakkar.org\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/rakkar.org\/blog\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/rakkar.org\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=221"}],"version-history":[{"count":0,"href":"https:\/\/rakkar.org\/blog\/index.php\/wp-json\/wp\/v2\/posts\/221\/revisions"}],"wp:attachment":[{"href":"https:\/\/rakkar.org\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=221"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/rakkar.org\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=221"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/rakkar.org\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=221"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}