-
|
This is my conclusion after reading the Programming Manual. Please correct me if I'm wrong. I'm considering using structs for performance reasons. I want to avoid boxing and unboxing, but I also prefer heap allocation over stack allocation. My struct is very simple. It contains a real number and a method that generates another real number using the original one through a mathematical formula (involving square root, exponential, and some trigonometric functions). BTW, I’d like to ask: are there mathematical methods available for working with primitive types if I decide to use Thank you. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
|
Yes, it is correct. To force it to be dynamic you have to declare it as nullable. So the situation is following. The structure is a dump of memory. And when you declare a local variable it will reside in the stack. When you declare a field inside the structure, it will be placed inside this structure. If it is a normal class - it will be created dynamically When you call the constructor in the worst case it will create a dynamic object and copy it content into the stack. There is an optimization which allow to call the constructor directly For example the following code: If you look into bytecode generated you will notice it is stack allocated: But you may force the compiler to make it always dynamic simply be declaring it as nullable And now look at byte code: As you see no copy opcode, so the structure is pure dynamic. The only problem that it will give you warning about an operation with nullable object. But you either check if the object is not nil, or if you sure it is always not nil, similar to C# do something like this: As for primitive data types. All basic numeric operations with real type will be native, e.g. And the output is: You will see, that no methods were called. With mathematical functions, it is still requires to call the method. But I could probably think about how to support it natively as well. |
Beta Was this translation helpful? Give feedback.
Yes, it is correct. To force it to be dynamic you have to declare it as nullable.
So the situation is following. The structure is a dump of memory. And when you declare a local variable it will reside in the stack. When you declare a field inside the structure, it will be placed inside this structure. If it is a normal class - it will be created dynamically
When you call the constructor in the worst case it will create a dynamic object and copy it content into the stack. There is an optimization which allow to call the constructor directly
For example the following code:
If yo…