-
Notifications
You must be signed in to change notification settings - Fork 0
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
Implement conversion methods between Vec
s of AST enums which inherit from each other
#153
Comments
I am interested in trying it |
The current conversions use safe code only and rely on compiler to optimize it. But I think these ones would need unsafe code ( impl<'a> From<Vec<'a, Expression<'a>> for Vec<'a, Argument<'a>> {
fn from(vec: Vec<'a, Expression<'a>) -> Self {
// SAFETY: Maybe this is safe!
unsafe { std::mem::transmute(vec) }
}
} |
Vec
s and Box
es of AST enums which inherit from each otherVec
s of AST enums which inherit from each other
We could maybe also add APIs to |
|
I'm not 100% sure if this is safe. I think it is, but would like to think about it more carefully. If we decide it is safe, and we want to do it, I think we should implement for all inherited enums with (a) It's less error-prone to generate unsafe code than to write it by hand, and It's not going to be a massive perf win, so I think no hurry. |
Beyond soundness issues, there's also a question of whether allowing this conversion loses us anything else useful. At present, we have an invariant that if you get a pointer to an element in a But if you can convert a We don't currently make use of that invariant, but we may want to in future. So by doing this, we close off other possibilities. That's the part I'm not sure about - is it a good idea or not? Do we care about this invariant? |
We have zero-cost conversion methods for converting e.g.
From<Expression> for Argument
. It's zero cost becauseArgument
inheritsExpression
's variants, so a validExpression
is also a validArgument
, and the conversion is just an in-place transmute.Implement the same for converting
Vec
s. e.g.Vec<'a, Expression<'a>>
should be able to be converted toVec<'a, Argument<'a>>
as a zero cost transmute - without memory copy or allocation.DittoEdit: No point. We never box enums.From<Box<'a, Expression<'a>>> for Box<'a, Argument<'a>>
.The code touched in oxc-project/oxc#7854 is an example of where we could use this for a more efficient operation.
The text was updated successfully, but these errors were encountered: