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

Don't cache the blockstate twice, speeds up connecting to servers with many mods #7496

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions patches/minecraft/net/minecraft/block/AbstractBlock.java.patch
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,18 @@
public abstract static class AbstractBlockState extends StateHolder<Block, BlockState> {
private final int field_215708_d;
private final boolean field_215709_e;
@@ -428,14 +439,18 @@
@@ -388,6 +399,10 @@

}

+ public void clearCache() {
+ this.field_215707_c = null;
+ }
+
public Block func_177230_c() {
return this.field_235892_c_;
}
@@ -428,14 +443,18 @@
return this.field_215708_d;
}

Expand All @@ -100,7 +111,7 @@
public BlockState func_185907_a(Rotation p_185907_1_) {
return this.func_177230_c().func_185499_a(this.func_230340_p_(), p_185907_1_);
}
@@ -802,6 +817,9 @@
@@ -802,6 +821,9 @@
private ResourceLocation field_222381_j;
private boolean field_226895_m_ = true;
private boolean field_235813_o_;
Expand All @@ -110,7 +121,7 @@
private AbstractBlock.IExtendedPositionPredicate<EntityType<?>> field_235814_p_ = (p_235832_0_, p_235832_1_, p_235832_2_, p_235832_3_) -> {
return p_235832_0_.func_224755_d(p_235832_1_, p_235832_2_, Direction.UP) && p_235832_0_.func_185906_d() < 14;
};
@@ -863,6 +881,8 @@
@@ -863,6 +885,8 @@
abstractblock$properties.field_226895_m_ = p_200950_0_.field_235684_aB_.field_226895_m_;
abstractblock$properties.field_235813_o_ = p_200950_0_.field_235684_aB_.field_235813_o_;
abstractblock$properties.field_235806_h_ = p_200950_0_.field_235684_aB_.field_235806_h_;
Expand All @@ -119,7 +130,7 @@
return abstractblock$properties;
}

@@ -877,6 +897,24 @@
@@ -877,6 +901,24 @@
return this;
}

Expand All @@ -144,7 +155,7 @@
public AbstractBlock.Properties func_200941_a(float p_200941_1_) {
this.field_200961_i = p_200941_1_;
return this;
@@ -932,11 +970,17 @@
@@ -932,11 +974,17 @@
return this;
}

Expand Down
10 changes: 8 additions & 2 deletions src/main/java/net/minecraftforge/registries/ForgeRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -518,10 +518,16 @@ void validateContent(ResourceLocation registryName)
}
}

public void bake()
@Deprecated
//TODO 1.17: Remove
public void bake() {
bake(null);
}

public void bake(BakeReason reason)
{
if (this.bake != null)
this.bake.onBake(this, this.stage);
this.bake.onBake(this, this.stage, reason);
}

void sync(ResourceLocation name, ForgeRegistry<V> from)
Expand Down
24 changes: 19 additions & 5 deletions src/main/java/net/minecraftforge/registries/GameData.java
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ public static void freezeData()
});
RegistryManager.ACTIVE.registries.forEach((name, reg) -> {
reg.freeze();
reg.bake();
reg.bake(IForgeRegistry.BakeReason.FREEZE);
reg.dump(name);
});

Expand Down Expand Up @@ -327,7 +327,7 @@ public static void revertTo(final RegistryManager target, boolean fireEvents)
final Class<? extends IForgeRegistryEntry> clazz = RegistryManager.ACTIVE.getSuperType(r.getKey());
loadRegistry(r.getKey(), target, RegistryManager.ACTIVE, clazz, true);
}
RegistryManager.ACTIVE.registries.forEach((name, reg) -> reg.bake());
RegistryManager.ACTIVE.registries.forEach((name, reg) -> reg.bake(IForgeRegistry.BakeReason.REVERT));
// the id mapping has reverted, fire remap events for those that care about id changes
if (fireEvents) {
fireRemapEvent(ImmutableMap.of(), true);
Expand Down Expand Up @@ -504,7 +504,12 @@ public Block createDummy(ResourceLocation key)
}

@Override
public void onBake(IForgeRegistryInternal<Block> owner, RegistryManager stage)
public void onBake(IForgeRegistryInternal<Block> owner, RegistryManager stage) {
onBake(owner, stage, null);
}

@Override
public void onBake(IForgeRegistryInternal<Block> owner, RegistryManager stage, IForgeRegistry.BakeReason reason)
{
@SuppressWarnings("unchecked")
ClearableObjectIntIdentityMap<BlockState> blockstateMap = owner.getSlaveMap(BLOCKSTATE_TO_ID, ClearableObjectIntIdentityMap.class);
Expand All @@ -514,7 +519,15 @@ public void onBake(IForgeRegistryInternal<Block> owner, RegistryManager stage)
for (BlockState state : block.getStateContainer().getValidStates())
{
blockstateMap.add(state);
state.cacheState();
if (reason == IForgeRegistry.BakeReason.FREEZE || reason == IForgeRegistry.BakeReason.REMOTE_SNAPSHOT_INJECT)
{
//If the reason is freeze or remote snapshot inject, the data will be recomputed later anyway. Do not waste time recomputing it twice
state.clearCache();
}
else
{
state.cacheState();
}
}

block.getLootTable();
Expand Down Expand Up @@ -866,8 +879,9 @@ else if (isLocalWorld)
loadRegistry(key, STAGING, RegistryManager.ACTIVE, registrySuperType, true);
});

IForgeRegistry.BakeReason reason = isLocalWorld ? IForgeRegistry.BakeReason.LOCAL_SNAPSHOT_INJECT : IForgeRegistry.BakeReason.REMOTE_SNAPSHOT_INJECT;
RegistryManager.ACTIVE.registries.forEach((name, reg) -> {
reg.bake();
reg.bake(reason);

// Dump the active registry
reg.dump(name);
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/net/minecraftforge/registries/IForgeRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,20 @@ interface ValidateCallback<V extends IForgeRegistryEntry<V>>
@FunctionalInterface
interface BakeCallback<V extends IForgeRegistryEntry<V>>
{
// TODO 1.17: Remove and replace with signature below
void onBake(IForgeRegistryInternal<V> owner, RegistryManager stage);

default void onBake(IForgeRegistryInternal<V> owner, RegistryManager stage, @Nullable BakeReason reason) {
onBake(owner, stage);
}
}

/**
* The reason why {@link BakeCallback#onBake(IForgeRegistryInternal, RegistryManager, BakeReason)} has been called
*/
enum BakeReason
{
FREEZE, REMOTE_SNAPSHOT_INJECT, LOCAL_SNAPSHOT_INJECT, REVERT
}

/**
Expand Down
16 changes: 12 additions & 4 deletions src/main/java/net/minecraftforge/registries/RegistryBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -312,10 +312,18 @@ public BakeCallback<T> getBake()
if (bakeCallback.size() == 1)
return bakeCallback.get(0);

return (owner, stage) ->
{
for (BakeCallback<T> cb : this.bakeCallback)
cb.onBake(owner, stage);
return new BakeCallback<T>() {

@Override
public void onBake(IForgeRegistryInternal<T> owner, RegistryManager stage) {
this.onBake(owner, stage, null);
}

@Override
public void onBake(IForgeRegistryInternal<T> owner, RegistryManager stage, BakeReason reason) {
for (BakeCallback<T> cb : RegistryBuilder.this.bakeCallback)
cb.onBake(owner, stage, reason);
}
};
}

Expand Down