microsoft / microsoft/STL

`<xstring>`: `basic_string` could handle insert aliasing without allocating in some common cases

Open
#2,944 4 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

performance
Dominant language
C++
Stars
11.1k
Forks
1.7k
Avg merge
4d 15h
Merged PRs (30d)
22

Description

Notably:

  1. Given a range of contiguous iterators we can directly determine if it aliases the string contents.
  2. Given a sized or forward range that will fit in available capacity, we could append and then rotate (which can't throw) into place to effect inserts even in the presence of aliasing.
  3. For sizeable-but-won't-fit, we could allocate the new memory, insert the new range in place, and then move (which can't throw) the pre-existing characters.
  4. For non-sizeable, fill the tail, either rotating if the capacity suffices or shifting into the correct places when we do allocate a new block.

Note that these are roughly in order of ease of implementation and expected improvement. The more exotic the workaround, the less improvement we can expect over the dumb "make a temporary string and then merge" strategy.

(Inspired by discussion in https://github.com/microsoft/STL/pull/2806#discussion_r918399814.)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading the basic_string insert aliasing discussion in the referenced STL pull request and compare it with the approaches outlined here. Investigate which of the four cases can be handled without a temporary string, then establish performance measurements before deciding which optimization is worthwhile.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
performance
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.