Skip to content

Commit 10fab8c

Browse files
Less allocations when parsing chat
1 parent 220112b commit 10fab8c

File tree

3 files changed

+32
-35
lines changed

3 files changed

+32
-35
lines changed

fCraft/Commands/ChatCommands.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1091,10 +1091,9 @@ private static void greetHandler(Player player, CommandReader cmd) {
10911091
static void LdisHandler( Player player, CommandReader cmd ) {
10921092
string first = cmd.Next();
10931093
string second = cmd.Next();
1094-
if (first == null)
1095-
first = "";
1096-
if (second == null)
1097-
second = "";
1094+
if (first == null) first = "";
1095+
if (second == null) second = "";
1096+
10981097
float percent = (1 - Chat.LDistance(first, second)) * 100;
10991098
player.Message( percent + "&S% similarity between:" );
11001099
player.Message( " &7{0} &Sand &7{1}", first, second );

fCraft/Player/Chat.cs

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,16 @@ public static bool SendGlobal([NotNull] Player player, [NotNull] string rawMessa
5555

5656
public static string Filter(string rawMessage, Player player) {
5757
if (player != null && !player.IsStaff) {
58-
if (player.LastMessage == new string(rawMessage.ToLower().Where(c => !char.IsWhiteSpace(c)).ToArray())) {
59-
if (player.MessageSpam >= 2) {
58+
string trimmedMsg = new string(rawMessage.Where(c => !char.IsWhiteSpace(c)).ToArray());
59+
if (player.lastMsg.CaselessEquals(trimmedMsg)) {
60+
if (player.lastMsgCount >= 2) {
6061
player.Message("Please refrain from repeating yourself!");
6162
return null;
6263
}
63-
player.MessageSpam++;
64+
player.lastMsgCount++;
6465
} else {
65-
player.LastMessage = new string(rawMessage.ToLower().Where(c => !char.IsWhiteSpace(c)).ToArray());
66-
player.MessageSpam = 0;
66+
player.lastMsg = trimmedMsg;
67+
player.lastMsgCount = 0;
6768
}
6869
}
6970

@@ -80,9 +81,7 @@ public static string Filter(string rawMessage, Player player) {
8081
if (rawMessage.Length >= 10 && player.Info.Rank.MaxCaps > 0) {
8182
int caps = 0;
8283
for (int i = 0; i < rawMessage.Length; i++) {
83-
if (char.IsUpper(rawMessage[i])) {
84-
caps++;
85-
}
84+
if (char.IsUpper(rawMessage[i])) caps++;
8685
}
8786

8887
if (player.Info.Rank.MaxCaps == 1) {
@@ -162,15 +161,15 @@ static void checkBotResponses(Player player, string rawMessage) {
162161
player.ParseMessage("/bot <CalledFromChat> " + rawMessage.Remove(0, 4), false);
163162
}
164163
double BotTime = player.TimeSinceLastServerMessage.TotalSeconds;
165-
if (LDistance(rawMessage.ToLower(), "how do i rank up?") <= 0.25
166-
|| LDistance(rawMessage.ToLower(), "how do we rank up") <= 0.25) {
164+
if (LDistance(rawMessage, "how do i rank up?") <= 0.25
165+
|| LDistance(rawMessage, "how do we rank up") <= 0.25) {
167166
if (BotTime > 5) {
168167
Server.BotMessage("You rank up by building something nice, preferably big. Then ask a staff member for a review.");
169168
player.LastServerMessageDate = DateTime.UtcNow;
170169
player.Info.TimesUsedBot++;
171170
}
172171
}
173-
if (LDistance(rawMessage.ToLower(), "who is the owner?") <= 0.25) {
172+
if (LDistance(rawMessage, "who is the owner?") <= 0.25) {
174173
if (BotTime > 5) {
175174
PlayerInfo owner;
176175
if (PlayerDB.FindPlayerInfo(ConfigKey.ServerOwner.GetString(), out owner) && owner != null) {
@@ -182,24 +181,24 @@ static void checkBotResponses(Player player, string rawMessage) {
182181
player.Info.TimesUsedBot++;
183182
}
184183
}
185-
if (LDistance(rawMessage.ToLower(), "what is this server called?") <= 0.25
186-
|| LDistance(rawMessage.ToLower(), "what is the name of this server?") <= 0.25) {
184+
if (LDistance(rawMessage, "what is this server called?") <= 0.25
185+
|| LDistance(rawMessage, "what is the name of this server?") <= 0.25) {
187186
if (BotTime > 5) {
188187
Server.BotMessage("The server name is: " + ConfigKey.ServerName.GetString());
189188
player.LastServerMessageDate = DateTime.UtcNow;
190189
player.Info.TimesUsedBot++;
191190
}
192191
}
193-
if (LDistance(rawMessage.ToLower(), "where can i build?") <= 0.25
194-
|| LDistance(rawMessage.ToLower(), "where do we build") <= 0.25) {
192+
if (LDistance(rawMessage, "where can i build?") <= 0.25
193+
|| LDistance(rawMessage, "where do we build") <= 0.25) {
195194
if (BotTime > 5) {
196195
Server.BotMessage("You can build anywhere outside of spawn. Just not on another persons build unless they say you can. ");
197196
player.LastServerMessageDate = DateTime.UtcNow;
198197
player.Info.TimesUsedBot++;
199198
}
200199
}
201-
if (LDistance(rawMessage.ToLower(), "what is my next rank?") <= 0.25 ||
202-
LDistance(rawMessage.ToLower(), "what rank is after this one?") <= 0.25) {
200+
if (LDistance(rawMessage, "what is my next rank?") <= 0.25 ||
201+
LDistance(rawMessage, "what rank is after this one?") <= 0.25) {
203202
Rank next = player.Info.Rank.NextRankUp;
204203
// donor ranks are skipped from being able to be promoted to
205204
while (next != null && next.IsDonor) next = next.NextRankUp;
@@ -217,14 +216,11 @@ static void checkBotResponses(Player player, string rawMessage) {
217216
}
218217
}
219218

220-
public static float LDistance( string s, string t ) {
219+
internal static float LDistance(string s, string t) {
221220
// degenerate cases
222-
if (s == t)
223-
return 0;
224-
if (s.Length == 0)
225-
return 1;
226-
if (t.Length == 0)
227-
return 1;
221+
if (s.CaselessEquals(t)) return 0;
222+
if (s.Length == 0) return 1;
223+
if (t.Length == 0) return 1;
228224

229225
// create two work vectors of integer distances
230226
int[] v0 = new int[t.Length + 1];
@@ -242,10 +238,13 @@ public static float LDistance( string s, string t ) {
242238
// first element of v1 is A[i+1][0]
243239
// edit distance is delete (i+1) chars from s to match empty t
244240
v1[0] = i + 1;
241+
char curS = s[i]; if (curS >= 'A' && curS <= 'Z') curS += ' ';
245242

246243
// use formula to fill in the rest of the row
247244
for (int j = 0; j < t.Length; j++) {
248-
var cost = (s[i] == t[j]) ? 0 : 1;
245+
char curT = t[j]; if (curT >= 'A' && curT <= 'Z') curT += ' ';
246+
int cost = (curS == curT) ? 0 : 1;
247+
249248
v1[j + 1] = Math.Min( v1[j] + 1, v0[j + 1] + 1 );
250249
v1[j + 1] = Math.Min( v1[j + 1], v0[j] + cost );
251250
}
@@ -254,11 +253,10 @@ public static float LDistance( string s, string t ) {
254253
for (int j = 0; j < v0.Length; j++)
255254
v0[j] = v1[j];
256255
}
256+
257257
float percent = (((float)v1[t.Length]) / ((float)(s.Length + t.Length) / 2));
258-
if (percent < 0)
259-
percent = 0;
260-
if (percent > 1)
261-
percent = 1;
258+
if (percent < 0) percent = 0;
259+
if (percent > 1) percent = 1;
262260
return percent;
263261
}
264262

fCraft/Player/Player.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ public sealed partial class Player : IClassy {
5555

5656
public string quitmessage = "/Quit";
5757

58-
public string LastMessage { get; set; }
59-
public int MessageSpam { get; set; }
58+
internal string lastMsg;
59+
internal int lastMsgCount;
6060

6161
/// <summary> Last sign clicked by the player</summary>
6262
public string LastSignClicked;

0 commit comments

Comments
 (0)