reduced allocations on reading block messages
Some checks failed
studiorailgun/Renderer/pipeline/head There was a failure building this commit

This commit is contained in:
austin 2025-05-26 11:22:05 -04:00
parent bb48ca0663
commit ac13a458fd
4 changed files with 72 additions and 20 deletions

View File

@ -1,7 +1,5 @@
package electrosphere.client.block;
import java.nio.ByteBuffer;
import java.nio.ShortBuffer;
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedList;
@ -17,7 +15,6 @@ import electrosphere.client.terrain.cells.DrawCell;
import electrosphere.engine.Globals;
import electrosphere.entity.Entity;
import electrosphere.logger.LoggerInterface;
import electrosphere.mem.BlockChunkPool;
import electrosphere.net.parser.net.message.TerrainMessage;
import electrosphere.renderer.meshgen.BlockMeshgen.BlockMeshData;
import electrosphere.server.physics.terrain.manager.ServerTerrainChunk;
@ -115,19 +112,13 @@ public class ClientBlockManager {
//read main data
if(data.getHomogenousValue() == BlockChunkData.NOT_HOMOGENOUS){
short[] type = BlockChunkPool.getShort();
short[] metadata = BlockChunkPool.getShort();
ByteBuffer buffer = ByteBuffer.wrap(message.getchunkData());
ShortBuffer shortBuffer = buffer.asShortBuffer();
for(int i = 0; i < BlockChunkData.TOTAL_DATA_WIDTH; i++){
type[i] = shortBuffer.get();
List<Object> extraData = message.getExtraData();
if(extraData == null || extraData.size() != 2){
throw new Error("Failed to attach extra data!");
}
for(int i = 0; i < BlockChunkData.TOTAL_DATA_WIDTH; i++){
metadata[i] = shortBuffer.get();
}
data.setType(type);
data.setMetadata(metadata);
data.setType((short[])extraData.get(0));
data.setMetadata((short[])extraData.get(1));
extraData.clear();
}
blockCache.add(message.getworldX(), message.getworldY(), message.getworldZ(), message.getchunkResolution(), data);
//remove from request map

View File

@ -1,7 +1,9 @@
package electrosphere.net.client;
import electrosphere.client.block.BlockChunkData;
import electrosphere.engine.Globals;
import electrosphere.logger.LoggerInterface;
import electrosphere.mem.BlockChunkPool;
import electrosphere.net.parser.net.message.NetworkMessage;
import electrosphere.net.parser.net.message.ServerMessage;
import electrosphere.net.parser.net.message.TerrainMessage;
@ -16,6 +18,8 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.TimeUnit;
/**
@ -181,7 +185,26 @@ public class ClientNetworking implements Runnable {
castMessage.setworldZ(ByteStreamUtils.popIntFromByteQueue(buff));
castMessage.setchunkResolution(ByteStreamUtils.popIntFromByteQueue(buff));
castMessage.sethomogenousValue(ByteStreamUtils.popIntFromByteQueue(buff));
castMessage.setchunkData(ByteStreamUtils.popByteArrayFromByteQueue(buff));
//strip the byte stream header
ByteStreamUtils.popIntFromByteQueue(buff);
if(castMessage.gethomogenousValue() == BlockChunkData.NOT_HOMOGENOUS){
//read types from byte stream
short[] types = BlockChunkPool.getShort();
for(int i = 0; i < BlockChunkData.TOTAL_DATA_WIDTH; i++){
types[i] = ByteStreamUtils.popShortFromByteQueue(buff);
}
//read metadata from byte stream
short[] metadata = BlockChunkPool.getShort();
for(int i = 0; i < BlockChunkData.TOTAL_DATA_WIDTH; i++){
metadata[i] = ByteStreamUtils.popShortFromByteQueue(buff);
}
List<Object> extraData = new LinkedList<Object>();
extraData.add(types);
extraData.add(metadata);
castMessage.setExtraData(extraData);
} else {
buff.read(1);
}
});

View File

@ -5,6 +5,7 @@ import io.github.studiorailgun.CircularByteBuffer;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Map;
import java.util.List;
import java.util.function.BiConsumer;
/**
@ -43,6 +44,11 @@ public abstract class NetworkMessage {
*/
byte[] rawBytes;
/**
* Extra data that can be attached to a message optionally (used for reading in messages, does not affect ougoing messages).
*/
private List<Object> extraData;
/**
* Gets the type of the message
* @return The type of the message
@ -536,6 +542,22 @@ public abstract class NetworkMessage {
* Serializes the message
*/
abstract void serialize();
/**
* Gets the extra data attached to the message
* @return The extra data if it exists, null otherwise
*/
public List<Object> getExtraData(){
return this.extraData;
}
/**
* Sets the extra data on the message
* @param extraData The extra data
*/
public void setExtraData(List<Object> extraData){
this.extraData = extraData;
}
}

View File

@ -1,7 +1,7 @@
package electrosphere.net.parser.util;
import io.github.studiorailgun.CircularByteBuffer;
package electrosphere.net.parser.util;
import io.github.studiorailgun.CircularByteBuffer;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.ByteBuffer;
@ -167,6 +167,22 @@ public class ByteStreamUtils {
bufferLock.release();
return rVal;
}
public static short popShortFromByteQueue(CircularByteBuffer byteBuffer){
short rVal = -1;
bufferLock.acquireUninterruptibly();
integerCompactor.clear();
integerCompactor.position(0);
integerCompactor.limit(2);
integerCompactor.put(0,byteBuffer.peek(0));
integerCompactor.put(1,byteBuffer.peek(1));
byteBuffer.read(2);
integerCompactor.position(0);
integerCompactor.limit(2);
rVal = integerCompactor.getShort();
bufferLock.release();
return rVal;
}
public static String popStringFromByteQueue(CircularByteBuffer byteBuffer){
int length = popIntFromByteQueue(byteBuffer);