Optimize async -> sync task execution

This commit is contained in:
Jesse Boyd 2017-08-27 20:18:31 +10:00
parent bc0bc5b705
commit ee0a09e132
No known key found for this signature in database
GPG Key ID: 59F1DE6293AF6E1F
4 changed files with 85 additions and 9 deletions

View File

@ -0,0 +1,65 @@
package com.boydti.fawe.bukkit.v1_12.packet;
public class BitArray {
private final long[] longArray;
private final int bitsPerEntry;
private final long maxEntryValue;
private final int arraySize;
public BitArray(int num, int length) {
this.arraySize = length;
this.bitsPerEntry = num;
this.maxEntryValue = (1L << num) - 1L;
this.longArray = new long[roundUp(length * num, 64) / 64];
}
private int roundUp(int value, int interval) {
if(interval == 0) {
return 0;
} else if(value == 0) {
return interval;
} else {
if(value < 0) {
interval *= -1;
}
int lvt_2_1_ = value % interval;
return lvt_2_1_ == 0?value:value + interval - lvt_2_1_;
}
}
public void setAt(int index, int value) {
int lvt_3_1_ = index * this.bitsPerEntry;
int lvt_4_1_ = lvt_3_1_ / 64;
int lvt_5_1_ = ((index + 1) * this.bitsPerEntry - 1) / 64;
int lvt_6_1_ = lvt_3_1_ % 64;
this.longArray[lvt_4_1_] = this.longArray[lvt_4_1_] & ~(this.maxEntryValue << lvt_6_1_) | ((long)value & this.maxEntryValue) << lvt_6_1_;
if(lvt_4_1_ != lvt_5_1_) {
int lvt_7_1_ = 64 - lvt_6_1_;
int lvt_8_1_ = this.bitsPerEntry - lvt_7_1_;
this.longArray[lvt_5_1_] = this.longArray[lvt_5_1_] >>> lvt_8_1_ << lvt_8_1_ | ((long)value & this.maxEntryValue) >> lvt_7_1_;
}
}
public int getAt(int index) {
int localBitIndex = index * this.bitsPerEntry;
int localLongIndex = localBitIndex / 64;
int lvt_4_1_ = ((index + 1) * this.bitsPerEntry - 1) / 64;
int lvt_5_1_ = localBitIndex % 64;
if(localLongIndex == lvt_4_1_) {
return (int)(this.longArray[localLongIndex] >>> lvt_5_1_ & this.maxEntryValue);
} else {
int lvt_6_1_ = 64 - lvt_5_1_;
return (int)((this.longArray[localLongIndex] >>> lvt_5_1_ | this.longArray[lvt_4_1_] << lvt_6_1_) & this.maxEntryValue);
}
}
public long[] getBackingLongArray() {
return this.longArray;
}
public int size() {
return this.arraySize;
}
}

View File

@ -1,4 +1,4 @@
package com.boydti.fawe.bukkit.v1_12;
package com.boydti.fawe.bukkit.v1_12.packet;
import com.boydti.fawe.FaweCache;
import com.boydti.fawe.jnbt.anvil.MCAChunk;
@ -10,9 +10,6 @@ import com.comphenix.protocol.wrappers.nbt.NbtBase;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import net.minecraft.server.v1_12_R1.Block;
import net.minecraft.server.v1_12_R1.DataBits;
import net.minecraft.server.v1_12_R1.MathHelper;
public class FaweChunkPacket {
@ -49,19 +46,19 @@ public class FaweChunkPacket {
continue;
}
byte[] layerData = chunk.data[layer];
int num = MathHelper.d(Block.REGISTRY_ID.a());
int num = 9;
buffer.write(num); // num blocks, anything > 8 - doesn't need to be accurate
buffer.writeVarInt(0); // varint 0 - data palette global
DataBits bits = new DataBits(num, 4096);
BitArray bits = new BitArray(num, 4096);
for (int i = 0; i < 4096; i++) {
int id = layerIds[i];
if (id != 0) {
int data = FaweCache.hasData(id) ? chunk.getNibble(i, layerData) : 0;
int combined = FaweCache.getCombined(id, data);
bits.a(i, combined);
bits.setAt(i, combined);
}
}
buffer.write(bits.a());
buffer.write(bits.getBackingLongArray());
buffer.write(chunk.blockLight[layer]);
if (sky) {
@ -74,7 +71,6 @@ public class FaweChunkPacket {
}
byteArray.write(0, fbaos.toByteArray());
// TODO - empty
StructureModifier<List<NbtBase<?>>> list = packet.getListNbtModifier();
list.write(0, new ArrayList<>());

View File

@ -600,6 +600,9 @@ public class CreateFromImage extends Command {
}
private BufferedImage getImage(String arg, FawePlayer fp) throws IOException {
if (arg.endsWith(".jpg")) {
fp.sendMessage(BBC.getPrefix() + "JPG is lossy, you may see compression artifacts. For large image hosting you can try: empcraft.com/ui");
}
if (arg.startsWith("http")) {
URL url = new URL(arg);
fp.sendMessage(BBC.getPrefix() + "Downloading image... (3)");

View File

@ -105,11 +105,20 @@ public class SetQueue {
if (!emptyTasks) {
long taskAllocate = empty ? currentAllocate : currentAllocate >> 1;
long used = 0;
boolean wait = false;
do {
Runnable task = tasks.poll();
if (task != null) {
task.run();
wait = true;
} else {
if (wait) {
synchronized (tasks) {
tasks.wait(1);
}
wait = false;
continue;
}
break;
}
} while ((used = System.currentTimeMillis() - now) < taskAllocate);
@ -396,6 +405,9 @@ public class SetQueue {
public void addTask(Runnable whenFree) {
tasks.add(whenFree);
synchronized (tasks) {
tasks.notifyAll();
}
}
/**