Skip to content

Commit ce19e0d

Browse files
authored
improve code size (#28)
1 parent 1bd4cd6 commit ce19e0d

File tree

4 files changed

+48
-37
lines changed

4 files changed

+48
-37
lines changed

generate-data/src/main.rs

Lines changed: 44 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,6 @@ fn build_caniuse_global() -> Result<()> {
163163
fs::write(
164164
format!("{OUT_DIR}/caniuse-browsers.rs"),
165165
{
166-
let map_cap = data.agents.len();
167166
let browser_stat = data.agents.iter().map(|(name, agent)| {
168167
let detail = agent.version_list.iter().map(|version| {
169168
let ver = &version.version;
@@ -182,16 +181,14 @@ fn build_caniuse_global() -> Result<()> {
182181
}
183182
});
184183
quote! {
185-
map.insert(#name, BrowserStat {
184+
(#name, BrowserStat {
186185
name: #name,
187186
version_list: vec![#(#detail),*],
188-
});
187+
})
189188
}
190189
});
191190
quote! {{
192-
let mut map = AHashMap::with_capacity(#map_cap);
193-
#(#browser_stat)*
194-
map
191+
AHashMap::from([ #( #browser_stat ),* ])
195192
}}
196193
}
197194
.to_string(),
@@ -216,7 +213,7 @@ fn build_caniuse_global() -> Result<()> {
216213
fs::write(
217214
format!("{OUT_DIR}/caniuse-global-usage.rs"),
218215
quote! {
219-
vec![#(#push_usage),*]
216+
&[#(#push_usage),*]
220217
}
221218
.to_string(),
222219
)?;
@@ -254,7 +251,9 @@ fn build_caniuse_global() -> Result<()> {
254251
)?,
255252
)?;
256253
}
257-
let features = data.data.keys().collect::<Vec<_>>();
254+
let mut features = data.data.keys().collect::<Vec<_>>();
255+
features.sort();
256+
let features_len = features.len();
258257
let tokens = quote! {{
259258
use ahash::AHashMap;
260259
use indexmap::IndexMap;
@@ -265,19 +264,24 @@ fn build_caniuse_global() -> Result<()> {
265264
type Stat = LazyLock<AHashMap<&'static str, IndexMap<&'static str, u8>>>;
266265
type Json = AHashMap::<u8, IndexMap<&'static str, u8>>;
267266

268-
match name {
269-
#( #features => {
270-
static STAT: Stat = LazyLock::new(|| {
271-
from_str::<Json>(include_str!(concat!("features/", #features, ".json")))
272-
.unwrap()
273-
.into_iter()
274-
.map(|(browser, versions)| (decode_browser_name(browser), versions))
275-
.collect()
276-
});
277-
Some(&*STAT)
278-
}, )*
279-
_ => None,
267+
#[inline(never)]
268+
fn stat(data: &'static str) -> AHashMap<&'static str, IndexMap<&'static str, u8>> {
269+
from_str::<Json>(data)
270+
.unwrap()
271+
.into_iter()
272+
.map(|(browser, versions)| (decode_browser_name(browser), versions))
273+
.collect()
280274
}
275+
276+
static FEATURES: &[&str] = &[
277+
#( #features ),*
278+
];
279+
static STATS: [Stat; #features_len] = [
280+
#( LazyLock::new(|| stat(include_str!(concat!("features/", #features, ".json")))) ),*
281+
];
282+
283+
let idx = FEATURES.binary_search(&name).ok()?;
284+
STATS.get(idx).map(|v| &**v)
281285
}};
282286
fs::write(
283287
format!("{OUT_DIR}/caniuse-feature-matching.rs"),
@@ -332,7 +336,7 @@ fn build_caniuse_region() -> Result<()> {
332336
serde_json::to_string(&usage)?,
333337
)?;
334338
}
335-
let regions = files
339+
let mut regions = files
336340
.iter()
337341
.map(|entry| {
338342
entry
@@ -344,6 +348,8 @@ fn build_caniuse_region() -> Result<()> {
344348
.unwrap()
345349
})
346350
.collect::<Vec<_>>();
351+
regions.sort();
352+
let regions_len = regions.len();
347353
let tokens = quote! {{
348354
use serde_json::from_str;
349355
use std::sync::LazyLock;
@@ -352,19 +358,24 @@ fn build_caniuse_region() -> Result<()> {
352358
type Usage = LazyLock<Vec<(&'static str, &'static str, f32)>>;
353359
type Json = Vec<(u8, &'static str, f32)>;
354360

355-
match region {
356-
#( #regions => {
357-
static USAGE: Usage = LazyLock::new(|| {
358-
from_str::<Json>(include_str!(concat!("region/", #regions, ".json")))
359-
.unwrap()
360-
.into_iter()
361-
.map(|(browser, version, usage)| (decode_browser_name(browser), version, usage))
362-
.collect()
363-
});
364-
Some(&*USAGE)
365-
}, )*
366-
_ => None,
361+
#[inline(never)]
362+
fn usage(data: &'static str) -> Vec<(&'static str, &'static str, f32)> {
363+
from_str::<Json>(data)
364+
.unwrap()
365+
.into_iter()
366+
.map(|(browser, version, usage)| (decode_browser_name(browser), version, usage))
367+
.collect()
367368
}
369+
370+
static REGIONS: &[&str] = &[
371+
#( #regions ),*
372+
];
373+
static USAGES: [Usage; #regions_len] = [
374+
#( LazyLock::new(|| usage(include_str!(concat!("region/", #regions, ".json")))) ),*
375+
];
376+
377+
let idx = REGIONS.binary_search(&region).ok()?;
378+
USAGES.get(idx).map(|v| &**v)
368379
}};
369380
fs::write(
370381
format!("{OUT_DIR}/caniuse-region-matching.rs"),

src/data/caniuse.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ pub type CaniuseData = AHashMap<&'static str, BrowserStat>;
2525
pub static CANIUSE_BROWSERS: LazyLock<CaniuseData> =
2626
LazyLock::new(|| include!("../generated/caniuse-browsers.rs"));
2727

28-
pub static CANIUSE_GLOBAL_USAGE: LazyLock<Vec<(&'static str, &'static str, f32)>> =
29-
LazyLock::new(|| include!("../generated/caniuse-global-usage.rs"));
28+
pub static CANIUSE_GLOBAL_USAGE: &[(&'static str, &'static str, f32)] =
29+
include!("../generated/caniuse-global-usage.rs");
3030

3131
pub static BROWSER_VERSION_ALIASES: LazyLock<
3232
AHashMap<&'static str, AHashMap<&'static str, &'static str>>,

vendor/caniuse

Submodule caniuse updated 814 files

0 commit comments

Comments
 (0)