Skip to content
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

Add analyzer for StringBuilder[int] indexer #64545

Open
Tracked by #79053
danmoseley opened this issue Jan 31, 2022 · 2 comments
Open
Tracked by #79053

Add analyzer for StringBuilder[int] indexer #64545

danmoseley opened this issue Jan 31, 2022 · 2 comments
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation area-System.Runtime code-analyzer Marks an issue that suggests a Roslyn analyzer
Milestone

Comments

@danmoseley
Copy link
Member

Indexing into a StringBuilder is not O(1) as one might expect, but O(number of chunks). StringBuilders may have few or one chunks (especially if small, or presized appropriately) or many (especially if it had many appends or grew often). Essentially, StringBuilders are intended for appending, and indexing (or removing) are generally to be avoided.

There are warnings about this in the docs
and some discussion here #24395

As mentioned, indexing is not automatically a perf trap, so an analyzer for it may be noisy, but it's likely worthwhile as it can be a significant problem in some cases, and it's not necessarily obvious. An example of a real perf bug caused by that here dotnet/msbuild#1593

If it is a problem, potential fixes might be: use something other than a StringBuilder (eg., a char buffer, a string); presize the StringBuilder appropriately; or avoid indexing into it. There is no real automatic fixer that could be written.

The analyzer implementation should simply look for use of the StringBuilder indexer, and offer an informational note if it is seen, providing some guidance.

@danmoseley danmoseley added area-System.Runtime code-analyzer Marks an issue that suggests a Roslyn analyzer labels Jan 31, 2022
@ghost
Copy link

ghost commented Jan 31, 2022

Tagging subscribers to this area: @dotnet/area-system-runtime
See info in area-owners.md if you want to be subscribed.

Issue Details

Indexing into a StringBuilder is not O(1) as one might expect, but O(number of chunks). StringBuilders may have few or one chunks (especially if small, or presized appropriately) or many (especially if it had many appends or grew often). Essentially, StringBuilders are intended for appending, and indexing (or removing) are generally to be avoided.

There are warnings about this in the docs
and some discussion here #24395

As mentioned, indexing is not automatically a perf trap, so an analyzer for it may be noisy, but it's likely worthwhile as it can be a significant problem in some cases, and it's not necessarily obvious. An example of a real perf bug caused by that here dotnet/msbuild#1593

If it is a problem, potential fixes might be: use something other than a StringBuilder (eg., a char buffer, a string); presize the StringBuilder appropriately; or avoid indexing into it. There is no real automatic fixer that could be written.

The analyzer implementation should simply look for use of the StringBuilder indexer, and offer an informational note if it is seen, providing some guidance.

Author: danmoseley
Assignees: -
Labels:

area-System.Runtime, code-analyzer

Milestone: -

@dotnet-issue-labeler dotnet-issue-labeler bot added the untriaged New issue has not been triaged by the area owner label Jan 31, 2022
@danmoseley
Copy link
Member Author

Analyzer needs to detect both set and get

@tannergooding tannergooding removed the untriaged New issue has not been triaged by the area owner label Jul 15, 2022
@tannergooding tannergooding added this to the Future milestone Jul 15, 2022
@buyaa-n buyaa-n added the api-suggestion Early API idea and discussion, it is NOT ready for implementation label Nov 9, 2022
@buyaa-n buyaa-n modified the milestones: Future, 8.0.0 Nov 11, 2022
@tannergooding tannergooding modified the milestones: 8.0.0, Future Jul 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation area-System.Runtime code-analyzer Marks an issue that suggests a Roslyn analyzer
Projects
None yet
Development

No branches or pull requests

3 participants