Skip to content

Commit

Permalink
Zephyr: Use EASTL's hash_map over std::unordered_map in hotspots
Browse files Browse the repository at this point in the history
  • Loading branch information
fleroviux committed Jun 4, 2024
1 parent 2841e91 commit cd32996
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 10 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@
[submodule "external/json"]
path = external/json
url = https://github.com/nlohmann/json
[submodule "external/EASTL"]
path = external/EASTL
url = https://github.com/ZephyrEngine/EASTL
1 change: 1 addition & 0 deletions external/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@

add_subdirectory(EASTL)
add_subdirectory(fmt)

set(JSON_BuildTests OFF CACHE INTERNAL "")
Expand Down
1 change: 1 addition & 0 deletions external/EASTL
Submodule EASTL added at 651f3a
3 changes: 2 additions & 1 deletion zephyr/common/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

set(SOURCES
src/eastl.cpp
src/panic.cpp
)

Expand All @@ -25,4 +26,4 @@ set(HEADERS_PUBLIC
add_library(zephyr-common ${SOURCES} ${HEADERS} ${HEADERS_PUBLIC})

target_include_directories(zephyr-common PUBLIC include)
target_link_libraries(zephyr-common PUBLIC fmt zephyr-cxx-opts)
target_link_libraries(zephyr-common PUBLIC fmt zephyr-cxx-opts EASTL)
9 changes: 9 additions & 0 deletions zephyr/common/src/eastl.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include <cstddef>

void* operator new[](std::size_t size, const char* name, int flags, unsigned debugFlags, const char* file, int line) {
return ::operator new(size);
}

void* operator new[](std::size_t size, std::size_t alignment, std::size_t alignmentOffset, const char* pName, int flags, unsigned debugFlags, const char* file, int line) {
return ::operator new(size);
}
4 changes: 2 additions & 2 deletions zephyr/renderer/include/zephyr/renderer/render_engine.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
#include <zephyr/renderer/resource/geometry.hpp>
#include <zephyr/scene/scene_graph.hpp>
#include <zephyr/scene/scene_node.hpp>
#include <EASTL/hash_map.h>
#include <atomic>
#include <optional>
#include <semaphore>
#include <thread>
#include <typeindex>
#include <unordered_map>
#include <vector>

namespace zephyr {
Expand Down Expand Up @@ -72,7 +72,7 @@ namespace zephyr {
// Representation of the scene graph that is internal to the render engine.
std::vector<SceneNodeMeshData> m_scene_node_mesh_data{};
std::vector<RenderCamera> m_scene_node_camera_data{};
std::unordered_map<const SceneNode*, SceneNodeData> m_scene_node_data{};
eastl::hash_map<const SceneNode*, SceneNodeData> m_scene_node_data{};

std::vector<RenderObject> m_render_objects{};
RenderCamera m_render_camera{};
Expand Down
14 changes: 9 additions & 5 deletions zephyr/renderer/src/render_engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,15 @@ namespace zephyr {
}

void RenderEngine::PatchNodeComponentRemoved(SceneNode* node, std::type_index component_type) {
if(!m_scene_node_data.contains(node)) {
const auto node_data_match = m_scene_node_data.find(node);

if(node_data_match == m_scene_node_data.end()) {
return;
}

SceneNodeData& node_data = node_data_match->second;

if(component_type == typeid(MeshComponent)) {
SceneNodeData& node_data = m_scene_node_data[node];
if(node_data.mesh_data_id.has_value()) {
// TODO(fleroviux): this breaks indices in other SceneNodeData
m_scene_node_mesh_data.erase(m_scene_node_mesh_data.begin() + node_data.mesh_data_id.value());
Expand All @@ -124,7 +127,6 @@ namespace zephyr {
}

if(component_type == typeid(PerspectiveCameraComponent)) {
SceneNodeData& node_data = m_scene_node_data[node];
if(node_data.camera_data_id.has_value()) {
// TODO(fleroviux): this breaks indices in other SceneNodeData
m_scene_node_camera_data.erase(m_scene_node_camera_data.begin() + node_data.camera_data_id.value());
Expand All @@ -138,11 +140,13 @@ namespace zephyr {
}

void RenderEngine::PatchNodeTransformChanged(SceneNode* node) {
if(!m_scene_node_data.contains(node)) {
const auto node_data_match = m_scene_node_data.find(node);

if(node_data_match == m_scene_node_data.end()) {
return;
}

const SceneNodeData& node_data = m_scene_node_data[node];
const SceneNodeData& node_data = node_data_match->second;

if(node_data.mesh_data_id.has_value()) {
m_scene_node_mesh_data[node_data.mesh_data_id.value()].local_to_world = node->GetTransform().GetWorld();
Expand Down
4 changes: 2 additions & 2 deletions zephyr/scene/include/zephyr/scene/scene_graph.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
#pragma once

#include <zephyr/integer.hpp>
#include <EASTL/hash_map.h>
#include <memory>
#include <span>
#include <vector>
#include <unordered_map>
#include <typeindex>

namespace zephyr {
Expand Down Expand Up @@ -63,7 +63,7 @@ namespace zephyr {
std::shared_ptr<SceneNode> m_root_node{};
std::vector<SceneNode*> m_pending_nodes_with_dirty_transforms{};
std::vector<SceneNode*> m_nodes_with_dirty_transform{};
std::unordered_map<const SceneNode*, bool> m_node_world_visibility{}; //< Tracks the world visibility of nodes
eastl::hash_map<const SceneNode*, bool> m_node_world_visibility{}; //< Tracks the world visibility of nodes
std::vector<ScenePatch> m_scene_patches{};
};

Expand Down

0 comments on commit cd32996

Please sign in to comment.