Skip to content

[Repo Assist] perf: eliminate unnecessary List(T) allocations in DisplayText and GetSessionListInternal#156

Closed
github-actions[bot] wants to merge 1 commit intomasterfrom
repo-assist/perf-avoid-list-alloc-display-20260408-5b64110d3ae6402c
Closed

[Repo Assist] perf: eliminate unnecessary List(T) allocations in DisplayText and GetSessionListInternal#156
github-actions[bot] wants to merge 1 commit intomasterfrom
repo-assist/perf-avoid-list-alloc-display-20260408-5b64110d3ae6402c

Conversation

@github-actions
Copy link
Copy Markdown
Contributor

@github-actions github-actions bot commented Apr 8, 2026

🤖 This PR was created by Repo Assist, an automated AI assistant.

Summary

Two small hot-path allocations eliminated: SessionInfo.DisplayText and GetSessionListInternal.


1. SessionInfo.DisplayText — avoid List(string) per UI render

Problem: DisplayText builds a 1–3 element list then calls string.Join, allocating both a List(string) and its backing string[] on every call. This property is called every UI refresh for every visible session.

Fix: Replace with a tuple switch over the two optional segments (mid and end); only the final interpolated string is allocated, same as before.

// Before
var parts = new List(string) { prefix };
if (!string.IsNullOrEmpty(Channel)) parts.Add(Channel);
if (!string.IsNullOrEmpty(CurrentActivity)) parts.Add(CurrentActivity);
else if (...) parts.Add(Status);
return string.Join(" · ", parts);

// After
string? mid = !string.IsNullOrEmpty(Channel) ? Channel : null;
string? end = ...;
return (mid, end) switch
{
    (null, null) => prefix,
    (not null, null) => $"{prefix} · {mid}",
    (null, not null) => $"{prefix} · {end}",
    _ => $"{prefix} · {mid} · {end}",
};

2. GetSessionListInternal — avoid double-copy on session-list snapshot

Problem: Every session-list update called new List(SessionInfo)(_sessions.Values) (copy 1) then .ToArray() (copy 2), paying two heap allocations per snapshot.

Fix: Allocate one SessionInfo[count] array, CopyTo the dictionary values directly into it, sort in-place with Array.Sort and a static lambda.

// Before: 2 allocations
var list = new List(SessionInfo)(_sessions.Values);
list.Sort(...);
return list.ToArray();

// After: 1 allocation
var result = new SessionInfo[_sessions.Count];
_sessions.Values.CopyTo(result, 0);
Array.Sort(result, static (a, b) => ...);
return result;

Trade-offs

  • No behaviour change. All existing SessionInfoTests and GetSessionList_SortsMainSessionFirst tests pass unchanged.
  • static lambda in Array.Sort avoids the delegate allocation each time (compiler caches it).

Test Status

  • ✅ Build succeeded (0 errors, 0 warnings)
  • ✅ Shared tests: 525 passed, 20 skipped, 0 failed
  • ✅ Tray tests: 99 passed, 0 failed

Generated by Repo Assist ·

To install this agentic workflow, run

gh aw add githubnext/agentics/workflows/repo-assist.md@cbb46ab386962aa371045839fc9998ee4e97ca64

SessionInfo.DisplayText was allocating new List<string> + internal
backing array on every call (happens every UI refresh per session).
Replace with a simple tuple-switch over at most two optional segments
so only the final interpolated string is allocated.

GetSessionListInternal was constructing a List<SessionInfo> from the
dictionary values and then calling .ToArray(), paying two heap
allocations per session-list snapshot. Replace with a single
new SessionInfo[_sessions.Count] + Values.CopyTo + Array.Sort
so only one array is allocated and the sort is performed in-place.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@shanselman
Copy link
Copy Markdown
Collaborator

Thanks for the cleanup here — the idea makes sense, but we’re going to pass on this one for now. The current code is working today, and this is mostly a micro-optimization rather than a bug fix. We’d rather avoid extra churn in the session display/list snapshot path unless we’re addressing a larger underlying issue at the same time.

@shanselman shanselman closed this Apr 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant