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

Do not compress the outer parts of a SyntaxSet #398

Merged
merged 3 commits into from
Dec 23, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Binary file modified assets/default_newlines.packdump
Binary file not shown.
Binary file modified assets/default_nonewlines.packdump
Binary file not shown.
2 changes: 1 addition & 1 deletion benches/loading.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ fn bench_link_syntaxes(b: &mut Bencher) {

fn bench_from_dump_file(b: &mut Bencher) {
b.iter(|| {
let _: SyntaxSet = syntect::dumps::from_dump_file("assets/default_newlines.packdump").unwrap();
let _: SyntaxSet = syntect::dumps::from_uncompressed_dump_file("assets/default_newlines.packdump").unwrap();
})
}

Expand Down
4 changes: 2 additions & 2 deletions examples/gendata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ fn main() {
builder.add_plain_text_syntax();
builder.add_from_folder(package_dir, true).unwrap();
let ss = builder.build();
dump_to_file(&ss, packpath_newlines).unwrap();
dump_to_uncompressed_file(&ss, packpath_newlines).unwrap();

let mut builder_nonewlines = SyntaxSetBuilder::new();
builder_nonewlines.add_plain_text_syntax();
Expand All @@ -46,7 +46,7 @@ fn main() {
}

let ss_nonewlines = builder_nonewlines.build();
dump_to_file(&ss_nonewlines, packpath_nonewlines).unwrap();
dump_to_uncompressed_file(&ss_nonewlines, packpath_nonewlines).unwrap();

#[cfg(feature = "metadata")]
{
Expand Down
62 changes: 54 additions & 8 deletions src/dumps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ use serde::de::DeserializeOwned;
/// The writer is encoded with the `bincode` crate and compressed with `flate2`.
#[cfg(any(feature = "dump-create", feature = "dump-create-rs"))]
pub fn dump_to_writer<T: Serialize, W: Write>(to_dump: &T, output: W) -> Result<()> {
let mut encoder = ZlibEncoder::new(output, Compression::best());
serialize_into(&mut encoder, to_dump)
serialize_to_writer_impl(to_dump, output, true)
}

/// Dumps an object to a binary array in the same format as [`dump_to_writer`]
Expand Down Expand Up @@ -71,8 +70,7 @@ pub fn dump_to_file<T: Serialize, P: AsRef<Path>>(o: &T, path: P) -> Result<()>
/// A helper function for decoding and decompressing data from a reader
#[cfg(any(feature = "dump-load", feature = "dump-load-rs"))]
pub fn from_reader<T: DeserializeOwned, R: BufRead>(input: R) -> Result<T> {
let mut decoder = ZlibDecoder::new(input);
deserialize_from(&mut decoder)
deserialize_from_reader_impl(input, true)
}

/// Returns a fully loaded syntax set from a binary dump.
Expand All @@ -90,6 +88,54 @@ pub fn from_dump_file<T: DeserializeOwned, P: AsRef<Path>>(path: P) -> Result<T>
from_reader(&contents[..])
}

/// To be used when serializing a [`SyntaxSet`] to a file. A [`SyntaxSet`]
/// itself shall not be compressed, because the data for its lazy-loaded
/// syntaxes are already compressed. Compressing another time just results in
/// bad performance.
#[cfg(any(feature = "dump-create", feature = "dump-create-rs"))]
pub fn dump_to_uncompressed_file<T: Serialize, P: AsRef<Path>>(o: &T, path: P) -> Result<()> {
let out = BufWriter::new(File::create(path)?);
serialize_to_writer_impl(o, out, false)
}

/// To be used when deserializing a [`SyntaxSet`] that was previously written to
/// file using [dump_to_uncompressed_file].
#[cfg(any(feature = "dump-load", feature = "dump-load-rs"))]
pub fn from_uncompressed_dump_file<T: DeserializeOwned, P: AsRef<Path>>(path: P) -> Result<T> {
let contents = std::fs::read(path)?;
deserialize_from_reader_impl(&contents[..], false)
}

/// To be used when deserializing a [`SyntaxSet`] from raw data, for example
/// data that has been embedded in your own binary with the [`include_bytes!`]
/// macro.
#[cfg(any(feature = "dump-load", feature = "dump-load-rs"))]
fn from_uncompressed_data<T: DeserializeOwned>(v: &[u8]) -> Result<T> {
Copy link
Collaborator Author

@Enselic Enselic Nov 25, 2021

Choose a reason for hiding this comment

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

In the spirit of #98 I chose to return a Result<T> here instead of T as from_binary. I also chose to call it _data instead of _binary since I think _data is clearer. But I am of course willing to make any changes that my reviewer(s) request.

deserialize_from_reader_impl(v, false)
}

/// Private low level helper function used to implement the public API.
#[cfg(any(feature = "dump-create", feature = "dump-create-rs"))]
fn serialize_to_writer_impl<T: Serialize, W: Write>(to_dump: &T, output: W, use_compression: bool) -> Result<()> {
if use_compression {
let mut encoder = ZlibEncoder::new(output, Compression::best());
serialize_into(&mut encoder, to_dump)
} else {
serialize_into(output, to_dump)
}
}

/// Private low level helper function used to implement the public API.
#[cfg(any(feature = "dump-load", feature = "dump-load-rs"))]
fn deserialize_from_reader_impl<T: DeserializeOwned, R: BufRead>(input: R, use_compression: bool) -> Result<T> {
if use_compression {
let mut decoder = ZlibDecoder::new(input);
deserialize_from(&mut decoder)
} else {
deserialize_from(input)
}
}

#[cfg(all(feature = "parsing", feature = "assets", any(feature = "dump-load", feature = "dump-load-rs")))]
impl SyntaxSet {
/// Instantiates a new syntax set from a binary dump of Sublime Text's default open source
Expand All @@ -115,14 +161,14 @@ impl SyntaxSet {

#[cfg(feature = "metadata")]
{
let mut ps: SyntaxSet = from_binary(include_bytes!("../assets/default_nonewlines.packdump"));
let mut ps: SyntaxSet = from_uncompressed_data(include_bytes!("../assets/default_nonewlines.packdump")).unwrap();
let metadata = from_binary(include_bytes!("../assets/default_metadata.packdump"));
ps.metadata = metadata;
ps
}
#[cfg(not(feature = "metadata"))]
{
from_binary(include_bytes!("../assets/default_nonewlines.packdump"))
from_uncompressed_data(include_bytes!("../assets/default_nonewlines.packdump")).unwrap()
}
}

Expand All @@ -136,14 +182,14 @@ impl SyntaxSet {

#[cfg(feature = "metadata")]
{
let mut ps: SyntaxSet = from_binary(include_bytes!("../assets/default_newlines.packdump"));
let mut ps: SyntaxSet = from_uncompressed_data(include_bytes!("../assets/default_newlines.packdump")).unwrap();
let metadata = from_binary(include_bytes!("../assets/default_metadata.packdump"));
ps.metadata = metadata;
ps
}
#[cfg(not(feature = "metadata"))]
{
from_binary(include_bytes!("../assets/default_newlines.packdump"))
from_uncompressed_data(include_bytes!("../assets/default_newlines.packdump")).unwrap()
}
}
}
Expand Down