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
cmd/compile: opt: reuse interface payload during interface conversion #24582
Comments
In your first example, the compiler does not avoid an allocation. It does still call I'm confused as to why you have code like this. You are converting from an interface to a concrete type and then back to an interface. Why not just convert directly from interface A to interface B? That would avoid the allocation. |
Conceivably one might want to do something |
Sure, in general this can happen. But if the original interface is available you can always go the direct route. In this case, just return |
You're right. I wrote the first example without looking at the generated assembly because last time I checked the compiler was applying this optimization, but now that I check, it indeed is not, and go1.9's gc doesn't seem to do so either. Sorry for the confusion. Apparently the optimization is a figment of my imagination. So, let me change this issue to: please add this optimization for both cases. As to why it's needed, consider functions of this form that visit a tree of values:
Such a function need not allocate a new string header for each leaf in the tree, but that's what it does. |
I guess I don't see this as that subtle. Returning What would the optimization entail? We'd have to notice that at a concrete type -> interface type conversion, the concrete type came from an interface -> concrete conversion, and forward the data pointer. Doesn't seem too hard in the abstract. Concretely it might be kinda tricky at the moment. Forwarding like that is easy in SSA but hard on the AST. Unfortunately, lowering interface conversions to runtime calls happens before converting to SSA. So it might involve moving interface conversions into SSA. |
Good point.
That's fair. It seems like anything that eliminates an unnecessary implicit allocation is a good thing, but maybe returning the concrete value is infrequent anyway? |
The current representation of interface values is a two-word (type, data) pair where data is always a reference; multiword values such as strings must be copied to a hidden variable whose address is saved in the data field. The compiler is currently smart about avoiding unnecessary allocations in some cases, such as this one:
However, if the interface type has methods, the compiler does not appear to perform this optimization:
It would be nice if it did; I suspect the hard part is mostly done already.
The fact that assigning a string value to an interface variable allocates memory caught me out when I first noticed it in a profile.
The text was updated successfully, but these errors were encountered: