Skip to content

Reduce CPU overhead in pathfinding, entity logic, and shadow rendering#1047

Open
starseed12345 wants to merge 1 commit intosmartcmd:mainfrom
starseed12345:main
Open

Reduce CPU overhead in pathfinding, entity logic, and shadow rendering#1047
starseed12345 wants to merge 1 commit intosmartcmd:mainfrom
starseed12345:main

Conversation

@starseed12345
Copy link

Description

This PR introduces several CPU overhead optimizations in pathfinding, entity logic, and entity shadow rendering.
The changes reduce repeated smart pointer operations and avoid redundant world lookups during shadow rendering.

Changes
Previous Behavior

serverAiStep repeatedly called shared_from_this() inside the function.

Entity logic performed unnecessary shared_ptr operations in hot paths.

Shadow rendering repeatedly queried Level::getTile and Level::getRawBrightness for the same blocks during a single frame.

These operations added unnecessary CPU overhead during entity updates and rendering.

Root Cause

Repeated shared_from_this() calls caused avoidable reference counting overhead.

Some entity rendering paths unnecessarily copied shared_ptr values.

Shadow rendering performed redundant world queries inside nested loops.

New Behavior

shared_from_this() is cached once in serverAiStep and reused.

Several entity functions reduce unnecessary shared_ptr overhead.

Shadow rendering caches tile ID and brightness values per block during a frame.

This reduces repeated computations and improves CPU efficiency in both entity logic and rendering.

Fix Implementation

Cached shared_from_this() at the beginning of serverAiStep and reused the result.

Reduced unnecessary shared_ptr copies in entity-related code paths.

Added shadowTileCache in EntityRenderDispatcher to cache:

tile ID

brightness

Introduced beginFrame() to clear the cache once per frame.

Updated renderShadow to reuse cached tile data instead of performing repeated world lookups.

AI Use Disclosure

AI was used to make the description of this pull request

Related Issues
None

- Cache shared_from_this() in serverAiStep
- Reduce shared_ptr overhead in entities
- Add shadow tile cache to optimize entity shadow rendering
@starseed12345
Copy link
Author

I'd also like it if someone could compare the performance results/cpu usage and let me know if i did any mistakes in the code

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant