Skip to content

Commit

Permalink
Fixed some performance issues
Browse files Browse the repository at this point in the history
  • Loading branch information
StefanKert committed Oct 4, 2019
1 parent 74ff988 commit bae30e6
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,5 @@ public interface IBuildInformationProvider
void BuildStarted(BuildAction currentBuildAction, BuildScope scope);
void BuildUpdate();
void ResetBuildInformationModel();

event Action BuildUpdated;
}
}
5 changes: 0 additions & 5 deletions src/BuildVision.UI/ViewModels/BuildVisionPaneViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -215,11 +215,6 @@ public BuildVisionPaneViewModel(
ControlSettings = settingsProvider.Settings;
Projects = _buildInformationProvider.Projects;

_buildInformationProvider.BuildUpdated += () =>
{
Application.Current.Dispatcher.BeginInvoke(new Action(() => OnPropertyChanged(nameof(GroupedProjectsList))));
};

_settingsProvider = settingsProvider;
_settingsProvider.SettingsChanged += () =>
{
Expand Down
61 changes: 28 additions & 33 deletions src/BuildVision/Helpers/SolutionProjectsExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ namespace BuildVision.Helpers
{
public static class SolutionProjectsExtensions
{
private static IList<Project> _cachedProjects;

public static SolutionModel ToSolutionBuildState(this Solution solution)
{
var solutionItem = new SolutionModel();
Expand Down Expand Up @@ -57,47 +59,17 @@ public static SolutionModel ToSolutionBuildState(this Solution solution)
return solutionItem;
}

public static IList<Project> GetProjects(this Solution solution)
{
var list = new List<Project>();
foreach (var proj in solution.Projects)
{
var project = proj as Project;
if (project == null)
{
continue;
}

if (project.Kind == EnvDTEProjectKinds.ProjectKindSolutionFolder)
{
list.AddRange(project.GetSubProjects());
}
else if (!project.IsHidden())
{
list.Add(project);
}
}
return list;
}
public static Project FirstOrDefaultProject(this Solution solution, Func<Project, bool> cond) => _cachedProjects.FirstOrDefault(cond);

public static Project FirstOrDefaultProject(this Solution solution, Func<Project, bool> cond)
{
var projects = solution.GetProjects();
return projects.FirstOrDefault(cond);
}

public static Project FirstProject(this Solution solution, Func<Project, bool> cond)
{
var projects = solution.GetProjects();
return projects.First(cond);
}
public static Project FirstProject(this Solution solution, Func<Project, bool> cond) => _cachedProjects.First(cond);

public static IList<ProjectItem> GetProjectItems(this Solution solution)
{
IList<Project> dteProjects = new List<Project>();
try
{
dteProjects = solution.GetProjects();
_cachedProjects = dteProjects;
}
catch (Exception ex)
{
Expand All @@ -123,6 +95,29 @@ public static IList<ProjectItem> GetProjectItems(this Solution solution)
return projectItems;
}

public static IList<Project> GetProjects(this Solution solution)
{
var list = new List<Project>();
foreach (var proj in solution.Projects)
{
var project = proj as Project;
if (project == null)
{
continue;
}

if (project.Kind == EnvDTEProjectKinds.ProjectKindSolutionFolder)
{
list.AddRange(project.GetSubProjects());
}
else if (!project.IsHidden())
{
list.Add(project);
}
}
return list;
}

public static void UpdateProperties(Project project, ProjectItem projectItem, string configuration = null, string platform = null)
{
if (project != null)
Expand Down
43 changes: 12 additions & 31 deletions src/BuildVision/Services/BuildInformationProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,9 @@ public class BuildInformationProvider : IBuildInformationProvider
private readonly IBuildService _buildService;
private readonly ITaskBarInfoService _taskBarInfoService;
private string _origTextCurrentState;
private const int BuildInProcessCountOfQuantumSleep = 5;
private const int BuildInProcessQuantumSleep = 50;
private CancellationTokenSource _buildProcessCancellationToken;
private const int BuildInProcessQuantumSleep = 250;
private int _currentQueuePosOfBuildingProject = 0;
private Timer _timer;

public IBuildInformationModel BuildInformationModel { get; } = new BuildInformationModel();
public ObservableCollection<IProjectItem> Projects { get; } = new ObservableRangeCollection<IProjectItem>();
Expand Down Expand Up @@ -78,7 +77,7 @@ public BuildInformationProvider(
public void ReloadCurrentProjects()
{
Projects.Clear();
((ObservableRangeCollection< IProjectItem>)Projects).AddRange(_solutionProvider.GetProjects());
((ObservableRangeCollection<IProjectItem>)Projects).AddRange(_solutionProvider.GetProjects());
}

public void ResetCurrentProjects() => Projects.Clear();
Expand Down Expand Up @@ -171,24 +170,6 @@ private bool TryGetProjectItem(BuildProjectContextEntry projectEntry, out IProje
return true;
}

public void Run(CancellationToken cancellationToken)
{
while (!cancellationToken.IsCancellationRequested)
{
BuildUpdate();

for (int i = 0; i < BuildInProcessQuantumSleep * BuildInProcessCountOfQuantumSleep; i += BuildInProcessQuantumSleep)
{
if (cancellationToken.IsCancellationRequested)
{
break;
}

Thread.Sleep(BuildInProcessQuantumSleep);
}
}
}

public void BuildStarted(BuildAction buildAction, BuildScope buildScope)
{
_currentQueuePosOfBuildingProject = 0;
Expand All @@ -205,10 +186,9 @@ public void BuildStarted(BuildAction buildAction, BuildScope buildScope)
BuildInformationModel.BuildScope = buildScope;
BuildInformationModel.BuildId = Guid.NewGuid();

_buildProcessCancellationToken = new CancellationTokenSource();
_windowStateService.ApplyToolWindowStateAction(_packageSettingsProvider.Settings.WindowSettings.WindowActionOnBuildBegin);
Task.Run(() => Run(_buildProcessCancellationToken.Token), _buildProcessCancellationToken.Token);

_timer = new Timer(state => BuildUpdate(), null, BuildInProcessQuantumSleep, BuildInProcessQuantumSleep);
string message = _buildMessagesFactory.GetBuildBeginMajorMessage(BuildInformationModel);
_statusBarNotificationService.ShowTextWithFreeze(message);
_origTextCurrentState = message;
Expand Down Expand Up @@ -335,14 +315,17 @@ private ProjectState GetProjectState(bool success, bool canceled, IProjectItem c

public void BuildUpdate()
{
if(BuildInformationModel.CurrentBuildState != BuildState.InProgress)
{
_logger.Error("Build is finished but thread still running.");
}
var message = _origTextCurrentState + _buildMessagesFactory.GetBuildBeginExtraMessage(BuildInformationModel);
BuildInformationModel.StateMessage = message;
_statusBarNotificationService.ShowTextWithFreeze(message);
foreach (var project in Projects)
foreach (var project in Projects.Where(x => x.BuildStartTime.HasValue && !x.BuildFinishTime.HasValue))
{
project.RaiseBuildElapsedTimeChanged();
}
BuildUpdated?.Invoke();
}

public void BuildFinished(bool success, bool canceled)
Expand All @@ -363,8 +346,8 @@ public void BuildFinished(bool success, bool canceled)
}
}

_buildProcessCancellationToken.Cancel();

_timer.Dispose();
_logger.Information("Canceled build");
BuildInformationModel.BuildFinishTime = DateTime.Now;
if (success)
{
Expand Down Expand Up @@ -424,7 +407,5 @@ public void BuildFinished(bool success, bool canceled)
}
}
}

public event Action BuildUpdated;
}
}
14 changes: 7 additions & 7 deletions src/BuildVision/Services/BuildOutputLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class BuildOutputLogger : Logger, IBuildOutputLogger
private Serilog.ILogger _logger = LogManager.ForContext<BuildOutputLogger>();
public RegisterLoggerResult LoggerState { get; set; }

private List<BuildProjectContextEntry> _projects = new List<BuildProjectContextEntry>();
private Dictionary<string ,BuildProjectContextEntry> _projects = new Dictionary<string, BuildProjectContextEntry>();

public BuildOutputLogger(Guid loggerId, LoggerVerbosity loggerVerbosity)
{
Expand All @@ -30,7 +30,7 @@ public BuildOutputLogger(Guid loggerId, LoggerVerbosity loggerVerbosity)

public override void Initialize(IEventSource eventSource)
{
_projects = new List<BuildProjectContextEntry>();
_projects = new Dictionary<string, BuildProjectContextEntry>();
eventSource.ProjectStarted += OnProjectStarted;
eventSource.MessageRaised += (s, e) => EventSource_ErrorRaised(e, ErrorLevel.Message);
eventSource.WarningRaised += (s, e) => EventSource_ErrorRaised(e, ErrorLevel.Warning);
Expand All @@ -39,7 +39,8 @@ public override void Initialize(IEventSource eventSource)

private void OnProjectStarted(object sender, ProjectStartedEventArgs e)
{
_projects.Add(new BuildProjectContextEntry(
var identifier = $"{e.BuildEventContext.ProjectInstanceId}{e.BuildEventContext.ProjectContextId}";
_projects.Add(identifier, new BuildProjectContextEntry(
e.BuildEventContext.ProjectInstanceId,
e.BuildEventContext.ProjectContextId,
e.ProjectFile,
Expand Down Expand Up @@ -95,7 +96,7 @@ public void Attach()

public bool IsProjectUpToDate(IProjectItem projectItem)
{
return !_projects.Exists(t => t.FileName == projectItem.FullName);
return !_projects.Any(x => x.Value.FileName == projectItem.FullName);
}

private void EventSource_ErrorRaised(BuildEventArgs e, ErrorLevel errorLevel)
Expand All @@ -109,9 +110,8 @@ private void EventSource_ErrorRaised(BuildEventArgs e, ErrorLevel errorLevel)

int projectInstanceId = e.BuildEventContext.ProjectInstanceId;
int projectContextId = e.BuildEventContext.ProjectContextId;

var projectEntry = _projects.Find(t => t.InstanceId == projectInstanceId && t.ContextId == projectContextId);
if (projectEntry == null)
var identifier = $"{projectInstanceId}{projectContextId}";
if (!_projects.TryGetValue(identifier, out var projectEntry))
{
_logger.Warning("Project entry not found by ProjectInstanceId='{ProjectInstanceId}' and ProjectContextId='{ProjectContextId}'.", projectInstanceId, projectContextId);
return;
Expand Down

0 comments on commit bae30e6

Please sign in to comment.