Skip to content

Commit

Permalink
.Net: Use static Regex for ActionPlanner.CreatePlanAsync (#1954)
Browse files Browse the repository at this point in the history
### Motivation and Context
<!-- Thank you for your contribution to the semantic-kernel repo!
Please help reviewers and future users, providing the following
information:
  1. Why is this change required?
To ensure best performance for Semantic Kernel apps.
  2. What problem does it solve?
 Excessive CPU and memory usage when creating plans via ActionPlanner.
  4. What scenario does it contribute to?
  5. If it fixes an open issue, please link to the issue here.
  Related to this issue (which may already be fixed but not released):
#1919
-->

Creating and executing plans will be a common activity for apps using
the Semantic Kernel / Action Planner.

The regular expression used with a constant pattern string should be
allocated once and compiled to maximize performance for server
applications. This will save both CPU and memory.

### Description
Move the Regex allocation to a static field and consumed it from there.

### Contribution Checklist
<!-- Before submitting this PR, please make sure: -->
- [ X] The code builds clean without any errors or warnings
- [ X] The PR follows SK Contribution Guidelines
(https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md)
- [ X] The code follows the .NET coding conventions
(https://learn.microsoft.com/dotnet/csharp/fundamentals/coding-style/coding-conventions)
verified with `dotnet format`
- [ ] All unit tests pass, and I have added new tests where possible
Logic should be identical, however the tests in main don't currently
pass locally so there is no base line.
- [x ] I didn't break anyone 😄

---------

Co-authored-by: Dmytro Struk <13853051+dmytrostruk@users.noreply.github.com>
  • Loading branch information
nschuessler and dmytrostruk authored Jul 14, 2023
1 parent b841df7 commit 29f226c
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions dotnet/src/Extensions/Planning.ActionPlanner/ActionPlanner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ public sealed class ActionPlanner
private const string StopSequence = "#END-OF-PLAN";
private const string SkillName = "this";

/// <summary>
/// The regular expression for extracting serialized plan.
/// </summary>
private static readonly Regex PlanRegex = new("^[^{}]*(((?'Open'{)[^{}]*)+((?'Close-Open'})[^{}]*)+)*(?(Open)(?!))", RegexOptions.Singleline | RegexOptions.Compiled);

// Planner semantic function
private readonly ISKFunction _plannerFunction;

Expand Down Expand Up @@ -221,8 +226,7 @@ No parameters.
/// <returns>Instance of <see cref="ActionPlanResponse"/> object deserialized from extracted JSON.</returns>
private ActionPlanResponse? ParsePlannerResult(SKContext plannerResult)
{
Regex planRegex = new("^[^{}]*(((?'Open'{)[^{}]*)+((?'Close-Open'})[^{}]*)+)*(?(Open)(?!))", RegexOptions.Singleline);
Match match = planRegex.Match(plannerResult.ToString());
Match match = PlanRegex.Match(plannerResult.ToString());

if (match.Success && match.Groups["Close"].Length > 0)
{
Expand Down

0 comments on commit 29f226c

Please sign in to comment.