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

Boost performance by reducing the update frequency #31

Open
Qendolin opened this issue Oct 3, 2021 · 1 comment
Open

Boost performance by reducing the update frequency #31

Qendolin opened this issue Oct 3, 2021 · 1 comment

Comments

@Qendolin
Copy link
Contributor

Qendolin commented Oct 3, 2021

Atmosfera causes quite a hit on performance due to constant memory allocation and hash map merging. This could easily be improved by reducing the update frequency (specifically of atmosfera$updateEnvironmentContext) since the environment does not change so quickly. It would be nice to have an option for the amount of ticks between each update.
I.e.: A slider from 1 to 100 with a default value of 10.

Maybe 10 ticks / update is still to much, 20 ticks could be more than enough.

@Qendolin
Copy link
Contributor Author

This could be implemented like this:

public class AtmosphericSoundHandler {
    private int tick;
  
    private void tickSounds() {
//      ClientWorld world = MinecraftClient.getInstance().world;

//      if (world != null) {
//          SoundManager soundManager = MinecraftClient.getInstance().getSoundManager();

            if(tick % AtmosferaConfig.getUpdateInterval() == 0) {
                ((ClientWorldDuck) world).atmosfera$updateEnvironmentContext();
            }

//          for (AtmosphericSound definition : this.sounds) {
//              if (!this.soundInstances.containsKey(definition) || this.soundInstances.get(definition).isDone()) {
//                  float volume = definition.getVolume(world);
//
//                  // The non-zero volume prevents the events getting triggered multiple times at volumes near zero.
//                  if (volume >= 0.0125 && MinecraftClient.getInstance().options.getSoundVolume(SoundCategory.AMBIENT) > 0) {
//                      AtmosphericSoundInstance soundInstance = new AtmosphericSoundInstance(definition, 0.0001F);
//                      this.soundInstances.put(definition, soundInstance);
//                      soundManager.playNextTick(soundInstance);
//                      Atmosfera.debug("volume > 0: {} - {}", definition.id(), volume);
//                  }
//              }
//          }
//      }
//
//      this.soundInstances.values().removeIf(AtmosphericSoundInstance::isDone);
        tick++;
    }

And a few additions to AtmosferaConfig:

...

private static int UPDATE_INTERVAL = 20;

private static void read() {
    ...

    if(json.has("misc")) {
        JsonObject misc = json.getAsJsonObject("misc");
        if (misc.has("update_interval")) {
	    UPDATE_INTERVAL = misc.get("update_interval").getAsInt();
        }
    }

    ...
}

private static void write() {
    ...

    JsonObject misc = new JsonObject();
    misc.addProperty("update_interval", UPDATE_INTERVAL);
    jsonObject.add("misc", misc);

    ...
}

public static Screen getScreen(Screen parent) {
    ...

    ConfigCategory miscCategory = builder.getOrCreateCategory(new TranslatableText("config.category.atmosfera.misc"));
    miscCategory.addEntry(builder.entryBuilder().startIntSlider(
        new TranslatableText("config.value.atmosfera.update_interval"), UPDATE_INTERVAL, 1, 100)
        .setSaveConsumer(integer -> UPDATE_INTERVAL = integer)
        .setDefaultValue(20)
        .build()
    );

    ...
}

public static int getUpdateInterval() {
    return UPDATE_INTERVAL;
}

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

No branches or pull requests

1 participant