Code Optimizations extensions for Visual Studio Code (preview)
With the Code Optimizations extension for Visual Studio Code, you can generate a code fix proposal for performance issues identified by Code Optimizations in your running. NET apps.
This article guides you through using the extension with GitHub Copilot in Visual Studio Code.
Prerequisites
- Install the latest version of Visual Studio Code
- Sign up for and log into a GitHub account with a valid GitHub Copilot subscription.
- Install the GitHub Copilot Chat extension.
- Enable the following services for your .NET application:
Install the Code Optimizations extension (preview)
Install the Code Optimizations extension for Visual Studio Code.
When you run the extension for the first time, you might see two dialogue prompts asking you to sign into Azure and connect with your GitHub Copilot account.
Fix performance issues in your code using the extension
Once installed, the Code Optimizations extension (preview) introduces an agent called @code_optimizations
with a few commands or "skills" that you can use in GitHub Copilot to interface with Code Optimizations issues.
- In Visual Studio Code, open the repo holding your .NET application with Code Optimizations enabled.
- Open the GitHub Copilot chat.
You have two options for using the Code Optimizations extension in Visual Studio Code:
Option 1: With Code Optimizations
While using the extension with Code Optimizations enabled on your application requires more setup steps, you receive more accurate fix suggestions in your code.
Verify Code Optimizations for your application
To get started, make sure Code Optimizations are identified for your application.
In the Azure portal, navigate to your Application Insights resource.
Select Investigate > Performance.
In the Performance blade, select the Code Optimizations button in the top menu.
Make note of:
- The Application Insights resource name.
- The account with which you're signed into the Azure portal.
Get the Application ID for your app
In the portal, navigate to your Application Insights resource.
Select Configure > API Access.
Make note of your Application ID at the top of the API Access pane.
Invoke the extension
Invoke the extension by executing the following command, replacing the placeholder with the Application ID you saved earlier.
@code_optimizations /connect <YOUR_APPLICATION_ID>
The command pulls the top issues from Code Optimizations, maps them to source code in your local repo, and suggests fixes/recommendations. It automatically generates the top recommendation.
Generate fixes for other issues by following the prompts in the Copilot response.
Optimize your code
Aside from the @code-optimizations /connect
command, you can also use the /optimize
commands to resolve issues in your code. The extension provides two ways to use the /optimize
command.
@code-optimizations /optimize <GITHUB_ISSUE_NUMBER>
Call
/optimize
along with the GitHub issue number created by Code Optimizations service in the Azure portal. In the following example, "5" represents the GitHub issue number that you'd like to fix.@code-optimizations /optimize 5
The command:
- Pulls in the body of the issue, which includes the call stack, CPU usage, etc.
- Uses the Code Optimizations model to generate a code fix. This action may take some time.
Once the code fix is generated, click the Compare With Original button to review the suggested fix alongside the original.
Make any other changes to the code fix using the inline chat option. For example, ask Copilot to:
Update method name from
<A>
to<B>
.Use
API X
instead ofAPI Y
, etc.
Click Accept Fix once you're ready.
Code selection + @code-optimizaitons /optimize
Directly trigger /optimize
on selected code. Using this method, you can proactively optimize some code you think has a performance issue.
- Select potentially problematic code, or hover your cursor in a method with performance issues.
- Call the
/optimize
command in the Copilot chat.
You can then run benchmark/load tests to ensure the changes improve performance, and unit tests to ensure code semantics are preserved.
Option 2: Without Code Optimizations
You can use the Visual Studio Code extension without Code Optimizations enabled. While this method requires less setup time, you may receive less accurate fix suggestions.
Select potentially problematic code, or hover your cursor in a method with performance issues.
Call the
/optimize
command in the Copilot chat.