Lazy Static Include
This crate provides lazy_static_include_bytes
and lazy_static_include_str
macros to replace include_bytes
and include_str
macros.
Why should we do that?
Because the original include_bytes
and include_str
macros bring extra data from files into the compiled executable binary file, the time for compiling surges.
High compilation time is detrimental to software development. lazy_static_include_bytes
and lazy_static_include_str
macros can help you lazy load data from files
when you are not using the release profile. In other words, if you are using include_bytes
and include_str
macros, and you think your compilation time is too high to wait,
you can choose to use lazy_static_include_bytes
and lazy_static_include_str
macros.
lazy_static_include_bytes
and lazy_static_include_str
macros include data from files into the compiled executable binary file only when you are using the release profile.
Be careful when you distribute your program.
The paths used for lazy_static_include_bytes
and lazy_static_include_str
are relative to CARGO_MANIFEST_DIR.
Examples
extern crate lazy_static_include;
extern crate lazy_static;
lazy_static_include_str!;
lazy_static_include_str!;
assert_eq!;
assert_eq!;
extern crate lazy_static_include;
extern crate lazy_static;
lazy_static_include_bytes!;
assert_eq!;
assert_eq!;
You should notice that the struct created from lazy_static_include_bytes
and lazy_static_include_str
macros isn't equal to &'static [u8]
or &'static str
.
If you want to get an exact &'static [u8]
or &'static str
reference, you need to dereference the struct.
extern crate lazy_static_include;
extern crate lazy_static;
lazy_static_include_bytes!;
let data: &'static = *TEST;
If you include str and bytes from multiple files, after dereferencing the struct, you will get a Vec<&'static [u8]>
or a Vec<&'static str>
.
In order to not move out of borrowed content, use &* to get the reference of that Vec
.
extern crate lazy_static_include;
extern crate lazy_static;
lazy_static_include_str!;
let v: & = &*TEST;
Include Array
There is a special macro lazy_static_include_array
which can include arrays from files.
The array is fixed sized and can be one of these following types: bool
, char
, u8
, u16
, u32
, u64
, u128
, i8
, i16
, i32
, i64
, i128
, f32
, f64
, &'static str
.
Also, the lazy_static_include_array
macro includes data from files into the compiled executable binary file only when you are using the release profile.
Be careful when you distribute your program.
extern crate lazy_static_include;
extern crate lazy_static;
lazy_static_include_array!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
extern crate lazy_static_include;
extern crate lazy_static;
lazy_static_include_array!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
extern crate lazy_static_include;
extern crate lazy_static;
lazy_static_include_array!;
assert_eq!;
assert_eq!;
assert_eq!;
Benchmark
Using static mechanisms makes your program faster. See my benchmark result below (Intel i7-6700HQ, ran on 2018/09/10):
running 9 tests
test include_array_lazy_static ... bench: 44 ns/iter (+/- 2)
test include_array_native_static ... bench: 44 ns/iter (+/- 2)
test include_array_no_static ... bench: 8,470 ns/iter (+/- 568)
test include_bytes_lazy_static ... bench: 473 ns/iter (+/- 84)
test include_bytes_native_static ... bench: 482 ns/iter (+/- 30)
test include_bytes_no_static ... bench: 7,247 ns/iter (+/- 1,183)
test include_str_lazy_static ... bench: 963 ns/iter (+/- 85)
test include_str_native_static ... bench: 970 ns/iter (+/- 76)
test include_str_no_static ... bench: 8,338 ns/iter (+/- 556)
This benchmark program is in tests/benchmark.rs
.
When using the release profile, the performance of lazy_static_include_*
is very close to include_*
. That means you don't need to worry about the overhead, but just enjoy the faster compilation time.
Crates.io
https://crates.io/crates/lazy-static-include
Documentation
https://docs.rs/lazy-static-include