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

refactor: spectators system #1645

Merged
merged 9 commits into from
Oct 3, 2023
Merged

refactor: spectators system #1645

merged 9 commits into from
Oct 3, 2023

Conversation

mehah
Copy link
Contributor

@mehah mehah commented Sep 27, 2023

This refactoring consists of:

1- Improved caching algorithm
2- Applied pipeline concept
3- Use of vector_set instead of hashset.

code sample:

// [OLD]
SpectatorHashSet spectators;
getSpectators(spectators, Position(x, y, z), ...);
for(const auto spec: spectators) {}
// [NEW]

// Get All Creatures
Spectators().find<Creature>(Position(x, y, z), ...);

// Get All Players
Spectators().find<Player>(Position(x, y, z), ...);

// Method Filter
// Note: This method returns a new instance of spectators
{
  auto spectators = Spectators().find<Creature>(Position(x, y, z), ...);
  
  // Get All Npcs from spectators variable
  auto npcSpectators = spectators.filter<Npc>();
  
  // Get All Players from spectators variable
  auto playerSpectators = spectators.filter<Player>();
}

// Pipeline Code
{
  auto spectators = Spectators()
    .find<Creature>({100, 200, 100})
    .find<Player>({110, 210, 110})
    .find<Creature>({99, 101, 50});
  
  for(const auto spec : spectators) {}
  for(const auto spec : spectators.filter<Monster>()) {}
  for(const auto spec : spectators.filter<Npc>()) {}
}

[BENCHMARK] (Set SPECTATORS_USE_HASHSET if you want to benchmark with 'hashset'.)

Vector
image

Hashset
hashset_cache

[BENCHMARK CODE]

void Map::moveCreature(Creature &creature, Tile &newTile, bool forceTeleport /* = false*/) {
	.
	.
	.
	auto spectators = Spectators()
				  .find<Creature>(oldPos, true)
				  .find<Creature>(newPos, true);

	const int64_t start = OTSYS_TIME();

	for (int_fast32_t i = -1; ++i < 999999;) {
		for (const auto spec : Spectators()
						.find<Creature>(oldPos, true)
						.find<Creature>(newPos, true)) { }
	}

	g_logger().info("Benchmark std::vector({}) | cache enabled: {}ms", spectators.size(), static_cast<double>(OTSYS_TIME() - start) / 1000.f);
	.
	.
	.
}

@mehah mehah requested review from luan and dudantas and removed request for luan September 28, 2023 22:57
@sonarqubecloud
Copy link

sonarqubecloud bot commented Oct 3, 2023

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 35 Code Smells

0.0% 0.0% Coverage
1.9% 1.9% Duplication

@luan luan merged commit 1e1fd0c into main Oct 3, 2023
30 checks passed
@luan luan deleted the refactor-spectators-system branch October 3, 2023 19:48
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.

3 participants