𝗝𝗮𝗺𝗲𝘀
𝗝𝗮𝗺𝗲𝘀
CC#
Created by 𝗝𝗮𝗺𝗲𝘀 on 9/18/2024 in #help
Does structs generate inaccessible copies?
Consider the following large structure for the asked questions
[InlineArray(128)]
public struct x1024BitBuffer
{
byte e0;
}
[InlineArray(128)]
public struct x1024BitBuffer
{
byte e0;
}
Does a local copy is created when returning from a method? In my understanding of how structure returning methods are handled, the current method will allocate the necessary memory to store the called method's return within the stack. But, the called method also has it's own stack, so, in the next scenarios, does the struct gets copied back in forth or is it internally passed by reference?
// consider that we marked this method NOT to be inlined
static x1024BitBuffer CreateMe()
=> new()
;

static void Main()
{
var x = CreateMe();
}
// consider that we marked this method NOT to be inlined
static x1024BitBuffer CreateMe()
=> new()
;

static void Main()
{
var x = CreateMe();
}
When Main invokes CreateMe is the structure allocated inside the creator method or does the compiler detects that no one can access it so it makes new() allocate directly into x's var memory? What if the variable is accessed in the creator method but only returned
// consider that we marked this method NOT to be inlined
static x1024BitBuffer CreateMe()
{
var temp = new x1024BitBuffer ();
temp[5] = 0x11;
return temp;
}

static void Main()
{
var x = CreateMe();
}
// consider that we marked this method NOT to be inlined
static x1024BitBuffer CreateMe()
{
var temp = new x1024BitBuffer ();
temp[5] = 0x11;
return temp;
}

static void Main()
{
var x = CreateMe();
}
Since the value temp is only modified and then returned, is the memory for it allocated in CreateMe's stack and then copied to the caller method stack or accessed directly? Does a a copy of a struct is created when passing through an init accessor?
readonly struct Data
{
readonly x1024BitBuffer payload;

public x1024BitBuffer Payload
{
init => payload = value;
}
}

static void Main()
{
Data data = new()
{
PayLoad = CreateMe()
};
}
readonly struct Data
{
readonly x1024BitBuffer payload;

public x1024BitBuffer Payload
{
init => payload = value;
}
}

static void Main()
{
Data data = new()
{
PayLoad = CreateMe()
};
}
When creating a Data instance, the IL method init_Payload is called. So the stack traces looks something like the following .With that in mind, does the large structure gets copied 2 times or the compiler optmises that in some way? Main() -> CreateMe() -> init_Payload new Data() | => new(); | arg: value
12 replies
CC#
Created by 𝗝𝗮𝗺𝗲𝘀 on 5/7/2024 in #help
Are streams any better than byte array outside of I.O context?
Streams are visibly better when working with files, network responses and many other cases, but, when specifically working with objects which already live within RAM memory, are streams still useful? I.E why does Binary Formatter used streams when formatting managed objects? The understanding that I have from streams is a lot like IEnumerable<byte> with a temp buffer, meaning, nothing happens upon creation until fetched. Maybe postpone an action till last moment?
6 replies