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

is using clone on ToString function really important? #32

Open
Gulshan1-3 opened this issue Feb 8, 2025 · 10 comments
Open

is using clone on ToString function really important? #32

Gulshan1-3 opened this issue Feb 8, 2025 · 10 comments
Assignees
Labels
enhancement New feature or request good first issue Good for newcomers

Comments

@Gulshan1-3
Copy link

In https://github.com/metacall/metassr/blob/master/crates/html-generator/src/builder.rs#L27 we made a ToString method for HtmlOutput and we are cloning the string which is expensive is it important to clone the string cant we just use less expensive methods.

impl ToString for HtmlOutput { fn to_string(&self) -> String { self.0.clone() } }

@Gulshan1-3
Copy link
Author

@viferga

@hulxv hulxv added enhancement New feature or request good first issue Good for newcomers labels Feb 8, 2025
@hulxv
Copy link
Collaborator

hulxv commented Feb 8, 2025

It's a good note. You can work on it if you would like that :)

See CONTRIBUTING.md.

@Gulshan1-3
Copy link
Author

Sure I have 2 ways in my mind first is using reference to string instead of cloning and using COW(copy on write) which gives you optional ownership where it lazily copy the string which helps us encountering unnecessary cloning, your thoughts on it?

@Gulshan1-3
Copy link
Author

Also can you check the pull req and guide me on it

@Gulshan1-3
Copy link
Author

Gulshan1-3 commented Feb 9, 2025

tried using COW but the conversion of Scripts and styles from pathbufs to strings vice versa is difficult to handle with it ,I am trying to find a way to do that
also we are using unwrap for error handling which can panic cant be error handling better?

@hulxv
Copy link
Collaborator

hulxv commented Feb 10, 2025

HtmlOutput.to_string is used in place only

// crates/metassr-build/src/server/renderer/page.rs
pub fn render(&self) -> Result<String> {
    Ok(HtmlRenderer::new(&self.head, &self.body, &self.entries)
        .render()?
        .to_string())
}

I think it's useless to implement ToString. We can do something like use the existing string and take ownership of it like this:

impl HtmlOutput {
    pub fn into_inner(self) -> String {
        self.0
    }
}

Isn't that better? What do you think?

@Gulshan1-3
Copy link
Author

Yaa this could be a better solution than cloning everytime

@hulxv
Copy link
Collaborator

hulxv commented Feb 23, 2025

@Gulshan1-3 Would you like to refactor it?

@Gulshan1-3
Copy link
Author

sure i have to use the above approch you suggested?

@hulxv
Copy link
Collaborator

hulxv commented Feb 24, 2025

Yup, otherwise if you have something better

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

No branches or pull requests

2 participants