Replies: 3 comments 2 replies
-
Moving to runtime as this doesn't appear to be a language request. Up to runtime team if htey want to expose this special |
Beta Was this translation helpful? Give feedback.
-
One issue with this is inheritance. Say you have:
If it reserves 8 bytes, then when trying to store an instance of |
Beta Was this translation helpful? Give feedback.
-
@kevingosse Inheritance actually works fine! And by works fine.. I mean it doesn't work on Inline in cases where 'T' has child classes. Lets examine what happens! Say I have some base class: The default constructor is always used to initialize the object- and enough memory is allocated only for Now lets say I want to do this! case1:
I have updated the post to specify that Inline member variables are generally expected to NOT be polymorphic, hopefully this clears any confusion |
Beta Was this translation helpful? Give feedback.
-
It would be convenient if we could determine where reference types are allocated. The motivation is to reduce the amount of indirection in complex objects that are composed of nested heap-allocated reference types.
Example 1:
In the first example, I have marked reference types as
inline
which will instruct the runtime to allocate theInline<refTypes>
and their ValueType fields in a contiguous tightly packed block- which together will be the ExampleClass.The goal here, being reducing amount of indirection of accessing all the different reference types which are normally allocated individually to the heap.
There are some restrictions here of course. For example:
struct Inline<T>
has an overloaded assignment operator that only accepts objects who implementICopyable<T>
.ICopyable
is an copy interface which has the methodvoid copy<T>(in T from)
T
does not have to implement ICopyable to be used withInline<T>
. This generic constraint only applies to the assignment operator andcopy<T>(in T from)
function.struct Inline<T>
has aT getRef()
which just returns a normal reference type of T! (not a ref T though). The reference type returned bygetRef
can act like any normal reference type!Inline<object[]>
Inline<BaseT>
is always exclusively anInline<BaseT>
(The Copyable:copy interface can still be used for copying into them, though)Example 2:
In Example 2, I have allocated 20 reference types (from example 1) contiguously to an array. All of the same conditions from example 1 apply here too.. The array elements will not be freed from memory until the root (the array object) is freed.
So I know what you're thinking... why not use structs? And this is true! In my own code I can use structs. However, most of the code I use is not mine! (like the .NET library) and has many reference types which I would love to use more efficiently.
If we don't like the new
Inline<T>
wrapper, then there could also just be a new keywordinline
.private inline object _myObject;
var myArray = new inline ExampleClass[20];
(I think a Inline class would be easier to implement though, albeit requiring some special runtime & compiler updates)
Beta Was this translation helpful? Give feedback.
All reactions