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

Question: define! vs new! #12

Closed
diegopy opened this issue Mar 29, 2021 · 4 comments
Closed

Question: define! vs new! #12

diegopy opened this issue Mar 29, 2021 · 4 comments

Comments

@diegopy
Copy link

diegopy commented Mar 29, 2021

Hi team,

First, thanks for this library. The impressive benchmark result is making me move from Maud to this, although I'm not very experienced with either.
Me question is regarding define! vs new! macro use, like in the fortunes.rs example. I tend to prefer new! here, and have functions wrapping the template, so my question is: Is there any disadvantage (in performance or otherwise) of new! vs define!? Any advice on which one will scale better once the number of templates (and the references between them) scales as the application grows?

Thanks

@ZaneHannanAU
Copy link

new! is ad-hoc, and suitable to be returned from a function (ie: fn doc<Head, Body>(h: Head, b: Body)), while define! creates a structure, suitable for repeated use in a small context.

Well, new for stuff you return, define for stuff you own, I guess.

@utkarshkukreti
Copy link
Owner

Hi @diegopy,

Yes, there's a performance difference - new! uses closures to allow access to outer variables, and the way they're implemented in Rust + the fact that the Render trait uses a generic writer argument means that dynamic dispatch is used for all write operations, unlike define!, which uses static dispatch. The example you linked to was written mainly for benchmarking purpose. Here are its latest results: https://github.com/utkarshkukreti/markup.rs/runs/2213438391?check_suite_focus=true#step:7:58 tldr: define! is almost twice as fast as new!. In practice though, both are really really fast. Even new! renders at 500MB/s on GitHub Actions' server.

I personally use whichever makes more sense in the situation, without considering the performance difference. The primary reason to choose one over the other: new! lets you access variables in outer scope while render! makes it easy to have "named arguments" using structs. You should use whichever results in cleaner code in your situation.

@utkarshkukreti
Copy link
Owner

@ZaneHannanAU I'm not sure I understand your example. You can accept generic template with define as well:

markup::define! {
    Doc<Head: markup::Render, Body: markup::Render>(h: Head, b: Body) {
        head { @h }
        body { @b }
    }
}

Did you mean something else?

@diegopy
Copy link
Author

diegopy commented Apr 2, 2021

Yes, there's a performance difference -

Thanks for your detailed reply, very clear.

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

3 participants