Skip to content

Commit

Permalink
[#73] perf(mesh): use sorted vector instead of unordered_set for …
Browse files Browse the repository at this point in the history
…leaf polys

This patch improves performance for MinGW-w64 specifically. Vanilla GCC and Clang are unaffected by this patch apart from debug builds with sanitizers enabled which are slightly slower.

Closes: #71
  • Loading branch information
Try committed Jul 23, 2023
1 parent 0e7e507 commit 33f6f6d
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 6 deletions.
4 changes: 2 additions & 2 deletions include/phoenix/mesh.hh
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ namespace phoenix {
/// \throws parser_error if parsing fails.
/// \see #parse(buffer&&, const std::vector<std::uint32_t>&)
[[nodiscard]] PHOENIX_API static mesh
parse(buffer& buf, std::optional<std::unordered_set<std::uint32_t>> const& include_polygons = std::nullopt);
parse(buffer& buf, std::vector<uint32_t> const& include_polygons = {});

/// \brief Parses a mesh from the data in the given buffer.
///
Expand All @@ -103,7 +103,7 @@ namespace phoenix {
/// \throws parser_error if parsing fails.
/// \see #parse(buffer&, const std::vector<std::uint32_t>&)
[[nodiscard]] PHOENIX_API inline static mesh
parse(buffer&& buf, std::optional<std::unordered_set<std::uint32_t>> const& include_polygons = std::nullopt) {
parse(buffer&& buf, std::vector<std::uint32_t> const & include_polygons = {}) {
return mesh::parse(buf, include_polygons);
}

Expand Down
2 changes: 1 addition & 1 deletion include/phoenix/world/bsp_tree.hh
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ namespace phoenix {
std::vector<std::uint32_t> polygon_indices;

/// \brief All BSP leaf polygon indices.
std::unordered_set<std::uint32_t> leaf_polygons;
std::vector<std::uint32_t> leaf_polygons;

/// \brief All BSP light points.
std::vector<glm::vec3> light_points;
Expand Down
4 changes: 2 additions & 2 deletions source/mesh.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ namespace phoenix {
end = 0xB060
};

mesh mesh::parse(buffer& buf, std::optional<std::unordered_set<std::uint32_t>> const& leaf_polygons) {
mesh mesh::parse(buffer& buf, std::vector<std::uint32_t> const& leaf_polygons) {
mesh msh {};

std::uint16_t version {};
Expand Down Expand Up @@ -125,7 +125,7 @@ namespace phoenix {
// This presents a problem: Taking the leaf polygons as a parameter makes creating a unified
// parsing function for world meshes impossible. Instead, there should be a function to remove
// this extra data which would grant the user more freedom in how they use _phoenix_.
if (leaf_polygons && leaf_polygons->find(i) == leaf_polygons->end()) {
if (!leaf_polygons.empty() && !std::binary_search(leaf_polygons.begin(), leaf_polygons.end(), i)) {
// If the current polygon is not a leaf polygon, skip it.
chunk.skip((version == mesh_version_g2 ? 8 : 6) * vertex_count);
continue;
Expand Down
3 changes: 2 additions & 1 deletion source/world/bsp_tree.cc
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,10 @@ namespace phoenix {
auto& node = bsp.nodes[idx];

for (uint32_t i = 0; i < node.polygon_count; ++i) {
bsp.leaf_polygons.insert(bsp.polygon_indices[node.polygon_index + i]);
bsp.leaf_polygons.push_back(bsp.polygon_indices[node.polygon_index + i]);
}
}
std::sort(bsp.leaf_polygons.begin(), bsp.leaf_polygons.end());

assert(node_count == bsp.nodes.size());
assert(leaf_count == bsp.leaf_node_indices.size());
Expand Down

0 comments on commit 33f6f6d

Please sign in to comment.