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

Considering an alternative to pynliner #114

Closed
Stranger6667 opened this issue Jan 8, 2022 · 4 comments
Closed

Considering an alternative to pynliner #114

Stranger6667 opened this issue Jan 8, 2022 · 4 comments

Comments

@Stranger6667
Copy link

Hello!

I noticed that rssant uses pynliner where the last commit was in Jan 2017, so pynliner, unfortunately, looks abandoned. Therefore I'd like to propose replacing it with css-inline (that I authored) or some other maintained alternative (e.g. premailer).

css-inline is significantly faster - a sample HTML from templates/email/reset_password.html is processed locally in ~140µs vs. ~28ms with pynliner vs. ~2ms with premailer. Though, it adds a dependency on the Rust toolchain for some users (where wheels aren't provided, like arm64) and generally supports fewer architectures than lxml-based libraries (pynliner & premailer). However, I don't know whether it is a concern. The replacement itself would look like this:

from css_inline import inline

inline(html, remove_style_tags=True)

+ it might require changing the order of rendering for mako templates (pynliner doesn't transform Mako's syntax as css-inline does)

Let me know what do you think :) If you'll be ok with replacing pynliner, I'd be happy to submit a PR

@guyskk
Copy link
Contributor

guyskk commented Jan 9, 2022

Hi @Stranger6667, thank you for the proposal!

About Performance:

pynliner is really slow and I have ever encountered performance issue when render templates/email/feed_analysis.html.mako. The template contains a very large table, and need inline css for each cell. So the render process is very slow, and the final html is very large, which also cause email app rendering very slow.

My solution is pre inline css on mako template and cache the template, then do mako rendering. So only need inline css once, and reuse the inlined template. FYI: email_template.py

And to avoid too large html, I have to use css classes instead of inline styles.

it might require changing the order of rendering for mako templates (pynliner doesn't transform Mako's syntax as css-inline does)

It's not suitable for my cases to change rendering order, as described above.

About ARM wheel

ARM64 is required for rssant. Not sure whether it's hard to install from source or not. It's possible to build ARM64 wheel from source via docker multi-stage build.

@Stranger6667
Copy link
Author

@guyskk Thank you for providing the context!

Re ARM64 wheel: I added wheels for Python 3.8+ on macOS ARM64 and some more platforms including manylinux aarch64, musllinux aarch64 etc. Hopefully, it satisfies the requirements :) Let me know if it is not sufficient, and rssant needs e.g. Python 3.7 on ARM64

Re performance: Pre-inlining is a neat trick indeed! :) I tried to render that template with some randomly generated data (see below) and post-inlining with pynliner takes ~2.3s vs ~8.2ms with css-inline. Though, with pre-inlining, it becomes much or less irrelevant, as switching to post-inlining will give a net-negative effect on performance in any case :) Probably it is something for me to consider - maybe css-inline may support templated input one day :)

Details on data generation
from hypothesis import provisional as pr, strategies as st, settings, given

...  # import `Feed` from feed_analysis here - locally I copied the relevant definitions

feeds = st.builds(
    Feed, 
    id=st.integers(min_value=1), 
    domain=pr.domains(), 
    status=st.sampled_from(["pending", "updating", "ready", "error", "discard"]), 
    use_proxy=st.booleans(), 
    response_status=st.integers(min_value=200, max_value=500), 
    freeze_level=st.integers(min_value=1, max_value=10)
)

out = []

@given(feeds)
@settings(max_examples=10000)
def generate(x):
     out.append(x)

# calling generate() will add 10000 random `Feed` instances to `out`
# Then generating snapshots from it via `compute_snapshot`, and passing them to `compute_report`

So, effectively it looks like the only way to use css-inline at the moment means a performance hit due to unconditional post-inlining (though, maybe template itself can be re-arranged to support css-inline, but it is too much for this change). premailer also malformed the template :( Let me know if there is anything I can do on my side here, and thank you for consideration :)

@guyskk
Copy link
Contributor

guyskk commented Jan 11, 2022

@Stranger6667 Seems nothing needs to be done for inlining css in rssant project.

If you have interest in high performance project, maybe take a look at this optics science project:

@Stranger6667
Copy link
Author

Thanks, @guyskk ! :)

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

No branches or pull requests

2 participants