title: Why does generating a static HashMap with ~30K entries at compile time consume so many resources?
body: <p>I'm trying to write a <code>build.rs</code> script that creates an up-to-date <code>HashMap</code> that maps the first 6 characters of a MAC address with its corresponding vendor.</p>
  <p>It has 29231 key-value pairs which causes <code>cargo check</code> to spend more than 7 minutes on my source code. Before this, it was less than 20 seconds. It also uses <em>all</em> 8GB of the RAM available on my laptop and I cannot use it during those 7-8 minutes.</p>
  <p>I think this is either a <code>rustc</code>/<code>cargo</code> bug, or I am doing something wrong, and I'm pretty sure is the latter. What is the correct way of generating code like this?</p>
  <p><strong>main.rs</strong></p>
  <pre class="lang-rust prettyprint-override"><code>use std::collections::{HashMap, HashSet};
  use rustc_hash::{FxHashMap, FxHashSet, FxHasher};
  type CustomHasher = BuildHasherDefault&lt;FxHasher&gt;;
  include!(concat!(env!(&quot;OUT_DIR&quot;), &quot;/map_oui.rs&quot;));
  </code></pre>
  <p><strong>map_oui.rs</strong></p>
  <pre class="lang-rust prettyprint-override"><code>#[rustfmt::skip]
  lazy_static! {
      static ref MAP_MACS: FxHashMap&lt;&amp;'static [u8; 6], &amp;'static str&gt; = {
      let mut map_macs = HashMap::with_capacity_and_hasher(29231, CustomHasher::default());
      map_macs.insert(b&quot;002272&quot;, &quot;American Micro-Fuel Device Corp.&quot;);
      map_macs.insert(b&quot;00D0EF&quot;, &quot;IGT&quot;);
  //...
  </code></pre>
  <p><strong>build.rs</strong></p>
  <pre class="lang-rust prettyprint-override"><code>use std::env;
  use std::fs::File;
  use std::io::prelude::*;
  use std::io::{BufReader, BufWriter};
  use std::path::Path;
  
  fn main() {
      let out_dir = env::var_os(&quot;OUT_DIR&quot;).unwrap();
      let dest_path = Path::new(&amp;out_dir).join(&quot;map_oui.rs&quot;);
      let handle = File::create(dest_path).unwrap();
      let mut writer = BufWriter::new(handle);
      let response = ureq::get(&quot;http://standards-oui.ieee.org/oui.txt&quot;)
          .call()
          .expect(&quot;Conection Error&quot;);
      let mut reader = BufReader::new(response.into_reader());
      let mut line = Vec::new();
  
      writer
          .write(
              b&quot;#[rustfmt::skip]
  lazy_static! {
      static ref MAP_MACS: FxHashMap&lt;&amp;'static [u8; 6], &amp;'static str&gt; = {
      let mut map_macs = HashMap::with_capacity_and_hasher(29231, CustomHasher::default());\n&quot;,
          )
          .unwrap();
      loop {
          match reader.read_until('\n' as u8, &amp;mut line) {
              Ok(bytes_read) =&gt; {
                  if bytes_read == 0 {
                      break;
                  }
                  if line.get(12..=18).map_or(false, |s| s == b&quot;base 16&quot;) {
                      let mac_oui = String::from_utf8_lossy(&amp;line[0..6]);
                      let vendor = String::from_utf8_lossy(&amp;line[22..]);
                      writer.write(b&quot;    map_macs.insert(b\&quot;&quot;).unwrap();
                      writer.write(mac_oui.as_bytes()).unwrap();
                      writer.write(b&quot;\&quot;, \&quot;&quot;).unwrap();
                      writer.write(vendor.trim().as_bytes()).unwrap();
                      writer.write(b&quot;\&quot;);\n&quot;).unwrap();
                  }
                  line.clear();
              }
              Err(_) =&gt; (),
          }
      }
      writer
          .write(
              b&quot;    map_macs
      };
  }
  &quot;,
          )
          .unwrap();
      writer.flush().unwrap();
      println!(&quot;cargo:rerun-if-changed=build.rs&quot;);
  }
  </code></pre>