Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Seperate out icon crate #354

Merged
merged 10 commits into from
Mar 21, 2020
Merged

Seperate out icon crate #354

merged 10 commits into from
Mar 21, 2020

Conversation

liuchengxu
Copy link
Owner

To make the optimization of #353 easier.

To make the optimization of 353 easier.
@liuchengxu liuchengxu merged commit 3ee59c5 into master Mar 21, 2020
@delete-merged-branch delete-merged-branch bot deleted the split-out-icon-crate branch March 21, 2020 05:21
@@ -0,0 +1 @@
lazy_static! { pub static ref EXACTMATCH_MAP: HashMap<&'static str, &'static str> = [("ai", ""),("awk", ""),("bash", ""),("bat", ""),("bin", ""),("bmp", ""),("c", ""),("cc", ""),("cfg", ""),("clj", ""),("cljc", ""),("cljs", ""),("coffee", ""),("conf", ""),("cp", ""),("cpp", ""),("csh", ""),("css", ""),("d", ""),("dart", ""),("db", ""),("diff", ""),("dump", ""),("dylib", ""),("edn", ""),("eex", ""),("ejs", ""),("erl", ""),("ex", ""),("exs", ""),("fish", ""),("fs", ""),("fsi", ""),("fsx", ""),("gif", ""),("go", ""),("gz", ""),("h", ""),("hbs", ""),("hpp", ""),("hrl", ""),("hs", ""),("htm", ""),("html", ""),("ico", ""),("ini", ""),("java", ""),("jl", ""),("jpeg", ""),("jpg", ""),("js", ""),("json", ""),("jsx", ""),("ksh", ""),("less", ""),("lhs", ""),("lock", ""),("log", ""),("lua", ""),("markdown", ""),("md", ""),("ml", "λ"),("mli", "λ"),("mustache", ""),("php", ""),("pl", ""),("plist", "况"),("pm", ""),("png", ""),("pp", ""),("ps1", ""),("psb", ""),("psd", ""),("py", ""),("pyc", ""),("pyd", ""),("pyo", ""),("rb", ""),("rlib", ""),("rmd", ""),("rmeta", ""),("rs", ""),("rss", ""),("sass", ""),("scala", ""),("scss", ""),("sh", ""),("slim", ""),("sln", ""),("so", ""),("sql", ""),("styl", ""),("suo", ""),("swift", ""),("t", ""),("timestamp", "﨟"),("toml", ""),("ts", ""),("tsx", ""),("twig", ""),("txt", ""),("vim", ""),("vimrc", ""),("vue", "﵂"),("xcplayground", ""),("xul", ""),("yaml", ""),("yml", ""),("zip", ""),("zsh", "")].iter().copied().collect(); pub static ref EXTENSION_MAP: HashMap<&'static str, &'static str> = [("ai", ""),("awk", ""),("bash", ""),("bat", ""),("bin", ""),("bmp", ""),("c", ""),("cc", ""),("cfg", ""),("clj", ""),("cljc", ""),("cljs", ""),("coffee", ""),("conf", ""),("cp", ""),("cpp", ""),("csh", ""),("css", ""),("d", ""),("dart", ""),("db", ""),("diff", ""),("dump", ""),("dylib", ""),("edn", ""),("eex", ""),("ejs", ""),("erl", ""),("ex", ""),("exs", ""),("fish", ""),("fs", ""),("fsi", ""),("fsx", ""),("gif", ""),("go", ""),("gz", ""),("h", ""),("hbs", ""),("hpp", ""),("hrl", ""),("hs", ""),("htm", ""),("html", ""),("ico", ""),("ini", ""),("java", ""),("jl", ""),("jpeg", ""),("jpg", ""),("js", ""),("json", ""),("jsx", ""),("ksh", ""),("less", ""),("lhs", ""),("lock", ""),("log", ""),("lua", ""),("markdown", ""),("md", ""),("ml", "λ"),("mli", "λ"),("mustache", ""),("php", ""),("pl", ""),("plist", "况"),("pm", ""),("png", ""),("pp", ""),("ps1", ""),("psb", ""),("psd", ""),("py", ""),("pyc", ""),("pyd", ""),("pyo", ""),("rb", ""),("rlib", ""),("rmd", ""),("rmeta", ""),("rs", ""),("rss", ""),("sass", ""),("scala", ""),("scss", ""),("sh", ""),("slim", ""),("sln", ""),("so", ""),("sql", ""),("styl", ""),("suo", ""),("swift", ""),("t", ""),("timestamp", "﨟"),("toml", ""),("ts", ""),("tsx", ""),("twig", ""),("txt", ""),("vim", ""),("vimrc", ""),("vue", "﵂"),("xcplayground", ""),("xul", ""),("yaml", ""),("yml", ""),("zip", ""),("zsh", "")].iter().copied().collect(); }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you keep writing lazy_static hashmap instead of const table? Yeah, map has some indexing hacks, but you can do even better hacks with const tables.

There's no magic in lazy statics and they are good for very few things actually:

  1. You need a mutable heap-allocated static. Like static mut MAP: HashMap<_, _> = ...; /* other code */ unsafe { MAP.insert(item); }
  2. You need a big allocated thing, that should be static, but is not always necessary for each program's run. Like static INNER_HEAP: Vec<u8> = Vec::with_capacity(1_000_000);
  3. You need a big computation that involves heap-allocation, result should be static, and it is not always necessary for each program's run.

That's all. Any other thing could be done with const tables. For example, right now you can do better than hashmap with simple

let icon_index = match CONST_TABLE_EXTENSION.binary_search(extension) {
    Ok(index) => index,
    Err(_) => return DEFAULT_ICON,
};
return EXTENSION_ICONS[icon_index];

if you would break this tuple table into two separate tables, because now they are sorted. And much better than hashmap, if you'd implement intermediate indexing by first letter.

And the last thing I wanted to mention: [char; N] takes much less space (2-5 times less, based on pointer length) than [&str; N], and all you icons are a single char right now.

@liuchengxu

Copy link
Owner Author

@liuchengxu liuchengxu Mar 21, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ImmemorConsultrixContrarie Thanks for the insights. I merely copied from the src/icon.rs without improving such Rust code as I'm not familiar with these hacks, but I do agree with you that it can get much faster. I made this PR so you don't have to duplicate them in #353. And if you are intersted in contining to boost the icon code, this PR can make it easier. I have read the comment you wrote about the icons in #353, I think it's worthy of another standalone PR.

@liuchengxu liuchengxu mentioned this pull request Mar 22, 2020
liuchengxu added a commit that referenced this pull request Mar 23, 2020
liuchengxu added a commit that referenced this pull request Mar 23, 2020
* Use const table instead static HashMap for the icons

Apply the suggestion from #354 (comment)

* .
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants