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

In functions using ada_owned_string, return ada_owned_string instead of &str #67

Merged
merged 4 commits into from
May 20, 2024
Merged

Conversation

pratikpc
Copy link
Contributor

@pratikpc pratikpc commented May 20, 2024

Why

  1. The previous approach would leak memory

Advantages

  1. Returns an owned string
  2. No memory leaks

Disadvantages

  1. ada_owned_string now implements Drop trait.
    This would lead to the freeing of memory upon its going out of scope breaking existing code.
    This behaviour is probably more Rust-like but breaks user-space.
    The alternative is to not impement Drop and it could be done.
    However, this would make it user space's responsibility and hence not exactly an ideal behaviour
  2. Usage of as_ref needed to convert to &str as can be seen in unit tests.
    This can be resolved by probably implementing conversion functions if needed for Display, to_string etc. in std mode for end-user ease.

Another approach

  1. Returning String
    Would lead to a major breaking change.
    The approach would lead to all 3 functions no longer working in no-std mode.
    That is probably not ideal.

Closes #63

…ned_string

ada_owned_string is supposed to be cleared manually. This was not being done currently. Instead an internal reference to the memory was being returned by these functions. This led to us being unable to delete the memory out of fear of invalidating the &str present in user space. By returning ada_owned_string, this issue is resolved and the memory leaks no longer happen in library space. Old &str can still be accessed using as_ref.

BREAKING CHANGE: Functions using ada_owned_string now return ada_owned_string instead of &str. Memory leaks no longer happen in library space. Old &str can still be accessed using as_ref.
Clears the memory after the ada_owned_string goes out of scope. This ensures that ada_owned_string is cleared in user space.

BREAKING CHANGE: ada_owned_string was previously not freed upon going out of scope. Now it is.
@pratikpc pratikpc marked this pull request as ready for review May 20, 2024 18:39
src/idna.rs Outdated Show resolved Hide resolved
src/idna.rs Outdated Show resolved Hide resolved
@pratikpc
Copy link
Contributor Author

pratikpc commented May 20, 2024

Aah

I actually did implement the same and then yeeted everything back 30 minutes ago because of a few concerns

  1. Loss of performance due to an additional copy
  2. Would no longer support no-std as std::string::String.

I can re-implement the same. It's just a few undos

@pratikpc pratikpc marked this pull request as draft May 20, 2024 18:47
Makes it possible to convert ada_owned_string to ToString
…_string to String

Ensures that ada_owned_string does not leak to user space and allows us to make internal breaking changes to its implementation. Fixes all leak issue. Causes small performance downgrades due to copies. Causes functions to be usable only if std is enabled.

BREAKING CHANGE: Return type changed from ada_owned_string->String. Functions now work only with std feature enabled.
@pratikpc pratikpc marked this pull request as ready for review May 20, 2024 19:00
Copy link
Member

@anonrig anonrig left a comment

Choose a reason for hiding this comment

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

Thanks!

@anonrig anonrig merged commit f1bf053 into ada-url:main May 20, 2024
6 checks passed
@pratikpc pratikpc deleted the develop branch May 20, 2024 19:12
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

Successfully merging this pull request may close these issues.

Does Url::origin leak?
2 participants