Skip to content

Fixes #3985. Cache URI validation in Link to avoid revalidation on every redraw#4833

Merged
tig merged 2 commits intov2_developfrom
copilot/fix-uri-validation-performance-issue
Mar 13, 2026
Merged

Fixes #3985. Cache URI validation in Link to avoid revalidation on every redraw#4833
tig merged 2 commits intov2_developfrom
copilot/fix-uri-validation-performance-issue

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Mar 13, 2026

Link.OnDrawingText called Uri.IsWellFormedUriString on every redraw cycle. In TUI applications with many Link views or frequent redraws (layout invalidation, animations, refresh), this is redundant parsing of the same string.

Changes

  • _isUrlValid backing field added alongside _url, initialized to false (matches the DEFAULT_URL = "" default which is not a valid absolute URI)
  • Validation moved to SetUrl — computed once when Url changes, cached in _isUrlValid
  • OnDrawingText uses _isUrlValid — eliminates the per-draw Uri.IsWellFormedUriString call
  • Url XML doc updated to reflect validation happens at set-time, not draw-time
// Before: validated on every redraw
if (!Uri.IsWellFormedUriString (Url, UriKind.Absolute)) { ... }

// After: validated once in SetUrl, cached
_url = value;
_isUrlValid = Uri.IsWellFormedUriString (value, UriKind.Absolute);

// OnDrawingText uses the cache
if (!_isUrlValid) { ... }

Behavior is identical — empty/invalid URLs still render with VisualRole.Disabled and suppress OSC 8 sequences. All existing LinkTests pass.

Pull Request checklist:

  • I've named my PR in the form of "Fixes #issue. Terse description."
  • My code follows the style guidelines of Terminal.Gui - if you use Visual Studio, hit CTRL-K-D to automatically reformat your files before committing.
  • My code follows the Terminal.Gui library design guidelines
  • I ran dotnet test before commit
  • I have made corresponding changes to the API documentation (using /// style comments)
  • My changes generate no new warnings
  • I have checked my code and corrected any poor grammar or misspellings
  • I conducted basic QA to assure all features are working
Original prompt

This section details on the original issue you should resolve

<issue_title>Potential perf issue : Link URL validation moved to draw time may cause performance issues</issue_title>
<issue_description>Hi there,

Link view PR was merged with some adaptation.

URL validation moved to draw time — Url accepts any string in the setter (with full CWP event support via UrlChanging/UrlChanged). Validation happens in OnDrawingText: invalid URLs render with the Disabled visual role and don't emit OSC 8 sequences.

One small point of attention regarding the URI validation.

I noticed that Uri.IsWellFormedUriString is currently evaluated during rendering. While this works functionally, it means the URI will be revalidated every time the view is redrawn.

In most cases this probably has negligible impact, but since rendering can occur frequently in a TUI (e.g., layout invalidation, refresh cycles, animations, etc.), repeatedly parsing the same URI could become unnecessary work if many Link views are present.

An alternative approach could be to validate the URI when the Url property changes and cache the result, keeping the render path as lightweight as possible.

Of course this is not a blocker, just a small performance consideration depending on how often the control might be redrawn.</issue_description>

Comments on the Issue (you are @copilot in this section)


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Co-authored-by: tig <585482+tig@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix potential performance issue with link URL validation Fixes #3985. Cache URI validation in Link to avoid revalidation on every redraw Mar 13, 2026
Copilot AI requested a review from tig March 13, 2026 03:17
Copilot finished work on behalf of tig March 13, 2026 03:17
@tig tig marked this pull request as ready for review March 13, 2026 03:50
@tig tig merged commit dd2e44a into v2_develop Mar 13, 2026
11 checks passed
@tig tig deleted the copilot/fix-uri-validation-performance-issue branch March 13, 2026 04:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Potential perf issue : Link URL validation moved to draw time may cause performance issues

2 participants