Skip to content

Commit

Permalink
#351 Repl is slow when displaying lots of coloured output
Browse files Browse the repository at this point in the history
Batches edits to the repl window buffer so that the updates all occur at once.
Removes unnecessary marshalling to the UI thread.
Fixes error when opening interactive window.
  • Loading branch information
zooba committed Jun 9, 2015
1 parent 6bcccaa commit 451083a
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 18 deletions.
13 changes: 9 additions & 4 deletions Common/Product/ReplWindow/Repl/OutputBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
using System.Diagnostics;
using System.Text;
using System.Windows.Threading;
using Microsoft.VisualStudio.Text;

#if NTVS_FEATURE_INTERACTIVEWINDOW
namespace Microsoft.NodejsTools.Repl {
Expand Down Expand Up @@ -241,11 +242,15 @@ internal sealed class OutputBuffer : IDisposable {
}

if (entries.Length > 0) {
for (int i = 0; i < entries.Length; i++) {
var entry = entries[i];

_window.AppendOutput(entry.Properties.Color, entry.Buffer.ToString(), i == entries.Length - 1);
var colors = new List<ColoredSpan>();
var text = new StringBuilder();
foreach (var entry in entries) {
int start = text.Length;
text.Append(entry.Buffer.ToString());
colors.Add(new ColoredSpan(new Span(start, entry.Buffer.Length), entry.Properties.Color));
}
_window.AppendOutput(colors, text.ToString());

_window.TextView.Caret.EnsureVisible();
}
}
Expand Down
3 changes: 3 additions & 0 deletions Common/Product/ReplWindow/Repl/ReplOutputClassifier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ class ReplOutputClassifier : IClassifier {
if (startIndex < 0) {
startIndex = ~startIndex - 1;
}
if (startIndex < 0) {
startIndex = 0;
}

int spanEnd = span.End.Position;
for (int i = startIndex; i < coloredSpans.Count && coloredSpans[i].Span.Start < spanEnd; i++) {
Expand Down
29 changes: 15 additions & 14 deletions Common/Product/ReplWindow/Repl/ReplWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2087,15 +2087,11 @@ private sealed class CommandFilter : IOleCommandTarget {
}

public void WriteOutput(object output) {
UIThread(() => {
Write(output);
});
Write(output);
}

public void WriteError(object output) {
UIThread(() => {
Write(output, error: true);
});
Write(output, error: true);
}

private void Write(object text, bool error = false) {
Expand All @@ -2110,7 +2106,7 @@ private sealed class CommandFilter : IOleCommandTarget {
/// <summary>
/// Appends text to the output buffer and updates projection buffer to include it.
/// </summary>
internal void AppendOutput(ConsoleColor color, string text, bool lastOutput) {
internal void AppendOutput(IEnumerable<ColoredSpan> colors, string text) {
int oldBufferLength = _outputBuffer.CurrentSnapshot.Length;
int oldLineCount = _outputBuffer.CurrentSnapshot.LineCount;

Expand All @@ -2129,7 +2125,7 @@ private sealed class CommandFilter : IOleCommandTarget {
}

edit.Insert(oldBufferLength, text);
if (lastOutput && !_readingStdIn && !EndsWithLineBreak(text)) {
if (!_readingStdIn && !EndsWithLineBreak(text)) {
var lineBreak = GetLineBreak();
edit.Insert(oldBufferLength, lineBreak);
newOutputLength += lineBreak.Length;
Expand All @@ -2154,7 +2150,10 @@ private sealed class CommandFilter : IOleCommandTarget {
);

var outputSpan = new ReplSpan(trackingSpan, ReplSpanKind.Output);
_outputColors.Add(new ColoredSpan(span, color));
_outputColors.AddRange(colors.Select(cs => new ColoredSpan(
new Span(cs.Span.Start + oldBufferLength, cs.Span.Length),
cs.Color
)));

bool appended = false;

Expand Down Expand Up @@ -2210,7 +2209,11 @@ private sealed class CommandFilter : IOleCommandTarget {

private bool TryShowObject(object obj) {
UIElement element = obj as UIElement;
if (element != null) {
if (element == null) {
return false;
}

UIThread(() => {
_buffer.Flush();
// figure out where we're inserting the image
Expand Down Expand Up @@ -2244,10 +2247,8 @@ private sealed class CommandFilter : IOleCommandTarget {
OnInlineAdornmentAdded();
WriteLine(String.Empty);
WriteLine(String.Empty);
return true;
}

return false;
});
return true;
}

private void OnAdornmentLoaded(object source, EventArgs e) {
Expand Down

0 comments on commit 451083a

Please sign in to comment.