C
C#3w ago
SWEETPONY

✅ Is it worth to use stackalloc here?

Hey, I have this:
[HttpGet("stream")]
public async Task StreamHeartbeatAsync(CancellationToken cancellationToken)
{
Response.Headers.Append("Content-Type", "text/event-stream");
Response.Headers.Append("Cache-Control", "no-cache");
Response.Headers.Append("Connection", "keep-alive");

while(!cancellationToken.IsCancellationRequested)
{
var sseEvent = new SseEvent<object>
{
Action = SseAction.Heartbeat,
Timestamp = DateTime.UtcNow
};

await Response.WriteAsJsonAsync(sseEvent, cancellationToken);
await Response.Body.FlushAsync(cancellationToken);

await Task.Delay(sseSettings.Timeout, cancellationToken);
}
}

public sealed class SseEvent<T>
{
public required DateTime Timestamp { get; set; }
public required SseAction Action { get; set; }
public string? Store { get; set; }
public string? Identity { get; set; }
public T? Data { get; set; }
}
[HttpGet("stream")]
public async Task StreamHeartbeatAsync(CancellationToken cancellationToken)
{
Response.Headers.Append("Content-Type", "text/event-stream");
Response.Headers.Append("Cache-Control", "no-cache");
Response.Headers.Append("Connection", "keep-alive");

while(!cancellationToken.IsCancellationRequested)
{
var sseEvent = new SseEvent<object>
{
Action = SseAction.Heartbeat,
Timestamp = DateTime.UtcNow
};

await Response.WriteAsJsonAsync(sseEvent, cancellationToken);
await Response.Body.FlushAsync(cancellationToken);

await Task.Delay(sseSettings.Timeout, cancellationToken);
}
}

public sealed class SseEvent<T>
{
public required DateTime Timestamp { get; set; }
public required SseAction Action { get; set; }
public string? Store { get; set; }
public string? Identity { get; set; }
public T? Data { get; set; }
}
I think it is not good to create sseEvent every second without any memory clean. Is it worth to use stackalloc here?
22 Replies
ero
ero3w ago
var sseEvent = new SseEvent<object>
{
Action = SseAction.Heartbeat
};

while (!cancellationToken.IsCancellationRequested)
{
sseEvent.Timestamp = DateTime.UtcNow;

// ...
}
var sseEvent = new SseEvent<object>
{
Action = SseAction.Heartbeat
};

while (!cancellationToken.IsCancellationRequested)
{
sseEvent.Timestamp = DateTime.UtcNow;

// ...
}
would this not work?
jcotton42
jcotton423w ago
Classes cannot be stackalloced.
ero
ero3w ago
they can be in .net 9
jcotton42
jcotton423w ago
Link? Iirc there’s escape analysis coming, but that’s something the JIT may do as an optimization.
ero
ero3w ago
i must've made that fact up in my head somehow i swear i was very sure it was true
SWEETPONY
SWEETPONYOP3w ago
yeah, it was just an example and I missed this but anyway.. I can change class to struct or record struct for example is it worth it?
ero
ero3w ago
you'll have to benchmark that yourself. i wouldn't think it's really worth it, since it would be a fairly large struct
SWEETPONY
SWEETPONYOP3w ago
let's imagine we should somehow optimize objects creation. How can we do it?
ero
ero3w ago
why should you optimize? have you checked that this is a bottleneck at all? there are likely many other aspects that are much more prone to causing performance degradation besides instantiating a lot of classes...
cap5lut
cap5lut3w ago
because its async, u probably will have bad times with such a big struct anyway, if u pass value types around u are copying the data over. since its async, u cant utilize refs in any way either, so it will be most likely slower. if u want to reduce allocations, the better fit here would be most likely object pooling, which comes with its own down sides
ero
ero3w ago
(unless you use c# 13)
cap5lut
cap5lut3w ago
u are missing the point, u would have to pass the ref to the awaited async method, which wont happen to be allowed like ... ever
ero
ero3w ago
mhhh right
Unknown User
Unknown User3w ago
Message Not Public
Sign In & Join Server To View
SWEETPONY
SWEETPONYOP3w ago
hm yes, sse is good for this things actually I don't need signalr signalr can choose websockets, sse etc I just need sse
cap5lut
cap5lut3w ago
if its just server->client i wouldnt bother with websockets/signalr either
SWEETPONY
SWEETPONYOP3w ago
ws is good but i need sse here, thats why I use text/event-stream anyway thanks for helping
Unknown User
Unknown User3w ago
Message Not Public
Sign In & Join Server To View
SWEETPONY
SWEETPONYOP3w ago
MDN Web Docs
Using server-sent events - Web APIs | MDN
Developing a web application that uses server-sent events is straightforward. You'll need a bit of code on the server to stream events to the front-end, but the client side code works almost identically to websockets in part of handling incoming events. This is a one-way connection, so you can't send events from a client to a server.
SWEETPONY
SWEETPONYOP3w ago
and https://www.strathweb.com/2024/07/built-in-support-for-server-sent-events-in-net-9/ I don't need signalr in my opinion why should I install the whole library just for heartbeat I can do it manually also use signalr and force it to use just sse is weird
Omnissiah
Omnissiah3w ago
you would have to split by \n and add a data: for each line
Unknown User
Unknown User3w ago
Message Not Public
Sign In & Join Server To View
Want results from more Discord servers?
Add your server