Skip to content

Commit

Permalink
additional perf tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Oct 23, 2021
1 parent 5b9c36e commit 8f33ebb
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 138 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
* ScriptEntry contain information about a single entry from a dScript. It is used
* by the CommandExecutor, among other parts of Denizen.
*/
public class ScriptEntry implements Cloneable, Debuggable {
public class ScriptEntry implements Cloneable, Debuggable, Iterable<Argument> {

public static class ScriptEntryInternal {

Expand All @@ -48,8 +48,6 @@ public static class ScriptEntryInternal {

public boolean hasTags = false;

public int[] processArgs = null;

public List<Argument> preprocArgs = null;

public Object specialProcessedData = null;
Expand All @@ -61,6 +59,8 @@ public static class ScriptEntryInternal {
public boolean brokenArgs = false;

public HashMap<String, Integer> argPrefixMap = null;

public ArgumentIterator argumentIterator = null;
}

public static class InternalArgument {
Expand All @@ -71,6 +71,8 @@ public static class InternalArgument {

public Argument aHArg = null;

public boolean shouldProcess = false;

public InternalArgument duplicate() {
InternalArgument newArg = new InternalArgument();
newArg.prefix = prefix == null ? null : prefix.duplicate();
Expand All @@ -80,21 +82,44 @@ public InternalArgument duplicate() {
}
}

public List<Argument> getProcessedArgs() {
for (Argument arg : aHArgs) {
arg.scriptEntry = this;
if (arg.object instanceof ElementTag && ((ElementTag) arg.object).isRawInput && arg.prefix == null) {
arg.fillStr(arg.object.toString());
}
else {
arg.unsetValue();
public static class ArgumentIterator implements Iterator<Argument> {

public ArgumentIterator(ScriptEntry entry) {
this.entry = entry;
}

public ScriptEntry entry;

public int index = 0;

@Override
public boolean hasNext() {
return index < entry.internal.args_ref.size();
}

@Override
public Argument next() {
InternalArgument internalArg = entry.internal.args_ref.get(index++);
Argument arg = internalArg.aHArg;
arg.scriptEntry = entry;
if (internalArg.shouldProcess) {
TagManager.fillArgumentObjects(internalArg, arg, entry.context);
if (arg.object instanceof ElementTag && ((ElementTag) arg.object).isRawInput && arg.prefix == null) {
arg.fillStr(arg.object.toString());
}
arg.canBeElement = arg.object instanceof ElementTag;
}
arg.canBeElement = arg.object instanceof ElementTag;
return arg;
}
return aHArgs;
}

public List<Argument> aHArgs;
@Override
public ArgumentIterator iterator() {
// NOTE: This relies strongly on the assumption of non-async execution for performance benefit.
internal.argumentIterator.index = 0;
internal.argumentIterator.entry = this;
return internal.argumentIterator;
}

public ScriptEntryData entryData;

Expand Down Expand Up @@ -133,22 +158,13 @@ public void setBracedSet(List<BracedCommand.BracedData> set) {
NULL_INTERNAL_ARGUMENT.value = TagManager.DEFAULT_PARSEABLE_EMPTY;
}

public void generateAHArgs() {
for (int i : internal.processArgs) {
Argument aHArg = internal.args_ref.get(i).aHArg.clone();
aHArg.scriptEntry = this;
aHArgs.set(i, aHArg);
}
}

@Override
public ScriptEntry clone() {
try {
ScriptEntry se = (ScriptEntry) super.clone();
se.objects = new HashMap<>(8);
se.entryData = entryData.clone();
se.entryData.scriptEntry = se;
se.aHArgs = new ArrayList<>(aHArgs);
se.updateContext();
return se;
}
Expand Down Expand Up @@ -281,8 +297,6 @@ else if (internal.actualCommand != null) {
nested_depth = 0;
TagContext refContext = DenizenCore.getImplementation().getTagContext(this);
internal.args_ref = new ArrayList<>(internal.pre_tagged_args.size());
List<Integer> tempProcessArgs = new ArrayList<>(internal.pre_tagged_args.size());
aHArgs = new ArrayList<>(internal.pre_tagged_args.size());
for (int i = 0; i < internal.pre_tagged_args.size(); i++) {
String arg = internal.pre_tagged_args.get(i);
if (arg.equals("{")) {
Expand Down Expand Up @@ -316,23 +330,16 @@ else if (internal.actualCommand != null) {
}
}
crunchInto(argVal, arg, refContext);
if (argVal.value.hasTag || argVal.prefix != null) {
tempProcessArgs.add(aHArgs.size());
if ((argVal.value.hasTag || argVal.prefix != null) && (internal.actualCommand == null || internal.actualCommand.shouldPreParse())) {
argVal.shouldProcess = true;
}
aHArgs.add(argVal.aHArg);
}
internal.processArgs = new int[tempProcessArgs.size()];
for (int i = 0; i < tempProcessArgs.size(); i++) {
internal.processArgs[i] = tempProcessArgs.get(i);
}
}
else {
internal.pre_tagged_args = new ArrayList<>();
internal.preprocArgs = new ArrayList<>();
internal.pre_tagged_args = new ArrayList<>();
internal.processArgs = new int[0];
internal.args_ref = new ArrayList<>();
aHArgs = new ArrayList<>();
}
if (internal.actualCommand != null) {
int argCount = getOriginalArguments().size();
Expand All @@ -347,6 +354,7 @@ else if (internal.actualCommand != null) {
else {
internal.actualCommand = CommandRegistry.debugInvalidCommand;
}
internal.argumentIterator = new ArgumentIterator(this);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,23 +45,16 @@ public static List<BracedData> getBracedCommands(ScriptEntry scriptEntry, boolea
if (scriptEntry == null) {
return null;
}

boolean hyperdebug = Debug.verbose;

// And a place to store all the final braces...
List<BracedData> bracedSections;

List<BracedData> entryBracedSet = scriptEntry.getBracedSet();
if (entryBracedSet != null) {
if (!duplicate) {
return entryBracedSet;
}
List<BracedData> res = new ArrayList<>(entryBracedSet);
List<BracedData> res = new ArrayList<>(entryBracedSet.size());
try {
for (int i = 0; i < res.size(); i++) {
for (BracedData bd : entryBracedSet) {
BracedData newbd = new BracedData();
BracedData bd = res.get(i);
res.set(i, newbd);
newbd.key = bd.key;
newbd.value = new ArrayList<>(bd.value.size());
for (ScriptEntry sEntry : bd.value) {
Expand All @@ -74,14 +67,14 @@ public static List<BracedData> getBracedCommands(ScriptEntry scriptEntry, boolea
Debug.echoDebug(scriptEntry, "Wrangling braced command args: " + bd.key);
}
newbd.args = bd.args;
res.add(newbd);
}
}
catch (Exception e) {
Debug.echoError(scriptEntry.getResidingQueue(), e);
}
return res;
}

if (scriptEntry.getInsideList() != null) {
List<Object> contents = scriptEntry.getInsideList();
List<ScriptEntry> entries = ScriptBuilder.buildScriptEntries(contents,
Expand All @@ -96,42 +89,26 @@ public static List<BracedData> getBracedCommands(ScriptEntry scriptEntry, boolea
return getBracedCommands(scriptEntry);
}
bracedSections = new ArrayList<>();

// We need a place to store the commands being built at...
TreeMap<Integer, ArrayList<String>> commandList = new TreeMap<>();

int bracesEntered = 0;
boolean newCommand = true;
boolean waitingForDash = false;

// Inject the scriptEntry into the front of the queue, otherwise it doesn't exist
//scriptEntry.getResidingQueue().injectEntry(scriptEntry, 0);
// Send info to debug
if (hyperdebug) {
if (Debug.verbose) {
Debug.echoDebug(scriptEntry, "Starting getBracedCommands...");
}

// If the specified amount of possible entries is less than the queue size, print that instead
//if (hyperdebug) dB.echoDebug(scriptEntry, "...with queue size: " + scriptEntry.getResidingQueue().getQueueSize());
if (hyperdebug) {
Debug.echoDebug(scriptEntry, "...with first command name: " + scriptEntry.getCommandName());
}
if (hyperdebug) {
Debug.echoDebug(scriptEntry, "...with first command arguments: " + scriptEntry.getOriginalArguments());
}

if (hyperdebug) {
Debug.echoDebug(scriptEntry, "Entry found: " + scriptEntry.getCommandName());
}

// Loop through the arguments of each entry
List<String> argList = scriptEntry.getOriginalArguments();

// Set the variable to use for naming braced command lists; the first should be the command name
String bracesName = scriptEntry.getCommandName().toUpperCase();
ArrayList<String> bracesArgs = new ArrayList<>();
bracesArgs.add(bracesName);

int startArg = -1;
for (int i = 0; i < argList.size(); i++) {
String arg = argList.get(i);
Expand All @@ -140,24 +117,19 @@ public static List<BracedData> getBracedCommands(ScriptEntry scriptEntry, boolea
break;
}
}

if (startArg == -1) {
return null;
}

if (scriptEntry.getScript() != null) { // Ex command is temporarily allowed to bypass, to avoid annoying user testing
Deprecations.oldBraceSyntax.warn(scriptEntry);
}

int tStart = -1;
int tEnd = -1;

for (int i = startArg; i < argList.size(); i++) {
String arg = argList.get(i);
if (hyperdebug) {
if (Debug.verbose) {
Debug.echoDebug(scriptEntry, "Arg found: " + arg);
}

// Listen for opened braces
if (arg.equals("{")) {
bracesEntered++;
Expand All @@ -169,7 +141,7 @@ public static List<BracedData> getBracedCommands(ScriptEntry scriptEntry, boolea
}
newCommand = false;
waitingForDash = bracesEntered == 1;
if (hyperdebug) {
if (Debug.verbose) {
Debug.echoDebug(scriptEntry, "Opened brace; " + bracesEntered + " now");
}
if (bracesEntered > 1) {
Expand All @@ -181,7 +153,7 @@ public static List<BracedData> getBracedCommands(ScriptEntry scriptEntry, boolea
else if (arg.equals("}")) {
bracesEntered--;
newCommand = false;
if (hyperdebug) {
if (Debug.verbose) {
Debug.echoDebug(scriptEntry, "Closed brace; " + bracesEntered + " now");
}
if (bracesEntered > 0) {
Expand All @@ -197,13 +169,13 @@ else if (arg.equals("}")) {
ArrayList<ScriptEntry> bracesSection = new ArrayList<>();
for (ArrayList<String> command : commandList.values()) {
if (command.isEmpty()) {
if (hyperdebug) {
if (Debug.verbose) {
Debug.echoError(scriptEntry.getResidingQueue(), "Empty command?");
}
continue;
}
String cmd = command.get(0);
if (hyperdebug) {
if (Debug.verbose) {
Debug.echoDebug(scriptEntry, "Calculating " + cmd);
}
command.remove(0);
Expand All @@ -220,11 +192,11 @@ else if (arg.equals("}")) {
newEntry.internal.originalLine = newEntry.toString();
bracesSection.add(newEntry);
bracesSection.get(bracesSection.size() - 1).entryData.transferDataFrom(scriptEntry.entryData);
if (hyperdebug) {
if (Debug.verbose) {
Debug.echoDebug(scriptEntry, "Command added: " + cmd + ", with " + args.length + " arguments");
}
}
if (hyperdebug) {
if (Debug.verbose) {
Debug.echoDebug(scriptEntry, "Adding section " + bracesName + " with " + tStart + " to " + tEnd);
}
bd.args = bracesArgs;
Expand All @@ -245,7 +217,7 @@ else if (newCommand && bracesEntered == 1) {
commandList.put(commandList.size(), new ArrayList<>());
commandList.get(commandList.lastKey()).add(arg);
newCommand = false;
if (hyperdebug) {
if (Debug.verbose) {
Debug.echoDebug(scriptEntry, "Treating as new command");
}
}
Expand All @@ -254,7 +226,7 @@ else if (newCommand && bracesEntered == 1) {
else if (arg.equals("-") && bracesEntered == 1) {
newCommand = true;
waitingForDash = false;
if (hyperdebug) {
if (Debug.verbose) {
Debug.echoDebug(scriptEntry, "Assuming following is a new command");
}
}
Expand All @@ -273,7 +245,7 @@ else if (bracesEntered == 0) {
}
newCommand = false;
commandList.get(commandList.lastKey()).add(arg);
if (hyperdebug) {
if (Debug.verbose) {
Debug.echoDebug(scriptEntry, "Adding to the command");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ public boolean execute(ScriptEntry scriptEntry) {
}
String saveName = null;
try {
scriptEntry.generateAHArgs();
TagContext context = scriptEntry.getContext();
for (Argument arg : scriptEntry.internal.preprocArgs) {
if (DenizenCore.getImplementation().handleCustomArgs(scriptEntry, arg, false)) {
Expand All @@ -113,9 +112,6 @@ else if (arg.matchesPrefix("save")) {
}
}
}
if (scriptEntry.internal.actualCommand.shouldPreParse()) {
TagManager.fillArgumentsObjects(scriptEntry.internal.args_ref, scriptEntry.aHArgs, context, scriptEntry.internal.processArgs);
}
command.parseArgs(scriptEntry);
command.execute(scriptEntry);
if (saveName != null) {
Expand Down
Loading

0 comments on commit 8f33ebb

Please sign in to comment.