Skip to content

Commit

Permalink
perf: ⚡ avoid reconstructing query string with string.Join() when p…
Browse files Browse the repository at this point in the history
…ossible
  • Loading branch information
JamesNZL committed May 16, 2023
1 parent 67f9d56 commit 861e77d
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 23 deletions.
9 changes: 9 additions & 0 deletions src/Main.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Windows.Controls;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Flow.Launcher.Plugin.TogglTrack.ViewModels;
Expand All @@ -18,6 +19,14 @@ public class Main : IAsyncPlugin, ISettingProvider

internal TogglTrack? _togglTrack;

public static string ExtractFromQuery(Query query, int index)
{
return (index == 1)
// Expect slight performance improvement by using query.SecondToEndSearch directly
? query.SecondToEndSearch
: string.Join(" ", query.SearchTerms.Skip(index));
}

/// <summary>
/// Runs on plugin initialisation.
/// Expensive operations should be performed here.
Expand Down
49 changes: 26 additions & 23 deletions src/TogglTrack.cs
Original file line number Diff line number Diff line change
Expand Up @@ -403,11 +403,12 @@ internal async ValueTask<List<Result>> RequestStartEntry(CancellationToken token
);
}

return (string.IsNullOrWhiteSpace(string.Join(" ", query.SearchTerms.Skip(ArgumentIndices.Project))))
string projectQuery = Main.ExtractFromQuery(query, ArgumentIndices.Project);
return (string.IsNullOrWhiteSpace(projectQuery))
? projects
: projects.FindAll(result =>
{
return this._context.API.FuzzySearch(string.Join(" ", query.SearchTerms.Skip(ArgumentIndices.Project)), $"{result.Title} {Regex.Replace(result.SubTitle, @"(?: \| )?\d+ hours?$", string.Empty)}").Score > 0;
return this._context.API.FuzzySearch(projectQuery, $"{result.Title} {Regex.Replace(result.SubTitle, @"(?: \| )?\d+ hours?$", string.Empty)}").Score > 0;
});
}

Expand All @@ -422,7 +423,7 @@ internal async ValueTask<List<Result>> RequestStartEntry(CancellationToken token
? $"{project.name}{clientName}"
: "No Project";

string description = string.Join(" ", query.SearchTerms.Skip(ArgumentIndices.Description));
string description = Main.ExtractFromQuery(query, ArgumentIndices.Description);

var results = new List<Result>
{
Expand Down Expand Up @@ -496,7 +497,7 @@ internal async ValueTask<List<Result>> RequestStartEntry(CancellationToken token
try
{
var startTimeSpan = TimeSpanParser.Parse(
string.Join(" ", query.SearchTerms.Skip(Array.IndexOf(query.SearchTerms, Settings.TimeSpanFlag) + 1)),
Main.ExtractFromQuery(query, Array.IndexOf(query.SearchTerms, Settings.TimeSpanFlag) + 1),
new TimeSpanParserOptions
{
UncolonedDefault = Units.Minutes,
Expand Down Expand Up @@ -758,11 +759,12 @@ internal async ValueTask<List<Result>> RequestEditEntry(CancellationToken token,
);
}

return (string.IsNullOrWhiteSpace(string.Join(" ", query.SearchTerms.Skip(ArgumentIndices.Project))))
string projectQuery = Main.ExtractFromQuery(query, ArgumentIndices.Project);
return (string.IsNullOrWhiteSpace(projectQuery))
? projects
: projects.FindAll(result =>
{
return this._context.API.FuzzySearch(string.Join(" ", query.SearchTerms.Skip(ArgumentIndices.Project)), $"{result.Title} {Regex.Replace(result.SubTitle, @"(?: \| )?\d+ hours?$", string.Empty)}").Score > 0;
return this._context.API.FuzzySearch(projectQuery, $"{result.Title} {Regex.Replace(result.SubTitle, @"(?: \| )?\d+ hours?$", string.Empty)}").Score > 0;
});
}

Expand All @@ -779,13 +781,11 @@ internal async ValueTask<List<Result>> RequestEditEntry(CancellationToken token,
? $"{project.name}{clientName}"
: "No Project";

string description = string.Join(
" ",
query.SearchTerms.Skip(
(this._editProjectState == TogglTrack.EditProjectState.ProjectSelected)
? ArgumentIndices.DescriptionWithProject
: ArgumentIndices.DescriptionWithoutProject
)
string description = Main.ExtractFromQuery(
query,
(this._editProjectState == TogglTrack.EditProjectState.ProjectSelected)
? ArgumentIndices.DescriptionWithProject
: ArgumentIndices.DescriptionWithoutProject
);

var results = new List<Result>
Expand Down Expand Up @@ -860,7 +860,7 @@ internal async ValueTask<List<Result>> RequestEditEntry(CancellationToken token,
try
{
var startTimeSpan = TimeSpanParser.Parse(
string.Join(" ", query.SearchTerms.Skip(Array.IndexOf(query.SearchTerms, Settings.TimeSpanFlag) + 1)),
Main.ExtractFromQuery(query, Array.IndexOf(query.SearchTerms, Settings.TimeSpanFlag) + 1),
new TimeSpanParserOptions
{
UncolonedDefault = Units.Minutes,
Expand Down Expand Up @@ -1086,7 +1086,7 @@ internal async ValueTask<List<Result>> RequestStopEntry(CancellationToken token,
try
{
var stopTimeSpan = TimeSpanParser.Parse(
string.Join(" ", query.SearchTerms.Skip(Array.IndexOf(query.SearchTerms, Settings.TimeSpanFlag) + 1)),
Main.ExtractFromQuery(query, Array.IndexOf(query.SearchTerms, Settings.TimeSpanFlag) + 1),
new TimeSpanParserOptions
{
UncolonedDefault = Units.Minutes,
Expand Down Expand Up @@ -1329,11 +1329,12 @@ internal async ValueTask<List<Result>> RequestContinueEntry(CancellationToken to
};
});

return (string.IsNullOrWhiteSpace(string.Join(" ", query.SearchTerms.Skip(ArgumentIndices.Description))))
string entriesQuery = Main.ExtractFromQuery(query, ArgumentIndices.Description);
return (string.IsNullOrWhiteSpace(entriesQuery))
? entries
: entries.FindAll(result =>
{
return this._context.API.FuzzySearch(string.Join(" ", query.SearchTerms.Skip(ArgumentIndices.Description)), result.Title).Score > 0;
return this._context.API.FuzzySearch(entriesQuery, result.Title).Score > 0;
});
}

Expand Down Expand Up @@ -1396,12 +1397,13 @@ internal async ValueTask<List<Result>> RequestViewReports(CancellationToken toke
},
};
});

return (string.IsNullOrWhiteSpace(string.Join(" ", query.SearchTerms.Skip(ArgumentIndices.Span))))

string spanQuery = Main.ExtractFromQuery(query, ArgumentIndices.Span);
return (string.IsNullOrWhiteSpace(spanQuery))
? spans
: spans.FindAll(result =>
{
return this._context.API.FuzzySearch(string.Join(" ", query.SearchTerms.Skip(ArgumentIndices.Span)), result.Title).Score > 0;
return this._context.API.FuzzySearch(spanQuery, result.Title).Score > 0;
});
}

Expand All @@ -1426,12 +1428,13 @@ internal async ValueTask<List<Result>> RequestViewReports(CancellationToken toke
},
};
});

return (string.IsNullOrWhiteSpace(string.Join(" ", query.SearchTerms.Skip(ArgumentIndices.Grouping))))

string groupingsQuery = Main.ExtractFromQuery(query, ArgumentIndices.Grouping);
return (string.IsNullOrWhiteSpace(groupingsQuery))
? groupings
: groupings.FindAll(result =>
{
return this._context.API.FuzzySearch(string.Join(" ", query.SearchTerms.Skip(ArgumentIndices.Grouping)), result.Title).Score > 0;
return this._context.API.FuzzySearch(groupingsQuery, result.Title).Score > 0;
});
}

Expand Down

0 comments on commit 861e77d

Please sign in to comment.