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() {
int playerCount = PlayerInfo.Online.Count;
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() {

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -482,7 +482,7 @@ namespace MCGalaxy {
bool FilterChat(ref string text, byte continued) {
// 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));
UsingWom = true;
return true;

View File

@ -39,10 +39,18 @@ namespace MCGalaxy {
return value.Split(trimChars, maxParts);
}
public static string Truncate(this string source, int maxLength) {
if (source.Length > maxLength)
source = source.Substring(0, maxLength);
return source;
public static void FixedSplit(this string value, string[] split, char splitter) {
int start = 0, i = 0;
for (; i < split.Length && start <= value.Length; i++) {
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) {

View File

@ -25,7 +25,7 @@ namespace MCGalaxy {
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>
public object Get(string key){
public object Get(string key) {
object value;
TryGetValue(key, out value);
return value;