Search⌘ K
AI Features

Parallel Data Conversion

Explore how to enhance CSV data conversion by using C++17's parallel execution policies. Understand the need for preallocating vectors over back inserters to avoid threading issues. Learn to apply std::execution::par correctly with std::transform to improve performance by parallelizing independent data parsing operations.

We'll cover the following...

Using std::execution::par

As previously discussed, we can add parallel execution to the place where we convert the data. We have lots of lines to parse, each parsing is independent.

C++
[[nodiscard]] std::vector<OrderRecord>
LinesToRecords(const std::vector<std::string_view>& lines)
{
std::vector<OrderRecord> outRecords(lines.size());
std::transform(std::execution::par, std::begin(lines), std::end(lines),
std::begin(outRecords), LineToRecord);
return outRecords;
}

Two things needed to change to the serial version

  • we need
...