Skip to content

Commit 3afd8d6

Browse files
Don't use serde_json to read the list of named entities for codegen
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.
1 parent 07bd567 commit 3afd8d6

File tree

4 files changed

+2239
-2259
lines changed

4 files changed

+2239
-2259
lines changed

markup5ever/Cargo.toml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,3 @@ log = "0.4"
2222
[build-dependencies]
2323
string_cache_codegen = "0.5.1"
2424
phf_codegen = "0.8"
25-
serde = "1.0"
26-
serde_derive = "1.0"
27-
serde_json = "1.0"

markup5ever/build.rs

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,15 @@
99

1010
extern crate phf_codegen;
1111
extern crate string_cache_codegen;
12-
#[macro_use]
13-
extern crate serde_derive;
14-
extern crate serde_json;
1512

1613
use std::collections::HashMap;
1714
use std::env;
1815
use std::fs::File;
1916
use std::io::{BufRead, BufReader, BufWriter, Write};
2017
use std::path::Path;
2118

19+
mod entities;
20+
2221
static NAMESPACES: &'static [(&'static str, &'static str)] = &[
2322
("", ""),
2423
("*", "*"),
@@ -34,10 +33,7 @@ fn main() {
3433
let generated = Path::new(&env::var("OUT_DIR").unwrap()).join("generated.rs");
3534
let mut generated = BufWriter::new(File::create(&generated).unwrap());
3635

37-
let manifest_dir = env::var("CARGO_MANIFEST_DIR").unwrap();
38-
3936
named_entities_to_phf(
40-
&Path::new(&manifest_dir).join("data").join("entities.json"),
4137
&Path::new(&env::var("OUT_DIR").unwrap()).join("named_entities.rs"),
4238
);
4339

@@ -89,27 +85,14 @@ fn main() {
8985
writeln!(generated, "}}").unwrap();
9086
}
9187

92-
fn named_entities_to_phf(from: &Path, to: &Path) {
93-
// A struct matching the entries in entities.json.
94-
#[derive(Deserialize, Debug)]
95-
struct CharRef {
96-
codepoints: Vec<u32>,
97-
//characters: String, // Present in the file but we don't need it
98-
}
99-
100-
let entities: HashMap<String, CharRef> =
101-
serde_json::from_reader(&mut File::open(from).unwrap()).unwrap();
102-
let mut entities: HashMap<&str, (u32, u32)> = entities
88+
fn named_entities_to_phf(to: &Path) {
89+
let mut entities: HashMap<&str, (u32, u32)> = entities::NAMED_ENTITIES
10390
.iter()
104-
.map(|(name, char_ref)| {
91+
.map(|(name, cp1, cp2)| {
10592
assert!(name.starts_with("&"));
106-
assert!(char_ref.codepoints.len() <= 2);
10793
(
10894
&name[1..],
109-
(
110-
char_ref.codepoints[0],
111-
*char_ref.codepoints.get(1).unwrap_or(&0),
112-
),
95+
(*cp1, *cp2),
11396
)
11497
})
11598
.collect();

0 commit comments

Comments
 (0)