Avoid boxing primitives on NativeBackend, majorly reduced memory allocations during BlockDB saving. Also remove pointless text/salt check as it is entirely undocumented. (Borrowed from MCHmk, thanks Jjp137)

This commit is contained in:
UnknownShadow200 2016-03-25 21:24:47 +11:00
parent bde18d7d41
commit aadceacb1b
4 changed files with 31 additions and 43 deletions

View File

@ -76,16 +76,13 @@ namespace MCGalaxy.SQL.Native {
code = sqlite3_bind_text(Statement, nParam.Index, dataPtr, dataCount - 1, IntPtr.Zero);
break;
case DbType.UInt16:
ushort value_u16 = (ushort)nParam.Value;
code = sqlite3_bind_int(Statement, nParam.Index, value_u16);
code = sqlite3_bind_int(Statement, nParam.Index, nParam.U16Value);
break;
case DbType.Byte:
byte value_u8 = (byte)nParam.Value;
code = sqlite3_bind_int(Statement, nParam.Index, value_u8);
code = sqlite3_bind_int(Statement, nParam.Index, nParam.U8Value);
break;
case DbType.Boolean:
bool value_bool = (bool)nParam.Value;
code = sqlite3_bind_int(Statement, nParam.Index, value_bool ? 1 : 0);
code = sqlite3_bind_int(Statement, nParam.Index, nParam.BoolValue ? 1 : 0);
break;
}
if (code > 0) throw new NativeException(code);

View File

@ -44,7 +44,7 @@ namespace MCGalaxy.SQL.Native {
}
}
sealed class NativeParameter : IDataParameter {
internal sealed class NativeParameter : IDataParameter {
public DbType DbType { get { return type; } set { type = value; } }
public ParameterDirection Direction { get; set; }
public bool IsNullable { get { return false; } }
@ -55,6 +55,9 @@ namespace MCGalaxy.SQL.Native {
public DbType type;
public int Index = -1;
public ushort U16Value;
public byte U8Value;
public bool BoolValue;
}
sealed class NativeParamsList : List<IDataParameter>, IDataParameterCollection {

View File

@ -30,6 +30,7 @@ using Timer = System.Timers.Timer;
using MCGalaxy.BlockPhysics;
using MCGalaxy.Games;
using MCGalaxy.Levels.IO;
using MCGalaxy.SQL.Native;
//WARNING! DO NOT CHANGE THE WAY THE LEVEL IS SAVED/LOADED!
//You MUST make it able to save and load as a new version other wise you will make old levels incompatible!
@ -391,6 +392,7 @@ namespace MCGalaxy
IDataParameter zP = transaction.CreateParam("@Z", DbType.UInt16); cmd.Parameters.Add(zP);
IDataParameter tileP = transaction.CreateParam("@Tile", DbType.Byte); cmd.Parameters.Add(tileP);
IDataParameter delP = transaction.CreateParam("@Del", DbType.Boolean); cmd.Parameters.Add(delP);
bool isNative = transaction is NativeBulkTransaction;
for (int i = 0; i < tempCache.Count; i++) {
BlockPos bP = tempCache[i];
@ -401,9 +403,18 @@ namespace MCGalaxy
MakeInt(time.Hour, 2, 11, ptr); MakeInt(time.Minute, 2, 14, ptr); MakeInt(time.Second, 2, 17, ptr);
timeP.Value = date;
xP.Value = x; yP.Value = y; zP.Value = z;
tileP.Value = (bP.flags & 2) != 0 ? Block.custom_block : bP.rawType;
delP.Value = (bP.flags & 1) != 0;
// For NativeParameter, we make the optimisation of avoiding boxing primitive types.
if (!isNative) {
xP.Value = x; yP.Value = y; zP.Value = z;
tileP.Value = (bP.flags & 2) != 0 ? Block.custom_block : bP.rawType;
delP.Value = (bP.flags & 1) != 0;
} else {
((NativeParameter)xP).U16Value = x;
((NativeParameter)yP).U16Value = y;
((NativeParameter)zP).U16Value = z;
((NativeParameter)tileP).U8Value = (bP.flags & 2) != 0 ? Block.custom_block : bP.rawType;
((NativeParameter)delP).BoolValue = (bP.flags & 1) != 0;
}
if (!BulkTransaction.Execute(template, cmd)) {
cmd.Dispose();

View File

@ -27,38 +27,15 @@ namespace MCGalaxy {
public static class SrvProperties {
public static void Load(string givenPath, bool skipsalt = false) {
/*
if (!skipsalt)
{
Server.salt = "";
string rndchars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
Random rnd = new Random();
for (int i = 0; i < 16; ++i) { Server.salt += rndchars[rnd.Next(rndchars.Length)]; }
}*/
if ( !skipsalt ) {
bool gotSalt = false;
if ( File.Exists("text/salt.txt") ) {
string salt = File.ReadAllText("text/salt.txt");
if ( salt.Length != 16 )
Server.s.Log("Invalid salt in salt.txt!");
else {
Server.salt = salt;
gotSalt = true;
}
}
if ( !gotSalt ) {
RandomNumberGenerator prng = RandomNumberGenerator.Create();
StringBuilder sb = new StringBuilder();
byte[] oneChar = new byte[1];
while ( sb.Length < 16 ) {
prng.GetBytes(oneChar);
if ( Char.IsLetterOrDigit((char)oneChar[0]) ) {
sb.Append((char)oneChar[0]);
}
}
Server.salt = sb.ToString();
}
}
RandomNumberGenerator prng = RandomNumberGenerator.Create();
StringBuilder sb = new StringBuilder();
byte[] oneChar = new byte[1];
while (sb.Length < 16) {
prng.GetBytes(oneChar);
if (Char.IsLetterOrDigit((char)oneChar[0]))
sb.Append((char)oneChar[0]);
}
Server.salt = sb.ToString();
if (PropertiesFile.Read(givenPath, LineProcessor))
Server.s.SettingsUpdate();