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

Minimize allocations and avoid rebuilding weighted list when retrieving potential spawns #732

Merged
merged 1 commit into from
Mar 24, 2024

Conversation

embeddedt
Copy link
Member

The current implementation of EventHooks.getPotentialSpawns is rather inefficient - it fires the event, then constructs an entirely new weighted random list from the event's results, even if no mods changed the potential spawns. This is not very efficient and leads to a lot of allocation overhead beyond the vanilla code, as shown here:

potentialspawns

This PR solves the issue by making a few changes:

  • LevelEvent.PotentialSpawns now allocates its backing list lazily, rather than in the constructor.
  • EventHooks now takes advantage of that change to reuse the existing weighted list from vanilla if no mods modify potential spawns.

With these changes, the only allocation overhead left is that of allocating the LevelEvent.PotentialSpawns object itself. Unfortunately, that still shows up in the allocation profiler, but solving it is not really possible unless we move to pooling hot events like this.

@embeddedt embeddedt added performance Related to performance 1.20.4 Targeted at Minecraft 1.20.4 labels Mar 20, 2024
@neoforged-pr-publishing
Copy link

neoforged-pr-publishing bot commented Mar 20, 2024

  • Publish PR to GitHub Packages

Last commit published: cdc8ec88b828a4a3c035f88ede142f9f7b63c49d.

PR Publishing

The artifacts published by this PR:

Repository Declaration

In order to use the artifacts published by the PR, add the following repository to your buildscript:

repositories {
    maven {
        name 'Maven for PR #732' // https://github.com/neoforged/NeoForge/pull/732
        url 'https://prmaven.neoforged.net/NeoForge/pr732'
        content {
            includeModule('net.neoforged', 'neoforge')
            includeModule('net.neoforged', 'testframework')
        }
    }
}

MDK installation

In order to setup a MDK using the latest PR version, run the following commands in a terminal.
The script works on both *nix and Windows as long as you have the JDK bin folder on the path.
The script will clone the MDK in a folder named NeoForge-pr732.
On Powershell you will need to remove the -L flag from the curl invocation.

mkdir NeoForge-pr732
cd NeoForge-pr732
curl -L https://prmaven.neoforged.net/NeoForge/pr732/net/neoforged/neoforge/20.4.212-pr-732-potentialspawns-alloc/mdk-pr732.zip -o mdk.zip
jar xf mdk.zip
rm mdk.zip || del mdk.zip

To test a production environment, you can download the installer from here.

@marchermans marchermans merged commit 14c4194 into neoforged:1.20.x Mar 24, 2024
4 checks passed
@embeddedt embeddedt deleted the potentialspawns-alloc branch March 24, 2024 18:31
ApexModder pushed a commit to ApexModder/NeoForge that referenced this pull request May 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
1.20.4 Targeted at Minecraft 1.20.4 performance Related to performance
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants