Repository navigation
Closed
Milestone
Description
What are your thoughts on refactoring the execute method to use a switch statement?
-
Method is testing for unimplemented features. ie. Operators.DOT_...
-
Order of tests vs formula usage may make execution time vary on a case by case basis
-
Easiest, perhaps the ugliest, is to add an unique id to Operators that is a compile time constant. That unique id would then be used in a switch statement. I may do that first to see if there is a performance impact. enum ?
default Object execute(final Operator op, final Object... args) {
final Object a = args.length > 0 ? args[0] : null;
final Object b = args.length > 1 ? args[1] : null;
// Let the case logic begin!
if (op instanceof Function) return function(a, b);
if (op == Operators.DOT) return dot(a, b);
if (Tokens.isMatchingGroup(op, Operators.PARENS)) return parens(args);
if (Tokens.isMatchingGroup(op, Operators.BRACKETS)) return brackets(args);
if (Tokens.isMatchingGroup(op, Operators.BRACES)) return braces(args);
if (op == Operators.TRANSPOSE) return transpose(a);
if (op == Operators.DOT_TRANSPOSE) return dotTranspose(a);
if (op == Operators.POW) return pow(a, b);
if (op == Operators.DOT_POW) return dotPow(a, b);
if (op == Operators.POST_INC) return postInc(a);
if (op == Operators.POST_DEC) return postDec(a);
if (op == Operators.PRE_INC) return preInc(a);
if (op == Operators.PRE_DEC) return preDec(a);
if (op == Operators.POS) return pos(a);
if (op == Operators.NEG) return neg(a);
if (op == Operators.COMPLEMENT) return complement(a);
if (op == Operators.NOT) return not(a);
if (op == Operators.MUL) return mul(a, b);
if (op == Operators.DIV) return div(a, b);
if (op == Operators.MOD) return mod(a, b);
if (op == Operators.RIGHT_DIV) return rightDiv(a, b);
if (op == Operators.DOT_MUL) return dotMul(a, b);
if (op == Operators.DOT_DIV) return dotDiv(a, b);
if (op == Operators.DOT_RIGHT_DIV) return dotRightDiv(a, b);
if (op == Operators.ADD) return add(a, b);
if (op == Operators.SUB) return sub(a, b);
if (op == Operators.LEFT_SHIFT) return leftShift(a, b);
if (op == Operators.RIGHT_SHIFT) return rightShift(a, b);
if (op == Operators.UNSIGNED_RIGHT_SHIFT) return unsignedRightShift(a, b);
if (op == Operators.LESS_THAN) return lessThan(a, b);
if (op == Operators.GREATER_THAN) return greaterThan(a, b);
if (op == Operators.LESS_THAN_OR_EQUAL) return lessThanOrEqual(a, b);
if (op == Operators.GREATER_THAN_OR_EQUAL) return greaterThanOrEqual(a, b);
if (op == Operators.INSTANCEOF) return instanceOf(a, b);
if (op == Operators.EQUAL) return equal(a, b);
if (op == Operators.NOT_EQUAL) return notEqual(a, b);
if (op == Operators.BITWISE_AND) return bitwiseAnd(a, b);
if (op == Operators.BITWISE_OR) return bitwiseOr(a, b);
if (op == Operators.LOGICAL_AND) return logicalAnd(a, b);
if (op == Operators.LOGICAL_OR) return logicalOr(a, b);
if (op == Operators.QUESTION) return question(a, b);
if (op == Operators.COLON) return colon(a, b);
if (op == Operators.ASSIGN) return assign(a, b);
if (op == Operators.POW_ASSIGN) return powAssign(a, b);
if (op == Operators.DOT_POW_ASSIGN) return dotPowAssign(a, b);
if (op == Operators.MUL_ASSIGN) return mulAssign(a, b);
if (op == Operators.DIV_ASSIGN) return divAssign(a, b);
if (op == Operators.MOD_ASSIGN) return modAssign(a, b);
if (op == Operators.RIGHT_DIV_ASSIGN) return rightDivAssign(a, b);
if (op == Operators.DOT_DIV_ASSIGN) return dotDivAssign(a, b);
if (op == Operators.DOT_RIGHT_DIV_ASSIGN) return dotRightDivAssign(a, b);
if (op == Operators.ADD_ASSIGN) return addAssign(a, b);
if (op == Operators.SUB_ASSIGN) return subAssign(a, b);
if (op == Operators.AND_ASSIGN) return andAssign(a, b);
if (op == Operators.OR_ASSIGN) return orAssign(a, b);
if (op == Operators.LEFT_SHIFT_ASSIGN) return leftShiftAssign(a, b);
if (op == Operators.RIGHT_SHIFT_ASSIGN) return rightShiftAssign(a, b);
if (op == Operators.UNSIGNED_RIGHT_SHIFT_ASSIGN) return unsignedRightShiftAssign(a, b);
// Unknown operator.
return null;
}
Metadata
Metadata
Assignees
Labels
No labels
Activity
JaredDavis22 commentedon Aug 26, 2026
Sample changed code is in https://github.com/JaredDavis22/parsingtonFork/tree/regularExpressionRemovalAndString
It is faster on my test case by about 7%. I did not run the benchmark code.
JaredDavis22 commentedon Aug 26, 2026
I did two other new changes in the same branch. One reduced Objects[] creation and the other uses a char array for the Operator first char lookup. Both suggested/implemented by Claude Code.
JaredDavis22 commentedon Nov 15, 2026
fasterParsington fork has this in main.
https://github.com/JaredDavis22/fasterParsington
ctrueden commentedon Nov 17, 2026
I played a lot with this direction today. I pushed a WIP exploring it (dc228ee), but I do not like that it creates a shadow data structure in the form of an enum that must be kept aligned with the
Operatorsclass.Still, the ~9% average speedup is enough that I am reluctant to reject this direction; my idea is to change
Operatorfrom class to interface, then reworkOperatorsitself into anenum StandardOperator implements Operator. This would:OperatorKindlist.standardList()becomes the enum's values plus the three groups, and evaluators switch on the operator itself.But it would be a breaking change:
Tokento become an interface too, whichVariable,Unresolved,FunctionandGroupwould all implement.Operatorwould need to dropComparable<Operator>, becauseEnum<E>already implementsComparable<E>with a finalcompareTothat orders by declaration position. Any precedence ordering would have to move to aComparator(although Parsington may not actually use it as of this writing).So these changes would be part of a Parsington 4.0.0, maybe along with the changes in #32.
JaredDavis22 commentedon Nov 18, 2026
Hi - Hope all is well. Seems like a lot of changes to make an infrequent code change easier. With AI making the change x% of the time, does it matter that it has to be changed in sync in two places? The op method forces you to create a new enum. Then search where the enum are used. It flows well. Yes, the OperatorKind is not pretty. Is it worth a major change for all users?
WIP: Add a "kind" field to Operator, for switching
ctrueden commentedon Nov 19, 2026
Because #32 already broke backwards API compatibility, I decided to move forward with prototyping the
enumification of the standard operators, and I ended up really liking the result. It makes evaluation ~10% faster, while also making the standard operators logic IMHO a bit nicer, at a cost of less than 100 net lines of code. (Historical context: the reason I madeTokenandOperatorclasses rather than interfaces at the beginning is because I wanted to keep the library simple with a minimal number of classes, and I reasoned that making them interfaces would also needAbstractandDefaultbase classes to go with them. But that was back in the Java 1.6 era before default interface methods; with Java 1.8+ we are able to get away with noAbstractorDefaultclass layers with very little code duplication.)