title: Duplicate literals and hard-coding
body: <p>I see the follow pattern occurring quite frequently:</p>
  
  <pre><code> b-&gt;last = ngx_cpymem(b-&gt;last, "&lt;/pre&gt;&lt;hr&gt;", sizeof("&lt;/pre&gt;&lt;hr&gt;") - 1);
  </code></pre>
  
  <p>Notice that the literal string is used twice. The extract is from the nginx source-base. </p>
  
  <p>The compiler should be able to merge these literals when it is encountered within the compilation unit. </p>
  
  <p>My questions are:</p>
  
  <ol>
  <li>Do the commercial-grade compilers(VC++, GCC, LLVM/Clang) remove this redundancy when encountered within a compilation unit ? </li>
  <li>Does the (static) linker remove such redundancies when linking object files. </li>
  <li>if 2 applies would this optimization occur during dynamic linking ? </li>
  <li>If 1 and 2 apply, do they apply to all literals.</li>
  </ol>
  
  <p>These questions are important because it allows a programmer to be verbose without losing efficiency -- i.e., think about enormous static data models being hard-wired into a program (for example the rules of a Decision Support System used in some low-level scenario).</p>
  
  <p><strong>Edit</strong></p>
  
  <p>2 points / clarifications</p>
  
  <ol>
  <li><p>The code above is written by a recognised "master" programmer. The guy single handedly wrote nginx. </p></li>
  <li><p>I have not asked which of the possible mechanisms of literal hard-coding is better. Therefore don't go off-topic.</p></li>
  </ol>
  
  <p><strong>Edit 2</strong></p>
  
  <p>My original example was quite contrived and restrictive. The following snippet shows the usage of string literals being embedded into internal hard-coded knowledge. The first snippet is meant for the config parser telling it what enum values to set for which string, and the second to be used more generally as a string in the program. Personally I am happy with this as long as the compiler uses one copy of the string literal, and since the elements are static, they don't enter the global symbol tables.</p>
  
  <pre><code>static ngx_conf_bitmask_t  ngx_http_gzip_proxied_mask[] = {
     { ngx_string("off"), NGX_HTTP_GZIP_PROXIED_OFF },
     { ngx_string("expired"), NGX_HTTP_GZIP_PROXIED_EXPIRED },
     { ngx_string("no-cache"), NGX_HTTP_GZIP_PROXIED_NO_CACHE },
     { ngx_string("no-store"), NGX_HTTP_GZIP_PROXIED_NO_STORE },
     { ngx_string("private"), NGX_HTTP_GZIP_PROXIED_PRIVATE },
     { ngx_string("no_last_modified"), NGX_HTTP_GZIP_PROXIED_NO_LM },
     { ngx_string("no_etag"), NGX_HTTP_GZIP_PROXIED_NO_ETAG },
     { ngx_string("auth"), NGX_HTTP_GZIP_PROXIED_AUTH },
     { ngx_string("any"), NGX_HTTP_GZIP_PROXIED_ANY },
     { ngx_null_string, 0 }
  };
  </code></pre>
  
  <p>followed closely by:</p>
  
  <pre><code>static ngx_str_t  ngx_http_gzip_no_cache = ngx_string("no-cache");
  static ngx_str_t  ngx_http_gzip_no_store = ngx_string("no-store");
  static ngx_str_t  ngx_http_gzip_private = ngx_string("private");
  </code></pre>
  
  <p>To those that stayed on topic, bravo !</p>