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

feat: Arraylist, fast new container to add front and back #1677

Merged
merged 6 commits into from
Oct 19, 2023
Merged

Conversation

mehah
Copy link
Contributor

@mehah mehah commented Oct 4, 2023

Arraylist is a very fast container for adding to the front and back, it uses two vectors to do this juggling and doesn't allow you to remove the front, as it is slow, use std::deque for this case.

Benchmark
loop: 99999
image
loop: 999999
image

constexpr size_t LOOP = 999999;

Benchmark bm;
for (size_t i = 0; i < 999; i++) {
	stdext::arraylist<uint32_t> list;
	list.reserve(LOOP);

	for (size_t d = 0; d < LOOP; d++) {
		if (d % 2) {
			list.emplace_front(d);
		} else {
			list.emplace_back(d);
		}
	}
	for (const auto &v : list) { }
}

g_logger().info("stdext::arraylist + reserve: {}ms", bm.duration());

bm.reset();
bm.start();

for (size_t i = 0; i < 999; i++) {
	stdext::arraylist<uint32_t> list;
	for (size_t d = 0; d < LOOP; d++) {
		if (d % 2) {
			list.emplace_front(d);
		} else {
			list.emplace_back(d);
		}
	}
	for (const auto &v : list) { }
}

g_logger().info("stdext::arraylist: {}ms", bm.duration());

bm.reset();
bm.start();

for (size_t i = 0; i < 999; i++) {
	std::list<uint32_t> list;
	for (size_t d = 0; d < LOOP; d++) {
		if (d % 2) {
			list.emplace_front(d);
		} else {
			list.emplace_back(d);
		}
	}
	for (const auto &v : list) { }
}

g_logger().info("std::list: {}ms", bm.duration());

bm.reset();
bm.start();

for (size_t i = 0; i < 999; i++) {
	std::deque<uint32_t> list;
	for (size_t d = 0; d < LOOP; d++) {
		if (d % 2) {
			list.emplace_front(d);
		} else {
			list.emplace_back(d);
		}
	}
	for (const auto &v : list) { }
}

g_logger().info("std::deque: {}ms", bm.duration());

g_logger().info("--- stdext::arraylist vs std::forward_list vs std::list vs std::deque --- (push_front)");

bm.reset();
bm.start();

for (size_t i = 0; i < 999; i++) {
	stdext::arraylist<uint32_t> list;
	list.reserve(LOOP);
	for (size_t d = 0; d < LOOP; d++) {
		list.emplace_front(d);
	}
	for (const auto &v : list) { }
}

g_logger().info("stdext::arraylist + reserve: {}ms", bm.duration());

bm.reset();
bm.start();

for (size_t i = 0; i < 999; i++) {
	stdext::arraylist<uint32_t> arraylist;
	for (size_t d = 0; d < LOOP; d++) {
		arraylist.emplace_front(d);
	}
	for (const auto &v : arraylist) { }
}

g_logger().info("stdext::arraylist: {}ms", bm.duration());

bm.reset();
bm.start();

for (size_t i = 0; i < 999; i++) {
	std::forward_list<uint32_t> list;
	for (size_t d = 0; d < LOOP; d++) {
		list.emplace_front(d);
	}
	for (const auto &v : list) { }
}

g_logger().info("std::forward_list: {}ms", bm.duration());

bm.reset();
bm.start();

for (size_t i = 0; i < 999; i++) {
	std::list<uint32_t> list;
	for (size_t d = 0; d < LOOP; d++) {
		list.emplace_front(d);
	}
	for (const auto &v : list) { }
}

g_logger().info("std::list: {}ms", bm.duration());

bm.reset();
bm.start();

for (size_t i = 0; i < 999; i++) {
	std::deque<uint32_t> list;
	for (size_t d = 0; d < LOOP; d++) {
		list.emplace_front(d);
	}
	for (const auto &v : list) { }
}

g_logger().info("std::deque: {}ms", bm.duration());

Validating Content

constexpr size_t LOOP = 999999;
stdext::arraylist<uint32_t> arraylist;
arraylist.reserve(LOOP);

for (size_t d = 0; d < LOOP; d++) {
	if (d % 2) {
		arraylist.emplace_front(d);
	} else {
		arraylist.emplace_back(d);
	}
}

std::deque<uint32_t> deque;
for (size_t d = 0; d < LOOP; d++) {
	if (d % 2) {
		deque.emplace_front(d);
	} else {
		deque.emplace_back(d);
	}
}

for (size_t i = 0; i < LOOP; i++) {
	if (arraylist[i] != deque[i]) {
		g_logger().info("NOT EQUAL");
	}
}

g_logger().info("FINISHED");

@luan luan merged commit afcae2c into main Oct 19, 2023
@luan luan deleted the arraylist branch October 19, 2023 23:00
@sonarqubecloud
Copy link

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

0.0% 0.0% Coverage
0.0% 0.0% Duplication

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants