Skip to content

⚡ Optimize IPtalkManager string concatenation#3

Open
SilentMalachite wants to merge 1 commit into
mainfrom
perf/optimize-string-concatenation-28176032284257407
Open

⚡ Optimize IPtalkManager string concatenation#3
SilentMalachite wants to merge 1 commit into
mainfrom
perf/optimize-string-concatenation-28176032284257407

Conversation

@SilentMalachite
Copy link
Copy Markdown
Owner

💡 What: Replaced the receivedText stored String property with a receivedTextSegments array of strings, exposing receivedText as a computed property that joins the segments.

🎯 Why: The previous implementation used += for string concatenation in processReceivedData, which can lead to $O(N^2)$ performance degradation as the text grows (due to repeated copying). Using an array of strings reduces the append complexity to amortized $O(1)$.

📊 Measured Improvement:

  • Theoretical: $O(N^2)$ -> $O(N)$ (where N is total text length).
  • Verification: A benchmark test (PerformanceTests.swift) was designed but could not be executed due to the Linux environment lacking macOS frameworks (Network, XCTest).
  • Note: The logic ensures that newline handling remains identical to the original implementation. receivedText is now read-only, which matches its usage pattern (only written internally via processReceivedData and clearReceivedText).

PR created automatically by Jules for task 28176032284257407 started by @SilentMalachite

Replaced repeated string concatenation with array-based buffering to improve performance from O(N^2) to amortized O(1) for appends.
Verified logic preservation manually.
Performance verification was not executed due to environment limitations.

Co-authored-by: SilentMalachite <84108752+SilentMalachite@users.noreply.github.com>
Copilot AI review requested due to automatic review settings January 31, 2026 14:41
@google-labs-jules
Copy link
Copy Markdown

👋 Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR optimizes how IPtalkManager accumulates received text to avoid quadratic-time string concatenation, while preserving the observable interface used by the UI.

Changes:

  • Replaced the mutable receivedText: String stored property with a private receivedTextSegments: [String] plus a computed receivedText that joins the segments.
  • Updated processReceivedData(_:) to append incoming text and optional newlines to receivedTextSegments instead of concatenating onto a single String.
  • Updated clearReceivedText() to clear receivedTextSegments, keeping the external API (receivedText and clearReceivedText()) behaviorally consistent.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +8 to +10
private var receivedTextSegments: [String] = []
var receivedText: String {
receivedTextSegments.joined()
Copy link

Copilot AI Jan 31, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new computed receivedText now joins the entire receivedTextSegments array on every access, which turns each read into an O(N) operation and can cause extra allocations, especially since SwiftUI accesses this property multiple times per render (for Text, isEmpty checks, and button state). To preserve the append-time optimization without introducing a potential read-time bottleneck, consider caching the joined string and invalidating/rebuilding it only when receivedTextSegments changes (e.g., track a dirty flag or similar).

Suggested change
private var receivedTextSegments: [String] = []
var receivedText: String {
receivedTextSegments.joined()
private var receivedTextSegments: [String] = [] {
didSet {
receivedTextCache = receivedTextSegments.joined()
}
}
private var receivedTextCache: String = ""
var receivedText: String {
receivedTextCache

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

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.

2 participants