{"id":223,"date":"2014-06-04T19:02:58","date_gmt":"2014-06-04T19:02:58","guid":{"rendered":"http:\/\/finaldie.com\/blog\/?p=223"},"modified":"2014-06-04T19:02:58","modified_gmt":"2014-06-04T19:02:58","slug":"refactor-fhash-is-done","status":"publish","type":"post","link":"https:\/\/finaldie.com\/blog\/refactor-fhash-is-done\/","title":{"rendered":"Refactor fhash is done"},"content":{"rendered":"<h1>Overview<\/h1>\n<p>Aha, After some days, I have finished the refactor task for <a href=\"https:\/\/github.com\/finaldie\/final_libs\/tree\/0.5\/fhash\">fhash<\/a>, it&#8217;s really really big improvement for <a href=\"https:\/\/github.com\/finaldie\/final_libs\">flibs<\/a>, since many many components depend on it, such as:<\/p>\n<ul>\n<li>thread pool<\/li>\n<li>event framework<\/li>\n<li>pcap conversion lib<\/li>\n<li>log<\/li>\n<li>&#8230;<\/li>\n<\/ul>\n<h1>Why need to do refactor<\/h1>\n<p>The old fhash have some defects, such as:<\/p>\n<ul>\n<li>hard to iterate<\/li>\n<li>hard to extend<\/li>\n<li>hard to modify fhash when iteration<\/li>\n<li>some performance issues during iteration and fhash_set interface<\/li>\n<\/ul>\n<p>so finally, I decided to rewrite it. Before we go through the new design, let&#8217;s take a look on the old design at below section.<\/p>\n<h1>Graph of Old\/New design<\/h1>\n<div style=\"width: 640px; height: 480px; margin: 10px; position: relative;\"><iframe allowfullscreen frameborder=\"0\" style=\"width:640px; height:480px\" src=\"https:\/\/www.lucidchart.com\/documents\/embeddedchart\/3cb9fdb5-c874-4cc9-b4b6-915b2daa0c2f\"><\/iframe><a href=\"https:\/\/www.lucidchart.com\/pages\/examples\/mind_mapping_software\" style=\"margin: 0; padding: 0; border: none; display: inline-block; position: absolute; bottom: 5px; left: 5px;\"><img alt=\"mind mapping software\"title=\"Lucidchart online diagrams\"style=\"width: 100px; height: 30px; margin: 0; padding: 0; border-image: none; border: none; display: block\"src=\"https:\/\/www.lucidchart.com\/img\/diagrams-lucidchart.png\"\/><\/a><\/div>\n<h1>The new design<\/h1>\n<p>The new design fix all the issues in old design, which much clean and user friendly.<\/p>\n<p>Let&#8217;s take a look the core APIs:<\/p>\n<pre><code>fhash* fhash_create(uint32_t init_size, fhash_opt opt, uint32_t flags);\nvoid fhash_delete(fhash* table);\n\nvoid fhash_set(fhash* table,\n               const void* key, key_sz_t key_sz,\n               const void* value, value_sz_t value_sz);\nvoid* fhash_get(fhash* table, const void* key, key_sz_t key_sz,\n               value_sz_t* value_sz);\nvoid* fhash_fetch_and_del(fhash* table,\n               const void* key, key_sz_t key_sz,\n               void* value, value_sz_t value_sz);\n\nfhash_iter fhash_iter_new(fhash* table);\nvoid fhash_iter_release(fhash_iter* iter);\nvoid* fhash_next(fhash_iter* iter);\nvoid fhash_foreach(fhash* table, fhash_each_cb cb, void* ud);\n\nint fhash_rehash(fhash* table, uint32_t new_size);\nvoid fhash_profile(fhash* table, int flags, fhash_profile_data* data);\n<\/code><\/pre>\n<h1>Full API and Example Documents<\/h1>\n<p>Please refer to <a href=\"https:\/\/github.com\/finaldie\/final_libs\/wiki\">wiki<\/a><\/p>\n<h1>The benchmark<\/h1>\n<p>And for this time, I just add a benchmark tool for fhash, so that I can review the performance when I need it, let&#8217;s take a look one result I ran it on my virtual box:<\/p>\n<pre><code>========= fhash testing without auto rehash =========\nfhash_set x100000 spend time: 9189450 usec\nfhash_get x100000 spend time: 6535296 usec\nfhash_next x100000 spend time: 1111 usec\nfhash_rehash (index double), ret: 0, spend time: 42825 usec\n[index]: used: 63226, total: 100000, usage rate: 0.632260\n[slots]: used: 100000, total: 107241, usage rate: 0.932479\nfhash_del x100000 spend time: 32075 usec\n========= fhash testing with auto rehash =========\nfhash_set x100000 spend time: 112542 usec\nfhash_get x100000 spend time: 35542 usec\nfhash_next x100000 spend time: 3333 usec\nfhash_rehash (index double), ret: 0, spend time: 57153 usec\n[index]: used: 63226, total: 100000, usage rate: 0.632260\n[slots]: used: 100000, total: 107241, usage rate: 0.932479\nfhash_del x100000 spend time: 37410 usec\n<\/code><\/pre>\n<p>So, from above, we can see the performance comparison of disabling\/enabling auto rehash, the result with auto rehash is winner, that means in a normal case(if user cannot sure how many items will be putted into hash table), the best solution is enable auto rehash feature, it will avoid the hash table convert to a list.<\/p>\n<h1>The End<\/h1>\n<p>After rewrite fhash, I realized that:<\/p>\n<ul>\n<li>To create a user friendly, extendable program is more important than the performance, since the most important thing is that how can we get start with a new library? If the library is hard to use, user will give up and try to find a another library.<\/li>\n<li>Another hand, the documentation is also a very important part in the project, since that&#8217;s the easiest way to tell user: what it is and how to use it.<\/li>\n<\/ul>\n<h1>Next Step<\/h1>\n<p>In the future, I have some actions to optimize it:<\/p>\n<ul>\n<li>I can add a counter in every hash node, to record the get\/set actions frequency, so that fhash can have ability to optimize the hash node list in every rehash or some other trigger point, so the highest hotspot node will be putted at the front of list.<\/li>\n<li>Use a list instead of array, and compare the performance impact.<\/li>\n<\/ul>\n<p>Ok, let&#8217;s hacking begin!~<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Overview Aha, After some days, I have finished the refactor task for fhash, it&#8217;s really really big improvement for flibs, since many many components depend on it, such as: thread pool event framework pcap conversion lib log &#8230; Why need to do refactor The old fhash have some defects, such as: hard to iterate hard &#8230; <a title=\"Refactor fhash is done\" class=\"read-more\" href=\"https:\/\/finaldie.com\/blog\/refactor-fhash-is-done\/\" aria-label=\"More on Refactor fhash is done\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v22.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Refactor fhash is done - Final Blog<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/finaldie.com\/blog\/refactor-fhash-is-done\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Refactor fhash is done - Final Blog\" \/>\n<meta property=\"og:description\" content=\"Overview Aha, After some days, I have finished the refactor task for fhash, it&#8217;s really really big improvement for flibs, since many many components depend on it, such as: thread pool event framework pcap conversion lib log &#8230; Why need to do refactor The old fhash have some defects, such as: hard to iterate hard ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/finaldie.com\/blog\/refactor-fhash-is-done\/\" \/>\n<meta property=\"og:site_name\" content=\"Final Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/hu.yuzhang\" \/>\n<meta property=\"article:published_time\" content=\"2014-06-04T19:02:58+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.lucidchart.com\/img\/diagrams-lucidchart.png\" \/>\n<meta name=\"author\" content=\"final\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@hyzwowtools\" \/>\n<meta name=\"twitter:site\" content=\"@hyzwowtools\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"final\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/finaldie.com\/blog\/refactor-fhash-is-done\/\",\"url\":\"https:\/\/finaldie.com\/blog\/refactor-fhash-is-done\/\",\"name\":\"Refactor fhash is done - Final Blog\",\"isPartOf\":{\"@id\":\"https:\/\/finaldie.com\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/finaldie.com\/blog\/refactor-fhash-is-done\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/finaldie.com\/blog\/refactor-fhash-is-done\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.lucidchart.com\/img\/diagrams-lucidchart.png\",\"datePublished\":\"2014-06-04T19:02:58+00:00\",\"dateModified\":\"2014-06-04T19:02:58+00:00\",\"author\":{\"@id\":\"https:\/\/finaldie.com\/blog\/#\/schema\/person\/2d4c840d6e8e197f8ade98af2bd2fab3\"},\"breadcrumb\":{\"@id\":\"https:\/\/finaldie.com\/blog\/refactor-fhash-is-done\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/finaldie.com\/blog\/refactor-fhash-is-done\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/finaldie.com\/blog\/refactor-fhash-is-done\/#primaryimage\",\"url\":\"https:\/\/www.lucidchart.com\/img\/diagrams-lucidchart.png\",\"contentUrl\":\"https:\/\/www.lucidchart.com\/img\/diagrams-lucidchart.png\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/finaldie.com\/blog\/refactor-fhash-is-done\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/finaldie.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Refactor fhash is done\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/finaldie.com\/blog\/#website\",\"url\":\"https:\/\/finaldie.com\/blog\/\",\"name\":\"Final Blog\",\"description\":\"As simple as possible...\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/finaldie.com\/blog\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/finaldie.com\/blog\/#\/schema\/person\/2d4c840d6e8e197f8ade98af2bd2fab3\",\"name\":\"final\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/finaldie.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/4c720545b79ddb0f23b527e0bbcfd9bc?s=96&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/4c720545b79ddb0f23b527e0bbcfd9bc?s=96&r=g\",\"caption\":\"final\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Refactor fhash is done - Final Blog","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/finaldie.com\/blog\/refactor-fhash-is-done\/","og_locale":"en_US","og_type":"article","og_title":"Refactor fhash is done - Final Blog","og_description":"Overview Aha, After some days, I have finished the refactor task for fhash, it&#8217;s really really big improvement for flibs, since many many components depend on it, such as: thread pool event framework pcap conversion lib log &#8230; Why need to do refactor The old fhash have some defects, such as: hard to iterate hard ... Read more","og_url":"https:\/\/finaldie.com\/blog\/refactor-fhash-is-done\/","og_site_name":"Final Blog","article_publisher":"https:\/\/www.facebook.com\/hu.yuzhang","article_published_time":"2014-06-04T19:02:58+00:00","og_image":[{"url":"https:\/\/www.lucidchart.com\/img\/diagrams-lucidchart.png"}],"author":"final","twitter_card":"summary_large_image","twitter_creator":"@hyzwowtools","twitter_site":"@hyzwowtools","twitter_misc":{"Written by":"final","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/finaldie.com\/blog\/refactor-fhash-is-done\/","url":"https:\/\/finaldie.com\/blog\/refactor-fhash-is-done\/","name":"Refactor fhash is done - Final Blog","isPartOf":{"@id":"https:\/\/finaldie.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/finaldie.com\/blog\/refactor-fhash-is-done\/#primaryimage"},"image":{"@id":"https:\/\/finaldie.com\/blog\/refactor-fhash-is-done\/#primaryimage"},"thumbnailUrl":"https:\/\/www.lucidchart.com\/img\/diagrams-lucidchart.png","datePublished":"2014-06-04T19:02:58+00:00","dateModified":"2014-06-04T19:02:58+00:00","author":{"@id":"https:\/\/finaldie.com\/blog\/#\/schema\/person\/2d4c840d6e8e197f8ade98af2bd2fab3"},"breadcrumb":{"@id":"https:\/\/finaldie.com\/blog\/refactor-fhash-is-done\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/finaldie.com\/blog\/refactor-fhash-is-done\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/finaldie.com\/blog\/refactor-fhash-is-done\/#primaryimage","url":"https:\/\/www.lucidchart.com\/img\/diagrams-lucidchart.png","contentUrl":"https:\/\/www.lucidchart.com\/img\/diagrams-lucidchart.png"},{"@type":"BreadcrumbList","@id":"https:\/\/finaldie.com\/blog\/refactor-fhash-is-done\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/finaldie.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Refactor fhash is done"}]},{"@type":"WebSite","@id":"https:\/\/finaldie.com\/blog\/#website","url":"https:\/\/finaldie.com\/blog\/","name":"Final Blog","description":"As simple as possible...","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/finaldie.com\/blog\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/finaldie.com\/blog\/#\/schema\/person\/2d4c840d6e8e197f8ade98af2bd2fab3","name":"final","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/finaldie.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/4c720545b79ddb0f23b527e0bbcfd9bc?s=96&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/4c720545b79ddb0f23b527e0bbcfd9bc?s=96&r=g","caption":"final"}}]}},"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/finaldie.com\/blog\/wp-json\/wp\/v2\/posts\/223"}],"collection":[{"href":"https:\/\/finaldie.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/finaldie.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/finaldie.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/finaldie.com\/blog\/wp-json\/wp\/v2\/comments?post=223"}],"version-history":[{"count":16,"href":"https:\/\/finaldie.com\/blog\/wp-json\/wp\/v2\/posts\/223\/revisions"}],"predecessor-version":[{"id":241,"href":"https:\/\/finaldie.com\/blog\/wp-json\/wp\/v2\/posts\/223\/revisions\/241"}],"wp:attachment":[{"href":"https:\/\/finaldie.com\/blog\/wp-json\/wp\/v2\/media?parent=223"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/finaldie.com\/blog\/wp-json\/wp\/v2\/categories?post=223"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/finaldie.com\/blog\/wp-json\/wp\/v2\/tags?post=223"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}