-
Notifications
You must be signed in to change notification settings - Fork 86
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
Conversation
To make the optimization of 353 easier.
@@ -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(); } |
There was a problem hiding this comment.
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:
- You need a mutable heap-allocated static. Like
static mut MAP: HashMap<_, _> = ...; /* other code */ unsafe { MAP.insert(item); }
- 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);
- 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.
There was a problem hiding this comment.
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.
Apply the suggestion from #354 (comment)
* Use const table instead static HashMap for the icons Apply the suggestion from #354 (comment) * .
To make the optimization of #353 easier.