Created
December 19, 2014 17:30
-
-
Save aadnk/e8681cd556dd91c9d677 to your computer and use it in GitHub Desktop.
Test of MagicAccessorImpl to speed up access of private packet fields
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.comphenix.example; | |
import java.util.concurrent.ConcurrentMap; | |
import org.bukkit.plugin.java.JavaPlugin; | |
import com.comphenix.protocol.PacketType; | |
import com.comphenix.protocol.events.PacketContainer; | |
import com.comphenix.protocol.injector.StructureCache; | |
import com.comphenix.protocol.reflect.FieldUtils; | |
import com.comphenix.protocol.reflect.StructureModifier; | |
import com.comphenix.protocol.reflect.compiler.BackgroundCompiler; | |
import com.google.common.base.Stopwatch; | |
public class ExampleMod extends JavaPlugin { | |
@Override | |
public void onEnable() { | |
// Disable compiler first time around | |
BackgroundCompiler.getInstance().setEnabled(false); | |
getServer().getScheduler().runTaskTimer(this, new Runnable() { | |
@SuppressWarnings("unchecked") | |
@Override | |
public void run() { | |
Stopwatch watch = Stopwatch.createStarted(); | |
PacketContainer dummy = new PacketContainer(PacketType.Play.Server.EXPERIENCE); | |
System.out.println("Integer structure: " + dummy.getIntegers().getClass()); | |
System.out.println("Float structure: " + dummy.getFloat().getClass()); | |
for (int i = 0; i < 500000; i++) { | |
PacketContainer packet = new PacketContainer(PacketType.Play.Server.EXPERIENCE); | |
packet.getIntegers(). | |
write(0, 5). | |
write(1, 1000); | |
packet.getFloat(). | |
write(0, 0.5f); | |
} | |
watch.stop(); | |
System.out.println("Time taken to create 500000 packets: " + watch); | |
if (!BackgroundCompiler.getInstance().isEnabled()) { | |
// Clear cache | |
try { | |
((ConcurrentMap<PacketType, StructureModifier<Object>>) | |
FieldUtils.readStaticField(StructureCache.class, "structureModifiers", true)). | |
remove(PacketType.Play.Server.EXPERIENCE); | |
} catch (IllegalAccessException e) { | |
e.printStackTrace(); | |
} | |
BackgroundCompiler.getInstance().setEnabled(true); | |
System.out.println("Enabling compiler ..."); | |
} | |
} | |
}, 1, 5 * 20); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment