GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — src: refactor `SplitString` in util by anonrig · Pull Request #48491 · nodejs/node

Conversation

@anonrig

@anonrig anonrig commented Jun 18, 2023 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

See quick-bench.com

Screenshot 2023-06-18 at 12 48 39 PM
benchmark code
#include <string_view>
#include <algorithm>

// uses string::find_first_of
std::vector<std::string>
split(const std::string& str, const std::string& delims = " ")
{
	std::vector<std::string> output;
	//output.reserve(str.length() / 4);
	size_t first = 0;

	while (first < str.size())
	{
		const auto second = str.find_first_of(delims, first);

		if (first != second)
		{
			output.emplace_back(str.substr(first, second-first));
		}

		if (second == std::string::npos)
			break;

		first = second + 1;
	}

	return output;
}

std::vector<std::string>
splitStd(const std::string& str, const std::string& delims = " ")
{
	std::vector<std::string> output;
	auto first = std::cbegin(str);

	while (first != std::cend(str))
	{
		const auto second = std::find_first_of(first, std::cend(str),
			std::cbegin(delims), std::cend(delims));
		
		if (first != second)
			output.emplace_back(first, second);

		if (second == std::cend(str))
			break;

		first = std::next(second);
	}

	return output;
}

std::vector<std::string> splitPtr(const std::string& str, const std::string& delims = " ")
{
	std::vector<std::string> output;
//	output.reserve(str.size() / 2);

	for (auto first = str.data(), second = str.data(), last = first + str.size(); second != last && first != last; first = second + 1) {
		second = std::find_first_of(first, last, std::cbegin(delims), std::cend(delims));

		if (first != second)
			output.emplace_back(first, second);
	}

	return output;
}

std::vector<std::string_view>
splitSV(std::string_view strv, std::string_view delims = " ")
{
	std::vector<std::string_view> output;
	size_t first = 0;

	while (first < strv.size())
	{
		const auto second = strv.find_first_of(delims, first);
		
		if (first != second)
			output.emplace_back(strv.substr(first, second-first));

		if (second == std::string_view::npos)
			break;

		first = second + 1;
	}

	return output;
}

std::vector<std::string_view>
splitSVStd(std::string_view strv, std::string_view delims = " ")
{
	std::vector<std::string_view> output;
	//output.reserve(strv.length() / 4);
	auto first = strv.begin();

	while (first != strv.end())
	{
		const auto second = std::find_first_of(first, std::cend(strv),
			std::cbegin(delims), std::cend(delims));
		//std::cout << first << ", " << second << '\n';
		if (first != second)
		{
			output.emplace_back(strv.substr(std::distance(strv.begin(), first), std::distance(first, second)));
		}

		if (second == strv.end())
			break;

		first = std::next(second);
	}

	return output;
}

std::vector<std::string_view> splitSVPtr(std::string_view str, std::string_view delims = " ")
{
	std::vector<std::string_view> output;
	//output.reserve(str.size() / 2);

	for (auto first = str.data(), second = str.data(), last = first + str.size(); second != last && first != last; first = second + 1) {
		second = std::find_first_of(first, last, std::cbegin(delims), std::cend(delims));

		if (first != second)
			output.emplace_back(first, second - first);
	}

	return output;
}

const std::string_view LoremIpsumStrv{ "Lorem ipsum dolor sit amet, consectetur adipiscing elit,"
"sed do eiusmod tempor incididuntsuperlongwordsuper ut labore et dolore magna aliqua. Ut enim ad minim veniam, "
"quis nostrud exercitation ullamco laboris nisi ut aliquipsuperlongword ex ea commodo consequat. Duis aute"
"irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur."
"Excepteur sint occaecat cupidatatsuperlongword non proident, sunt in culpa qui officia deserunt mollit anim id est laborum." };

static void StringSplit(benchmark::State& state) {
  std::string str { LoremIpsumStrv };
  // Code inside this loop is measured repeatedly
  for (auto _ : state) {
    auto v = split(str);
    benchmark::DoNotOptimize(v);
  }
}
// Register the function as a benchmark
BENCHMARK(StringSplit);

static void StringSplitStd(benchmark::State& state) {
  std::string str { LoremIpsumStrv };
  // Code inside this loop is measured repeatedly
  for (auto _ : state) {
    auto v = splitStd(str);
    benchmark::DoNotOptimize(v);
  }
}
// Register the function as a benchmark
BENCHMARK(StringSplitStd);

static void StringSplitPtr(benchmark::State& state) {
  std::string str { LoremIpsumStrv };
  // Code inside this loop is measured repeatedly
  for (auto _ : state) {
    auto v = splitPtr(str);
    benchmark::DoNotOptimize(v);
  }
}
// Register the function as a benchmark
BENCHMARK(StringSplitPtr);

static void StringViewSplit(benchmark::State& state) {
  for (auto _ : state) {
    auto v = splitSV(LoremIpsumStrv);
    benchmark::DoNotOptimize(v);
  }
}
BENCHMARK(StringViewSplit);

static void StringViewSplitStd(benchmark::State& state) {
  for (auto _ : state) {
    auto v = splitSVStd(LoremIpsumStrv);
    benchmark::DoNotOptimize(v);
  }
}
BENCHMARK(StringViewSplitStd);

static void StringViewSplitPtr(benchmark::State& state) {
  for (auto _ : state) {
    auto v = splitSVPtr(LoremIpsumStrv);
    benchmark::DoNotOptimize(v);
  }
}
BENCHMARK(StringViewSplitPtr);

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

bricss reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
@nodejs-github-bot

nodejs-github-bot commented Jun 18, 2023

Copy link
Copy Markdown
Collaborator

Review requested:

  • @nodejs/security-wg
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@nodejs-github-bot nodejs-github-bot added c++ Issues and PRs that require attention from people who are familiar with C++. lib / src Issues and PRs involving general changes in the lib/ or src/ directories. needs-ci PRs that need a full CI run. labels Jun 18, 2023
@anonrig anonrig added the performance Issues and PRs related to the performance of Node.js. label Jun 18, 2023
@targos

targos commented Jun 18, 2023

Copy link
Copy Markdown
Member

Please at least post the benchmark results in the PR. Your link may become dead in the future

anonrig reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

RafaelGSS
RafaelGSS reviewed Jun 20, 2023
View reviewed changes
Comment thread src/permission/fs_permission.cc Resolved

Uh oh!

There was an error while loading. Please reload this page.

jasnell
jasnell approved these changes Jun 20, 2023
View reviewed changes
@anonrig anonrig added author ready PRs with CI started, the required approvals, and no outstanding review comments. request-ci Add this label to start a Jenkins CI on a PR. Only starts once the PR has an approving review. labels Jun 21, 2023
@github-actions github-actions Bot removed the request-ci Add this label to start a Jenkins CI on a PR. Only starts once the PR has an approving review. label Jun 21, 2023
@nodejs-github-bot

nodejs-github-bot commented Jun 21, 2023

Copy link
Copy Markdown
Collaborator

CI: https://ci.nodejs.org/job/node-test-pull-request/52324/

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@anonrig
src: refactor SplitString in util
b53ae22
@nodejs-github-bot

nodejs-github-bot commented Jun 22, 2023

Copy link
Copy Markdown
Collaborator

CI: https://ci.nodejs.org/job/node-test-pull-request/52350/

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@anonrig anonrig removed the author ready PRs with CI started, the required approvals, and no outstanding review comments. label Jun 22, 2023
lemire
lemire reviewed Jun 22, 2023
View reviewed changes
Comment thread src/node_options.cc Resolved

Uh oh!

There was an error while loading. Please reload this page.

lemire
lemire reviewed Jun 22, 2023
View reviewed changes
Comment thread src/node_options.cc Resolved

Uh oh!

There was an error while loading. Please reload this page.

lemire
lemire reviewed Jun 22, 2023
View reviewed changes
Comment thread src/node_options.cc Resolved

Uh oh!

There was an error while loading. Please reload this page.

lemire
lemire reviewed Jun 22, 2023
View reviewed changes
Comment thread src/node_options.cc Resolved

Uh oh!

There was an error while loading. Please reload this page.

lemire
lemire reviewed Jun 22, 2023
View reviewed changes
Comment thread src/node_options.cc Resolved

Uh oh!

There was an error while loading. Please reload this page.

lemire
lemire reviewed Jun 22, 2023
View reviewed changes
Comment thread src/node_v8_platform-inl.h Resolved

Uh oh!

There was an error while loading. Please reload this page.

lemire
lemire reviewed Jun 22, 2023
View reviewed changes
Comment thread src/permission/fs_permission.cc Resolved

Uh oh!

There was an error while loading. Please reload this page.

@anonrig
anonrig force-pushed the refactor-split-string branch from 8304083 to 27d1a87 Compare June 22, 2023 19:27
@anonrig

anonrig commented Jun 22, 2023

Copy link
Copy Markdown
Member Author

cc @nodejs/cpp-reviewers

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@anonrig
fixup! src: refactor SplitString in util
63f8f96
@anonrig
anonrig force-pushed the refactor-split-string branch from 27d1a87 to 63f8f96 Compare June 22, 2023 19:47
Qard
Qard approved these changes Jun 22, 2023
View reviewed changes
lemire
lemire approved these changes Jun 22, 2023
View reviewed changes
RafaelGSS
RafaelGSS approved these changes Jun 22, 2023
View reviewed changes
@RafaelGSS RafaelGSS added the request-ci Add this label to start a Jenkins CI on a PR. Only starts once the PR has an approving review. label Jun 22, 2023
@github-actions github-actions Bot removed the request-ci Add this label to start a Jenkins CI on a PR. Only starts once the PR has an approving review. label Jun 22, 2023
@nodejs-github-bot

nodejs-github-bot commented Jun 22, 2023

Copy link
Copy Markdown
Collaborator

CI: https://ci.nodejs.org/job/node-test-pull-request/52363/

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@anonrig anonrig added commit-queue PRs queued for automated landing through the Commit Queue. commit-queue-squash PRs the Commit Queue should land as one squashed commit. labels Jun 22, 2023
@nodejs-github-bot nodejs-github-bot removed the commit-queue PRs queued for automated landing through the Commit Queue. label Jun 22, 2023
@nodejs-github-bot
nodejs-github-bot merged commit 640a791 into nodejs:main Jun 22, 2023
@nodejs-github-bot

nodejs-github-bot commented Jun 22, 2023

Copy link
Copy Markdown
Collaborator

Landed in 640a791

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

This was referenced Jun 23, 2023
CI Reliability 2023-06-23 nodejs/reliability#596
Open
CI Reliability 2023-06-24 nodejs/reliability#597
Open
CI Reliability 2023-06-25 nodejs/reliability#598
Open
RafaelGSS pushed a commit that referenced this pull request Jul 3, 2023
@anonrig @RafaelGSS
src: refactor SplitString in util
c4b8ede
PR-URL: #48491
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Stephen Belanger <[email protected]>
Reviewed-By: Rafael Gonzaga <[email protected]>
@RafaelGSS RafaelGSS mentioned this pull request Jul 3, 2023
v20.4.0 proposal #48643
Merged
Ceres6 pushed a commit to Ceres6/node that referenced this pull request Aug 14, 2023
@anonrig @Ceres6
src: refactor SplitString in util
812a31e
PR-URL: nodejs#48491
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Stephen Belanger <[email protected]>
Reviewed-By: Rafael Gonzaga <[email protected]>
Ceres6 pushed a commit to Ceres6/node that referenced this pull request Aug 14, 2023
@anonrig @Ceres6
src: refactor SplitString in util
a57b650
PR-URL: nodejs#48491
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Stephen Belanger <[email protected]>
Reviewed-By: Rafael Gonzaga <[email protected]>
ruyadorno pushed a commit that referenced this pull request Sep 10, 2023
@anonrig @ruyadorno
src: refactor SplitString in util
ed198bf
PR-URL: #48491
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Stephen Belanger <[email protected]>
Reviewed-By: Rafael Gonzaga <[email protected]>
@ruyadorno ruyadorno mentioned this pull request Sep 10, 2023
v18.18.0 proposal #49220
Merged
ruyadorno pushed a commit that referenced this pull request Sep 13, 2023
@anonrig @ruyadorno
src: refactor SplitString in util
0d73009
PR-URL: #48491
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Stephen Belanger <[email protected]>
Reviewed-By: Rafael Gonzaga <[email protected]>
@tniessen tniessen mentioned this pull request Nov 10, 2023
src: avoid copying strings in FSPermission::Apply #50662
Merged
tniessen added a commit to tniessen/node that referenced this pull request Nov 17, 2023
@tniessen
src: avoid copying strings in FSPermission::Apply
2ee82d5
The use of string_view and subsequent copying to a string was supposed
to be a minor optimization in 640a791, however, since 413c16e, no
string splitting occurs anymore. Therefore, we can simply pass around
some references instead of using string_view or copying strings.

Refs: nodejs#48491
Refs: nodejs#49047
nodejs-github-bot pushed a commit that referenced this pull request Nov 19, 2023
@tniessen
src: avoid copying strings in FSPermission::Apply
48f32d7
The use of string_view and subsequent copying to a string was supposed
to be a minor optimization in 640a791, however, since 413c16e, no
string splitting occurs anymore. Therefore, we can simply pass around
some references instead of using string_view or copying strings.

Refs: #48491
Refs: #49047
PR-URL: #50662
Reviewed-By: Yagiz Nizipli <[email protected]>
Reviewed-By: Rafael Gonzaga <[email protected]>
Reviewed-By: Marco Ippolito <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
targos pushed a commit that referenced this pull request Nov 23, 2023
@tniessen @targos
src: avoid copying strings in FSPermission::Apply
e2862ab
The use of string_view and subsequent copying to a string was supposed
to be a minor optimization in 640a791, however, since 413c16e, no
string splitting occurs anymore. Therefore, we can simply pass around
some references instead of using string_view or copying strings.

Refs: #48491
Refs: #49047
PR-URL: #50662
Reviewed-By: Yagiz Nizipli <[email protected]>
Reviewed-By: Rafael Gonzaga <[email protected]>
Reviewed-By: Marco Ippolito <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
lucshi pushed a commit to lucshi/node that referenced this pull request Nov 27, 2023
@tniessen Lei Shi
src: avoid copying strings in FSPermission::Apply
c801888
The use of string_view and subsequent copying to a string was supposed
to be a minor optimization in 640a791, however, since 413c16e, no
string splitting occurs anymore. Therefore, we can simply pass around
some references instead of using string_view or copying strings.

Refs: nodejs#48491
Refs: nodejs#49047
PR-URL: nodejs#50662
Reviewed-By: Yagiz Nizipli <[email protected]>
Reviewed-By: Rafael Gonzaga <[email protected]>
Reviewed-By: Marco Ippolito <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
RafaelGSS pushed a commit that referenced this pull request Nov 29, 2023
@tniessen @RafaelGSS
src: avoid copying strings in FSPermission::Apply
0fb35b6
The use of string_view and subsequent copying to a string was supposed
to be a minor optimization in 640a791, however, since 413c16e, no
string splitting occurs anymore. Therefore, we can simply pass around
some references instead of using string_view or copying strings.

Refs: #48491
Refs: #49047
PR-URL: #50662
Reviewed-By: Yagiz Nizipli <[email protected]>
Reviewed-By: Rafael Gonzaga <[email protected]>
Reviewed-By: Marco Ippolito <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
UlisesGascon pushed a commit that referenced this pull request Dec 11, 2023
@tniessen @UlisesGascon
src: avoid copying strings in FSPermission::Apply
0e7acf9
The use of string_view and subsequent copying to a string was supposed
to be a minor optimization in 640a791, however, since 413c16e, no
string splitting occurs anymore. Therefore, we can simply pass around
some references instead of using string_view or copying strings.

Refs: #48491
Refs: #49047
PR-URL: #50662
Reviewed-By: Yagiz Nizipli <[email protected]>
Reviewed-By: Rafael Gonzaga <[email protected]>
Reviewed-By: Marco Ippolito <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
UlisesGascon pushed a commit that referenced this pull request Dec 19, 2023
@tniessen @UlisesGascon
src: avoid copying strings in FSPermission::Apply
d08eb38
The use of string_view and subsequent copying to a string was supposed
to be a minor optimization in 640a791, however, since 413c16e, no
string splitting occurs anymore. Therefore, we can simply pass around
some references instead of using string_view or copying strings.

Refs: #48491
Refs: #49047
PR-URL: #50662
Reviewed-By: Yagiz Nizipli <[email protected]>
Reviewed-By: Rafael Gonzaga <[email protected]>
Reviewed-By: Marco Ippolito <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@Qard Qard Qard approved these changes
@lemire lemire lemire approved these changes
@jasnell jasnell jasnell approved these changes
@RafaelGSS RafaelGSS RafaelGSS approved these changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

c++ Issues and PRs that require attention from people who are familiar with C++. commit-queue-squash PRs the Commit Queue should land as one squashed commit. lib / src Issues and PRs involving general changes in the lib/ or src/ directories. needs-ci PRs that need a full CI run. performance Issues and PRs related to the performance of Node.js.

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

7 participants

@anonrig @nodejs-github-bot @targos @Qard @lemire @jasnell @RafaelGSS
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.