-
-
Notifications
You must be signed in to change notification settings - Fork 419
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
Update from_array to prevent a copy (issue #1097) #1423
Conversation
The `String.from_array` method previously was documented to only reuse the underlying `Array[U8]` if it ended with a `0` byte, otherwise the Array would be copied with the `0` added to the end. However, the behavior was such that the no-copy version would make the trailing `0` the last character of the String instead of a terminator, and changing that behavior caused other issues. Since pony Strings may now be non-null-terminated, the new behavior of `.from_array` is to always use the entire underlying Array, without regard to the presence of a trailing `0` byte. The documentation is also updated and tests added.
Side note... both previously and currently, the _alloc = data.space() If the Just wanted to throw that out there. |
Another thought... this is now almost identical to |
The That is, possibility-wise, we have these two kinds of transformations (ignoring other rcaps for the moment): Array iso => (String iso | String val)
(Array iso | Array val) => String val I don't think we can safely eliminate either without losing possibilities. Removing the former means there is no way to end up with a However, I'm hoping that a combination of generic type inference (ponylang/rfcs#26) and compile-time specialization (ponylang/rfcs#62) will save us here, when implemented. Then we could have something like: new val from_array[A: (Array[U8] val | Array[U8] iso)](data: A) iftype A <: Array[U8] val =>
// ...
new iso from_array[A: (Array[U8] val | Array[U8] iso)](data: A) iftype A <: Array[U8] iso =>
// ... |
Thanks @jemc, you're right. I was thinking there was enough magic/restriction in a It would be interesting to see a let a: Array[U8] val = recover ['a', 'b', 'c'] end
let s = recover iso
// Make a String val
let s = String.from_array(a)
// ...
s // Enforces that the String never escaped in any way except as the result
end Anyway, that's just off the top of my head. Perhaps it would be too confusing or problematic in other ways. |
At any rate, this dicsussion is a side point to the central issue this PR resolves, so I'm merging. Thanks for the fix! |
The
String.from_array
method previously was documented to only reusethe underlying
Array[U8]
if it ended with a0
byte, otherwise theArray would be copied with the
0
added to the end. However, thebehavior was such that the no-copy version would make the trailing
0
the last character of the String instead of a terminator, and changing
that behavior caused other issues.
Since pony Strings may now be non-null-terminated, the new behavior of
.from_array
is to always use the entire underlying Array, withoutregard to the presence of a trailing
0
byte.The documentation is also updated and tests added.