C
C#2d ago
zacoons

Stackalloc a span of tuples?

Is it possible to stackalloc a span of tuples like this? Span<(object, string)> foos = stackalloc (object, string)[12];
No description
10 Replies
MODiX
MODiX2d ago
Zeth
REPL Result: Success
void M() {
Span<(int, int)> a = stackalloc (int, int)[1];
}
void M() {
Span<(int, int)> a = stackalloc (int, int)[1];
}
Compile: 306.049ms | Execution: 28.801ms | React with ❌ to remove this embed.
zacoons
zacoonsOP2d ago
gotcha
Jimmacle
Jimmacle2d ago
does additionalSources have a small bounded length?
zacoons
zacoonsOP2d ago
not bounded, but generally quite small, yeah it's just a helper fn for multi binding
cap5lut
cap5lut2d ago
if u have a small maximum u can use an inline array
[InlineArray(16)]
public struct Buffer16<T>
{
private T _element0;
}
[InlineArray(16)]
public struct Buffer16<T>
{
private T _element0;
}
and use that
Span<(object, string)> span = new Buffer16<(object, string)>(); // cant remember if u have to cast or use a method here to get it as span
span = span[..actualLength];
// do something
Span<(object, string)> span = new Buffer16<(object, string)>(); // cant remember if u have to cast or use a method here to get it as span
span = span[..actualLength];
// do something
generally u shouldnt use unbound stack allocations. constants are safe and fast dynamic and bound to an upper limit are safe but not that fast. dynamic and unbound is not that fast and dangerous. so if u go over some maximum, u should either allocate an array or use an array pool as fallback
Petris
Petris2d ago
or just
Span<(object, string)> s = [default, default, default, default, default, default, default, default, default, default, default, default, default, default, default, default];
Span<(object, string)> s = [default, default, default, default, default, default, default, default, default, default, default, default, default, default, default, default];
cap5lut
cap5lut2d ago
didnt know that, tho i would still go with the buffer apprach than with this mess >.<
Petris
Petris2d ago
well that uses an inlinearray i wish there was some syntax for it
Petris
Petris2d ago
yeah I've meant my versioon hmm with the assignments maybe somebody should open a #roslyn issue to skip assigning with default

Did you find this page helpful?