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

Improve String.map and String.mapi #2127

Open
ospencer opened this issue Jul 28, 2024 · 2 comments
Open

Improve String.map and String.mapi #2127

ospencer opened this issue Jul 28, 2024 · 2 comments
Labels
good second issue Great issues if you're familiar with contributing to Grain. stdlib

Comments

@ospencer
Copy link
Member

We currently implement these via String.explode and String.implode, but should instead iterate and build a new string. We probably don't want to make String depend on Buffer, but we could guess the next string size and realloc/copy if necessary.

@ospencer ospencer added stdlib good second issue Great issues if you're familiar with contributing to Grain. labels Jul 28, 2024
@spotandjake
Copy link
Member

It may make sense to implement an internal simplified version of the buffer library for this and #2087

@spotandjake
Copy link
Member

spotandjake commented Aug 2, 2024

We currently implement these via String.explode and String.implode, but should instead iterate and build a new string. We probably don't want to make String depend on Buffer, but we could guess the next string size and realloc/copy if necessary.

Instead of using a buffer for this would we be able to allocate a string of size 4*charCount which is the max bytesize the output string could be and, do the mapping and then shrink the strings bytelength to the real size without doing a realloc? This wouldn't hurt memory allocation in any way right but it would mean strings use more memory then they need beause we cant free a portion of a block right? I think this would be way more efficent then a buffer as it means we can do one alloc and write the characters directly to the string without needing any copying. Though it might result in a lot of wasted memory space if you are mapping large strings.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good second issue Great issues if you're familiar with contributing to Grain. stdlib
Projects
None yet
Development

No branches or pull requests

2 participants