1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2025-01-15 03:35:42 +00:00

Use ByteBuffers in term.blit

This is about 5-6x faster than using a String as we don't need to
allocate and copy multiple times. In the grand scheme of things, still
vastly overshadowed by the Lua interpreter, but worth doing.
This commit is contained in:
Jonathan Coates 2022-05-03 12:47:13 +01:00
parent 074793090d
commit 79467499e6
No known key found for this signature in database
GPG Key ID: B9E431FF07C98D06
4 changed files with 33 additions and 10 deletions

View File

@ -14,6 +14,7 @@ import dan200.computercraft.shared.util.StringUtil;
import org.apache.commons.lang3.ArrayUtils;
import javax.annotation.Nonnull;
import java.nio.ByteBuffer;
/**
* A base class for all objects which interact with a terminal. Namely the {@link TermAPI} and monitors.
@ -283,9 +284,9 @@ public abstract class TermMethods
* }</pre>
*/
@LuaFunction
public final void blit( String text, String textColour, String backgroundColour ) throws LuaException
public final void blit( ByteBuffer text, ByteBuffer textColour, ByteBuffer backgroundColour ) throws LuaException
{
if( textColour.length() != text.length() || backgroundColour.length() != text.length() )
if( textColour.remaining() != text.remaining() || backgroundColour.remaining() != text.remaining() )
{
throw new LuaException( "Arguments must be the same length" );
}
@ -294,7 +295,7 @@ public abstract class TermMethods
synchronized( terminal )
{
terminal.blit( text, textColour, backgroundColour );
terminal.setCursorPos( terminal.getCursorX() + text.length(), terminal.getCursorY() );
terminal.setCursorPos( terminal.getCursorX() + text.remaining(), terminal.getCursorY() );
}
}

View File

@ -11,6 +11,7 @@ import net.minecraft.nbt.CompoundNBT;
import net.minecraft.network.PacketBuffer;
import javax.annotation.Nonnull;
import java.nio.ByteBuffer;
public class Terminal
{
@ -191,7 +192,7 @@ public class Terminal
return palette;
}
public synchronized void blit( String text, String textColour, String backgroundColour )
public synchronized void blit( ByteBuffer text, ByteBuffer textColour, ByteBuffer backgroundColour )
{
int x = cursorX;
int y = cursorY;

View File

@ -5,6 +5,8 @@
*/
package dan200.computercraft.core.terminal;
import java.nio.ByteBuffer;
public class TextBuffer
{
private final char[] text;
@ -42,6 +44,19 @@ public class TextBuffer
}
}
public void write( ByteBuffer text, int start )
{
int pos = start;
start = Math.max( start, 0 );
int length = text.remaining();
int end = Math.min( start + length, pos + length );
end = Math.min( end, this.text.length );
for( int i = start; i < end; i++ )
{
this.text[i] = (char) (text.get( i - pos ) & 0xFF);
}
}
public void write( TextBuffer text )
{
int end = Math.min( text.length(), this.text.length );

View File

@ -5,6 +5,7 @@
*/
package dan200.computercraft.core.terminal;
import dan200.computercraft.api.lua.LuaValues;
import dan200.computercraft.shared.util.Colour;
import dan200.computercraft.utils.CallCounter;
import io.netty.buffer.Unpooled;
@ -293,7 +294,7 @@ class TerminalTest
CallCounter callCounter = new CallCounter();
Terminal terminal = new Terminal( 4, 3, callCounter );
terminal.blit( "test", "1234", "abcd" );
blit( terminal, "test", "1234", "abcd" );
assertThat( terminal, allOf(
textMatches( new String[] {
@ -322,7 +323,7 @@ class TerminalTest
terminal.setCursorPos( 2, 1 );
callCounter.reset();
terminal.blit( "hi", "11", "ee" );
blit( terminal, "hi", "11", "ee" );
assertThat( terminal, allOf(
textMatches( new String[] {
@ -354,13 +355,13 @@ class TerminalTest
terminal.setCursorPos( 2, -5 );
callCounter.reset();
terminal.blit( "hi", "11", "ee" );
blit( terminal, "hi", "11", "ee" );
assertThat( terminal, old.matches() );
callCounter.assertNotCalled();
terminal.setCursorPos( 2, 5 );
callCounter.reset();
terminal.blit( "hi", "11", "ee" );
blit( terminal, "hi", "11", "ee" );
assertThat( terminal, old.matches() );
callCounter.assertNotCalled();
}
@ -584,7 +585,7 @@ class TerminalTest
{
Terminal writeTerminal = new Terminal( 2, 1 );
writeTerminal.blit( "hi", "11", "ee" );
blit( writeTerminal, "hi", "11", "ee" );
writeTerminal.setCursorPos( 2, 5 );
writeTerminal.setTextColour( 3 );
writeTerminal.setBackgroundColour( 5 );
@ -614,7 +615,7 @@ class TerminalTest
void testNbtRoundtrip()
{
Terminal writeTerminal = new Terminal( 10, 5 );
writeTerminal.blit( "hi", "11", "ee" );
blit( writeTerminal, "hi", "11", "ee" );
writeTerminal.setCursorPos( 2, 5 );
writeTerminal.setTextColour( 3 );
writeTerminal.setBackgroundColour( 5 );
@ -687,6 +688,11 @@ class TerminalTest
assertEquals( 5, Terminal.getColour( 'Z', Colour.LIME ) );
}
private static void blit( Terminal terminal, String text, String fg, String bg )
{
terminal.blit( LuaValues.encode( text ), LuaValues.encode( fg ), LuaValues.encode( bg ) );
}
private static final class TerminalBufferSnapshot
{
final String[] textLines;