Skip to content

[Feature Request] [proposal] Have the Writable trait require a function that returns an estimate of the buffer size needed. #4249

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

Open
1 task done
martinvuyk opened this issue Apr 1, 2025 · 0 comments
Labels
enhancement New feature or request max-repo mojo Issues that are related to mojo

Comments

@martinvuyk
Copy link
Contributor

Review Mojo's priorities

What is your request?

Have the Writable trait require a function that returns an estimate of the buffer size needed. The function could return 0 in the case of datastructures that don't know it ahead of time.

What is your motivation for this change?

Currently many uses of the writer API look like this:

fn __str__(self) -> String:
    var res = String(capacity=some_estimate) # most of the time it's even an empty constructor
    res.write(self)
    return res

One either has to know the exact size of each component or guess sizes. These calculations aren't of any use when the type's write_to function gets called without reserving it that way.

Any other details?

This change will be useful for:

  • Letting the type define its specific memory needs, without having to reserve the string size beforehand (which is not scalable for deeply nested datastructures)
  • To avoid memory allocation overhead in loops
  • And maybe it also opens the door to other optimizations

CC: @jackos

@martinvuyk martinvuyk added enhancement New feature or request max-repo mojo Issues that are related to mojo labels Apr 1, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request max-repo mojo Issues that are related to mojo
Projects
None yet
Development

No branches or pull requests

1 participant