{"id":178,"date":"2009-11-26T19:44:16","date_gmt":"2009-11-26T21:44:16","guid":{"rendered":"http:\/\/www.ragestorm.net\/blogs\/?p=178"},"modified":"2009-11-27T09:09:13","modified_gmt":"2009-11-27T11:09:13","slug":"optimize-my-index-yo","status":"publish","type":"post","link":"https:\/\/www.ragestorm.net\/blogs\/?p=178","title":{"rendered":"Optimize My Index Yo"},"content":{"rendered":"<p>I happened to work with UNICODE_STRING recently for some kernel stuff. That simple structure is similar to pascal strings in a way, you got the length and the string doesn&#8217;t have to be null terminated, the length though, is stored in bytes. Normally I don&#8217;t look at the assembly listing of the application I compile, but when you get to debug it you get to see the code the compiler generated. Since some of my functions use strings for input but as null terminated ones, I had to copy the original string to my own copy and add the null character myself. And now that I think of it, I will rewrite everything to use lengths, I don&#8217;t like extra wcslen&#8217;s. :)<\/p>\n<p>Here is a simple usage case:<\/p>\n<pre lang=\"c\">\r\np = (PWCHAR)ExAllocatePool(SomePool, Str->Length + sizeof(WCHAR));\r\nif (p == NULL) return STATUS_NO_MEMORY;\r\nmemcpy(p, Str->buffer, Str->Length);\r\np[Str->Length \/ sizeof(WCHAR)] = UNICODE_NULL;<\/pre>\n<p>I will show you the resulting assembly code, so you can judge yourself:<\/p>\n<pre lang=\"asm\">\r\nshr    esi,1 \r\nxor    ecx,ecx \r\nmov  word ptr [edi+esi*2],cx <\/pre>\n<p>One time the compiler converts the length to WCHAR units, as I asked. Then it realizes it should take that value and use it as an index into the unicode string, thus it has to multiply the index by two, to get to the correct offset. It&#8217;s a waste-y.<br \/>\nThis is the output of a fully optimized code by VS08, shame.<\/p>\n<p>It&#8217;s silly, but this would generate what we really want:<\/p>\n<pre lang=\"c\">\r\n*(PWCHAR)((PWCHAR)p + Str->Length) = UNICODE_NULL;<\/pre>\n<p>With this fix, this time without the extra div\/mul. I just did a few more tests and it seems the dead-code removal and the simplifier algorithms are not perfect with doing some divisions inside the indexing for pointers.<\/p>\n<p><strong>Update<\/strong>: Thanks to commenter Roee Shenberg, it is now clear why the compiler does this extra shr\/mul. The reason is that the compiler can&#8217;t know whether the length is odd, thus it has to round it.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I happened to work with UNICODE_STRING recently for some kernel stuff. That simple structure is similar to pascal strings in a way, you got the length and the string doesn&#8217;t have to be null terminated, the length though, is stored in bytes. Normally I don&#8217;t look at the assembly listing of the application I compile, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","jetpack_publicize_message":""},"categories":[21,5,12,19,6],"tags":[],"jetpack_featured_media_url":"","jetpack_publicize_connections":[],"jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/pbWKd-2S","_links":{"self":[{"href":"https:\/\/www.ragestorm.net\/blogs\/index.php?rest_route=\/wp\/v2\/posts\/178"}],"collection":[{"href":"https:\/\/www.ragestorm.net\/blogs\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.ragestorm.net\/blogs\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.ragestorm.net\/blogs\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ragestorm.net\/blogs\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=178"}],"version-history":[{"count":5,"href":"https:\/\/www.ragestorm.net\/blogs\/index.php?rest_route=\/wp\/v2\/posts\/178\/revisions"}],"predecessor-version":[{"id":182,"href":"https:\/\/www.ragestorm.net\/blogs\/index.php?rest_route=\/wp\/v2\/posts\/178\/revisions\/182"}],"wp:attachment":[{"href":"https:\/\/www.ragestorm.net\/blogs\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=178"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ragestorm.net\/blogs\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=178"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ragestorm.net\/blogs\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=178"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}