Skip to content

Repo sync for protected CLA branch #8328

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Aug 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions docs/profiling/perf-insights-string-concatenation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
---
title: Performance insights for string concatenations
description: Learn how to improve performance for string concatenations.
ms.date: 7/20/2022
ms.topic: reference
author: mikejo5000
ms.author: mikejo
manager: jmartens
ms.technology: vs-ide-debug
ms.workload:
- multiple
---

# Use StringBuilder for concatenations

This article describes performance insights for string concatenations.

## Cause

Calls to System.String.Concat are a significant proportion of the profiling data. Consider using the <xref:System.Text.StringBuilder> class to construct strings from multiple segments.

## Performance insight description

A <xref:System.String> object is immutable. Therefore, any modification to the string creates a new string object and the garbage collection of the original. This behavior is the same whether you call String.Concat explicitly or use the string concatenation operators such as + or +=. Program performance can decrease if these methods are frequently called, such as when characters are added to a string in a tight loop.

The StringBuilder class is a mutable object, and, unlike System.String, most of the methods on StringBuilder that modify an instance of this class return a reference to that same instance. You can insert characters or append text to a StringBuilder instance, and remove or replace characters in the instance without the need for allocating a new instance and deleting the original instance.

## How to investigate a warning

In the CPU Usage tool, click **View source code** to go to the [call tree](../profiling/cpu-usage.md#BKMK_Call_tree_structure) and source line highlighting view showing where the most amount of time is spent in string concatenations. If the problem is occurring in multiple locations within the application, navigate to the caller/callee view to see each call location.
10 changes: 8 additions & 2 deletions docs/profiling/toc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,14 @@
href: analyze-database.md
- name: Events Viewer
href: events-viewer.md
- name: CPU usage
href: cpu-usage.md
- name: CPU Usage
items:
- name: CPU usage
href: cpu-usage.md
- name: Performance insights
items:
- name: String concatenation
href: perf-insights-string-concatenation.md
- name: File IO
href: use-file-io.md
- name: Memory usage
Expand Down