-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbuild.rs
175 lines (142 loc) · 4.66 KB
/
build.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
use std::process::{Command, Stdio};
use std::env;
use std::fs::{self, File};
use std::io::{self, prelude::*, BufWriter};
use std::path::Path;
use quote::quote;
use proc_macro2::{Ident, Span};
use enum_utils_from_str::StrMapFunc;
struct Dataset {
name: String,
values: Vec<String>,
}
impl Dataset {
fn file(&self, method: &str) -> io::Result<BufWriter<File>> {
let dir = env::var("OUT_DIR").unwrap();
let dir = Path::new(&dir);
let name = format!("{}_{}.rs", self.name, method);
File::create(&dir.join(name)).map(BufWriter::new)
}
fn trie(&self) -> io::Result<()> {
let mut w = self.file("trie")?;
let mut t = StrMapFunc::new("_trie", "usize");
let entries = self.values.iter()
.enumerate()
.map(|(i, s)| (&**s, i));
t.entries(entries);
let o = quote!{
pub fn trie(s: &str) -> Option<usize> {
#t
_trie(s.as_bytes())
}
};
write!(&mut w, "{}", o)
}
fn phf(&self) -> io::Result<()> {
let mut w = self.file("phf")?;
write!(&mut w, "static MAP: ::phf::Map<&'static str, usize> = ")?;
let mut map = phf_codegen::Map::new();
for (i, atom) in self.values.iter().enumerate() {
map.entry(atom.as_str(), i.to_string().as_str());
}
map.build(&mut w)?;
writeln!(&mut w, ";")?;
write!(&mut w, "{}", quote!{
pub fn phf(s: &str) -> Option<usize> {
MAP.get(s).cloned()
}
})
}
fn simple_match(&self) -> io::Result<()> {
let mut w = self.file("control")?;
let i = self.values.iter().enumerate().map(|(i, _)| i);
let values = self.values.iter();
let out = quote!{
pub fn control(s: &str) -> Option<usize> {
match s {
#( #values => Some(#i), )*
_ => None
}
}
};
write!(&mut w, "{}", out)
}
fn gperf(&self) -> io::Result<()> {
let name = &self.name;
let mut w = self.file("gperf")?;
// Run gperf
let mut path = env::temp_dir();
path.push("gperf.c");
let f = File::create(&path)?;
let mut child = Command::new("gperf")
.arg("--includes")
.arg("--struct-type")
.arg("--compare-strncmp")
.args(&["--lookup-function-name", name])
.stdin(Stdio::piped())
.stdout(f)
.spawn()
.expect("Failed to start gperf");
{
let stdin = child.stdin.as_mut().unwrap();
writeln!(stdin, "struct discriminant {{ char *name; int discriminant; }};")?;
writeln!(stdin, "%%")?;
for (i, atom) in self.values.iter().enumerate() {
writeln!(stdin, "{}, {}", atom, i)?;
}
}
child.wait().unwrap();
// Compile generated code
cc::Build::new()
.file(&path)
.flag("-Wno-missing-field-initializers")
.compile(name);
// Generate wrapper function
let fname = Ident::new(name, Span::call_site());
let out = quote!{
extern crate libc;
use self::libc::{c_char, c_int, size_t};
#[repr(C)]
struct Discriminant {
name: *const c_char,
discriminant: c_int,
}
#[link(name = #name)]
extern {
fn #fname(s: *const c_char, len: size_t) -> *mut Discriminant;
}
pub fn gperf(s: &str) -> Option<usize> {
let discriminant = unsafe {
#fname(s.as_ptr() as *const c_char, s.len()).as_ref()
};
discriminant.map(|d| {
d.discriminant as usize
})
}
};
write!(w, "{}", out)
}
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut datasets = vec![];
for entry in fs::read_dir("./data")? {
let path = entry?.path();
let name = path.file_stem().unwrap().to_owned().into_string().unwrap();
let f = io::BufReader::new(File::open(path)?);
let values: Result<Vec<String>, _> = f.lines()
.map(|s| s.map(|s| s.trim().to_owned()))
.collect();
let data = Dataset { name, values: values? };
datasets.push(data);
}
for data in &datasets {
data.simple_match()?;
data.trie()?;
data.phf()?;
// gperf fails on large inputs
if data.values.len() < 2000 {
data.gperf()?;
}
}
Ok(())
}