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

Improve recognition of text MIME types for compression #360

Merged
merged 4 commits into from
Apr 25, 2024

Conversation

palant
Copy link
Contributor

@palant palant commented Apr 23, 2024

Description

If you look at the list of MIME types potentially produced by the mime_guess package, the list in compression::TEXT_MIME_TYPES isn’t really great for recognizing text MIME types. This implements the following changes:

  • Generic recognition for MIME types starting with text/ or ending in +xml/+json
  • Faster testing of the remaining MIME type list by using a HashSet
  • Recognition no longer confused by charset parameter (a bug I mentioned in Apply the usual post-processing to error responses for consistency #359)
  • Adjusted font MIME types to the ones actually produced by mime_guess and removed the outdated application/x-javascript type

Note that lazy_static isn’t a new dependency, we already have it as a transitive dependency via tracing-subscriber.

How Has This Been Tested?

Functional testing on Linux looks fine, and Rust files (text/x-rust) are now being compressed as well.

Copy link

semanticdiff-com bot commented Apr 23, 2024

Review changes with SemanticDiff.

Analyzed 1 of 3 files.

Overall, the semantic diff is 34% smaller than the GitHub diff.

Filename Status
Cargo.lock Unsupported file format
Cargo.toml Unsupported file format
✔️ src/compression.rs 33.16% smaller

@@ -36,33 +38,19 @@ use crate::{
Result,
};

/// Contains a fixed list of common text-based MIME types in order to apply compression.
pub const TEXT_MIME_TYPES: [&str; 24] = [
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Unfortunately, this was public, so this is a breaking change.

Copy link
Collaborator

@joseluisq joseluisq Apr 25, 2024

Choose a reason for hiding this comment

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

Let's keep the comment open for mentioning this breaking change in the next release.

@joseluisq joseluisq added enhancement New feature or request v2 v2 release labels Apr 24, 2024
@palant
Copy link
Contributor Author

palant commented Apr 24, 2024

Benchmark results show a clear improvement on small text files (going down in the noise on large ones). Seemingly no real change on the directory index (text/html was listed first, so recognizing this MIME type was faster).

Copy link
Collaborator

@joseluisq joseluisq left a comment

Choose a reason for hiding this comment

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

Thanks!

@@ -36,33 +38,19 @@ use crate::{
Result,
};

/// Contains a fixed list of common text-based MIME types in order to apply compression.
pub const TEXT_MIME_TYPES: [&str; 24] = [
Copy link
Collaborator

@joseluisq joseluisq Apr 25, 2024

Choose a reason for hiding this comment

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

Let's keep the comment open for mentioning this breaking change in the next release.

src/compression.rs Show resolved Hide resolved
@joseluisq joseluisq added this to the v2.30.0 milestone Apr 25, 2024
@joseluisq joseluisq merged commit 5a4035f into static-web-server:master Apr 25, 2024
35 checks passed
@palant palant deleted the text-mime-recognition branch April 25, 2024 22:13
@palant
Copy link
Contributor Author

palant commented Apr 27, 2024

This caused a significant performance regression on directory listings for some reason.

@palant
Copy link
Contributor Author

palant commented Apr 27, 2024

Ah, I see – not actually a regression. Previously, we wouldn’t compress directory listings because the original logic wouldn’t recognize text/html; encoding=utf-8 as a text format. And now that we do compress directory listings, performance naturally went down.

@joseluisq
Copy link
Collaborator

Ah, I see – not actually a regression. Previously, we wouldn’t compress directory listings because the original logic wouldn’t recognize text/html; encoding=utf-8 as a text format. And now that we do compress directory listings, performance naturally went down.

It would be expected, but is there a big margin?

@palant
Copy link
Contributor Author

palant commented Apr 27, 2024

It would be expected, but is there a big margin?

Sure, compression takes time (gzip in particular which is what vegeta is testing). So directory listing tests went up by almost 50%.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request v2 v2 release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants