Reuse string array when splitting. Reduces mem allocations at startup by about 50kb.

This commit is contained in:
UnknownShadow200 2017-08-02 18:25:12 +10:00
parent 56a57be640
commit 41217c2dfa
12 changed files with 43 additions and 34 deletions

View File

@ -80,7 +80,11 @@ namespace MCGalaxy.Gui {
void UpdateNotifyIconText() { void UpdateNotifyIconText() {
int playerCount = PlayerInfo.Online.Count; int playerCount = PlayerInfo.Online.Count;
string players = " (" + playerCount + " players)"; string players = " (" + playerCount + " players)";
notifyIcon.Text = (ServerConfig.Name + players).Truncate(63);
// ArgumentException thrown if text length is > 63
string text = (ServerConfig.Name + players);
if (text.Length > 63) text = text.Substring(0, 63);
notifyIcon.Text = text;
} }
void MakeNotifyIcon() { void MakeNotifyIcon() {

View File

@ -147,11 +147,11 @@ namespace MCGalaxy.Blocks {
} }
static void LoadVersion2(string[] lines) { static void LoadVersion2(string[] lines) {
char[] colon = new char[] { ':' }; string[] args = new string[4];
foreach (string line in lines) { foreach (string line in lines) {
if (line.Length == 0 || line[0] == '#') continue; if (line.Length == 0 || line[0] == '#') continue;
//Name : Lowest : Disallow : Allow //Name : Lowest : Disallow : Allow
string[] args = line.Replace(" ", "").Split(colon); line.Replace(" ", "").FixedSplit(args, ':');
BlockPerms perms = new BlockPerms(); BlockPerms perms = new BlockPerms();
if (Block.Byte(args[0]) == Block.Invalid) continue; if (Block.Byte(args[0]) == Block.Invalid) continue;
@ -159,8 +159,7 @@ namespace MCGalaxy.Blocks {
try { try {
perms.MinRank = (LevelPermission)int.Parse(args[1]); perms.MinRank = (LevelPermission)int.Parse(args[1]);
string disallowRaw = args.Length > 2 ? args[2] : null; string disallowRaw = args[2], allowRaw = args[3];
string allowRaw = args.Length > 3 ? args[3] : null;
perms.Allowed = CommandPerms.ExpandPerms(allowRaw); perms.Allowed = CommandPerms.ExpandPerms(allowRaw);
perms.Disallowed = CommandPerms.ExpandPerms(disallowRaw); perms.Disallowed = CommandPerms.ExpandPerms(disallowRaw);

View File

@ -116,13 +116,11 @@ namespace MCGalaxy {
} }
string[] lines = tokensFile.GetText(); string[] lines = tokensFile.GetText();
char[] colon = null; string[] parts = new string[2];
foreach (string line in lines) { foreach (string line in lines) {
if (line.StartsWith("//")) continue; if (line.StartsWith("//")) continue;
line.FixedSplit(parts, ':');
if (colon == null) colon = new char[] { ':' }; if (parts[1] == null) continue; // not a proper line
string[] parts = line.Split(colon, 2);
if (parts.Length != 2) continue;
string key = parts[0].Trim(), value = parts[1].Trim(); string key = parts[0].Trim(), value = parts[1].Trim();
if (key.Length == 0) continue; if (key.Length == 0) continue;

View File

@ -139,14 +139,15 @@ namespace MCGalaxy.Commands {
} }
static void LoadCore() { static void LoadCore() {
string[] args = new string[4];
using (StreamReader r = new StreamReader(Paths.CmdExtraPermsFile)) { using (StreamReader r = new StreamReader(Paths.CmdExtraPermsFile)) {
string line; string line;
while ((line = r.ReadLine()) != null) { while ((line = r.ReadLine()) != null) {
if (line.Length == 0 || line[0] == '#' || line.IndexOf(':') == -1) continue; if (line.Length == 0 || line[0] == '#' || line.IndexOf(':') == -1) continue;
try { try {
string[] parts = line.Split(':'); line.FixedSplit(args, ':');
LoadExtraPerm(parts); LoadExtraPerm(args);
} catch (Exception ex) { } catch (Exception ex) {
Logger.Log(LogType.Warning, "Loading an additional command permission failed!!"); Logger.Log(LogType.Warning, "Loading an additional command permission failed!!");
Logger.LogError(ex); Logger.LogError(ex);
@ -155,10 +156,10 @@ namespace MCGalaxy.Commands {
} }
} }
static void LoadExtraPerm(string[] parts) { static void LoadExtraPerm(string[] args) {
string cmdName = parts[0]; string cmdName = args[0];
int number = int.Parse(parts[1]), minPerm = int.Parse(parts[2]); int number = int.Parse(args[1]), minPerm = int.Parse(args[2]);
string desc = parts.Length > 3 ? parts[3] : ""; string desc = args[3] == null ? "" : args[3];
CommandExtraPerms existing = Find(cmdName, number); CommandExtraPerms existing = Find(cmdName, number);
if (existing != null) desc = existing.Description; if (existing != null) desc = existing.Description;

View File

@ -195,16 +195,15 @@ namespace MCGalaxy.Commands {
} }
static void LoadVersion2(string[] lines) { static void LoadVersion2(string[] lines) {
char[] colon = new char[] { ':' }; string[] args = new string[4];
foreach (string line in lines) { foreach (string line in lines) {
if (line.Length == 0 || line[0] == '#') continue; if (line.Length == 0 || line[0] == '#') continue;
//Name : Lowest : Disallow : Allow //Name : Lowest : Disallow : Allow
string[] args = line.Replace(" ", "").Split(colon); line.Replace(" ", "").FixedSplit(args, ':');
try { try {
LevelPermission minRank = (LevelPermission)int.Parse(args[1]); LevelPermission minRank = (LevelPermission)int.Parse(args[1]);
string disallowRaw = args.Length > 2 ? args[2] : null; string disallowRaw = args[2], allowRaw = args[3];
string allowRaw = args.Length > 3 ? args[3] : null;
List<LevelPermission> allow = ExpandPerms(allowRaw); List<LevelPermission> allow = ExpandPerms(allowRaw);
List<LevelPermission> disallow = ExpandPerms(disallowRaw); List<LevelPermission> disallow = ExpandPerms(disallowRaw);

View File

@ -41,8 +41,9 @@ namespace MCGalaxy.DB {
public static bool Load( Player p ) { public static bool Load( Player p ) {
if (!File.Exists("players/" + p.name + "DB.txt")) return false; if (!File.Exists("players/" + p.name + "DB.txt")) return false;
foreach (string line in File.ReadAllLines( "players/" + p.name + "DB.txt")) { string[] lines = File.ReadAllLines( "players/" + p.name + "DB.txt");
if (string.IsNullOrEmpty(line) || line[0] == '#') continue; foreach (string line in lines) {
if (line.Length == 0 || line[0] == '#') continue;
string[] parts = line.Split(trimChars, 2); string[] parts = line.Split(trimChars, 2);
if (parts.Length < 2) continue; if (parts.Length < 2) continue;
string key = parts[0].Trim(), value = parts[1].Trim(); string key = parts[0].Trim(), value = parts[1].Trim();

View File

@ -68,12 +68,13 @@ namespace MCGalaxy.Drawing {
string[] lines = File.ReadAllLines(file); string[] lines = File.ReadAllLines(file);
List<PaletteEntry> entries = new List<PaletteEntry>(); List<PaletteEntry> entries = new List<PaletteEntry>();
string[] parts = new string[5];
foreach (string line in lines) { foreach (string line in lines) {
if (line.StartsWith("#") || line.Length == 0) continue; if (line.StartsWith("#") || line.Length == 0) continue;
string[] parts = line.Split(':'); line.FixedSplit(parts, ':');
if (parts.Length != 4) continue; if (parts[3] == null || parts[4] != null) continue; // not a proper line
entries.Add(ParseEntry(parts)); entries.Add(ParseEntry(parts));
} }

View File

@ -29,8 +29,6 @@ namespace MCGalaxy {
public partial class Player : IDisposable { public partial class Player : IDisposable {
public Dictionary<string, object> ExtraData = new Dictionary<string, object>();
internal class PendingItem { internal class PendingItem {
public string Name; public string Name;
public DateTime Connected; public DateTime Connected;

View File

@ -482,7 +482,7 @@ namespace MCGalaxy {
bool FilterChat(ref string text, byte continued) { bool FilterChat(ref string text, byte continued) {
// handles the /womid client message, which displays the WoM vrersion // handles the /womid client message, which displays the WoM vrersion
if (text.StartsWith("/womid") { if (text.StartsWith("/womid")) {
string version = (text.Length <= 21 ? text.Substring(text.IndexOf(' ') + 1) : text.Substring(7, 15)); string version = (text.Length <= 21 ? text.Substring(text.IndexOf(' ') + 1) : text.Substring(7, 15));
UsingWom = true; UsingWom = true;
return true; return true;

View File

@ -39,10 +39,18 @@ namespace MCGalaxy {
return value.Split(trimChars, maxParts); return value.Split(trimChars, maxParts);
} }
public static string Truncate(this string source, int maxLength) { public static void FixedSplit(this string value, string[] split, char splitter) {
if (source.Length > maxLength) int start = 0, i = 0;
source = source.Substring(0, maxLength); for (; i < split.Length && start <= value.Length; i++) {
return source; int end = value.IndexOf(splitter, start);
if (end == -1) end = value.Length;
split[i] = value.Substring(start, end - start);
start = end + 1;
}
// If not enough split strings, set remaining to null
for (; i < split.Length; i++) { split[i] = null; }
} }
public static byte[] GZip(this byte[] bytes) { public static byte[] GZip(this byte[] bytes) {

View File

@ -25,7 +25,7 @@ namespace MCGalaxy {
public sealed class ExtrasCollection : Dictionary<string, object> { public sealed class ExtrasCollection : Dictionary<string, object> {
/// <summary> Returns the value associated with the given key as an object, or null if no value exists for this key </summary> /// <summary> Returns the value associated with the given key as an object, or null if no value exists for this key </summary>
public object Get(string key){ public object Get(string key) {
object value; object value;
TryGetValue(key, out value); TryGetValue(key, out value);
return value; return value;