Skip to content

Commit

Permalink
🦛 systemd: reduce allocations
Browse files Browse the repository at this point in the history
  • Loading branch information
database64128 committed Mar 22, 2024
1 parent 36d0635 commit 4c53d55
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 15 deletions.
24 changes: 17 additions & 7 deletions CubicBot.Telegram/Commands/Systemd.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ public static class Systemd
Supported commands: *start*, *stop*, *restart*, *reload*\.
Reply to a message to use the sender's name as the unit\.";

private enum SystemdUnitType
{
Service,
Timer,
Target,
}

public static void AddCommands(List<CubicBotCommand> commands)
{
commands.Add(new("systemctl", "➡️ systemctl <command> [unit]", SystemctlAsync, statsCollector: CountSystemctlCalls));
Expand All @@ -41,29 +48,32 @@ public static Task SystemctlAsync(CommandContext commandContext, CancellationTok
return SendHelpAsync(commandContext, @"Missing command\.", cancellationToken);
}

string command, unit;
SystemdUnitType unitType = SystemdUnitType.Service;
ReadOnlySpan<char> command;
string unit;
SystemdUnitType unitType;

var spacePos = argument.IndexOf(' ');
if (spacePos == -1 && commandContext.Message.ReplyToMessage?.From?.FirstName is string firstname)
{
command = argument;
unit = firstname;
unitType = SystemdUnitType.Service;
}
else if (spacePos > 0 && spacePos < argument.Length - 1)
{
command = argument[..spacePos];
command = argument.AsSpan()[..spacePos];
unit = argument[(spacePos + 1)..];
var dotPos = unit.LastIndexOf('.');
if (dotPos != -1)
unitType = dotPos switch
{
unitType = unit[dotPos..] switch
-1 => SystemdUnitType.Service,
_ => unit.AsSpan()[dotPos..] switch
{
".timer" => SystemdUnitType.Timer,
".target" => SystemdUnitType.Target,
_ => SystemdUnitType.Service,
};
}
},
};
}
else
{
Expand Down
8 changes: 0 additions & 8 deletions CubicBot.Telegram/Commands/SystemdUnitType.cs

This file was deleted.

0 comments on commit 4c53d55

Please sign in to comment.