Skip to content

Commit

Permalink
Don't use serde_json to read the list of named entities for codegen
Browse files Browse the repository at this point in the history
This can have the effect of taking out serde_derive from the critical
path of crates that only use markup5ever.  In the case of librsvg,
this reduces build time in release mode by 30 seconds.
  • Loading branch information
federicomenaquintero committed Oct 30, 2019
1 parent 07bd567 commit 3afd8d6
Show file tree
Hide file tree
Showing 4 changed files with 2,239 additions and 2,259 deletions.
3 changes: 0 additions & 3 deletions markup5ever/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,3 @@ log = "0.4"
[build-dependencies]
string_cache_codegen = "0.5.1"
phf_codegen = "0.8"
serde = "1.0"
serde_derive = "1.0"
serde_json = "1.0"
29 changes: 6 additions & 23 deletions markup5ever/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,15 @@

extern crate phf_codegen;
extern crate string_cache_codegen;
#[macro_use]
extern crate serde_derive;
extern crate serde_json;

use std::collections::HashMap;
use std::env;
use std::fs::File;
use std::io::{BufRead, BufReader, BufWriter, Write};
use std::path::Path;

mod entities;

static NAMESPACES: &'static [(&'static str, &'static str)] = &[
("", ""),
("*", "*"),
Expand All @@ -34,10 +33,7 @@ fn main() {
let generated = Path::new(&env::var("OUT_DIR").unwrap()).join("generated.rs");
let mut generated = BufWriter::new(File::create(&generated).unwrap());

let manifest_dir = env::var("CARGO_MANIFEST_DIR").unwrap();

named_entities_to_phf(
&Path::new(&manifest_dir).join("data").join("entities.json"),
&Path::new(&env::var("OUT_DIR").unwrap()).join("named_entities.rs"),
);

Expand Down Expand Up @@ -89,27 +85,14 @@ fn main() {
writeln!(generated, "}}").unwrap();
}

fn named_entities_to_phf(from: &Path, to: &Path) {
// A struct matching the entries in entities.json.
#[derive(Deserialize, Debug)]
struct CharRef {
codepoints: Vec<u32>,
//characters: String, // Present in the file but we don't need it
}

let entities: HashMap<String, CharRef> =
serde_json::from_reader(&mut File::open(from).unwrap()).unwrap();
let mut entities: HashMap<&str, (u32, u32)> = entities
fn named_entities_to_phf(to: &Path) {
let mut entities: HashMap<&str, (u32, u32)> = entities::NAMED_ENTITIES
.iter()
.map(|(name, char_ref)| {
.map(|(name, cp1, cp2)| {
assert!(name.starts_with("&"));
assert!(char_ref.codepoints.len() <= 2);
(
&name[1..],
(
char_ref.codepoints[0],
*char_ref.codepoints.get(1).unwrap_or(&0),
),
(*cp1, *cp2),
)
})
.collect();
Expand Down
Loading

0 comments on commit 3afd8d6

Please sign in to comment.