Skip to content

Replace if == Operators 50+ rows in StandardEvaluator execute with something faster? #36

Closed

Description

@JaredDavis22

What are your thoughts on refactoring the execute method to use a switch statement?

  1. Method is testing for unimplemented features. ie. Operators.DOT_...

  2. Order of tests vs formula usage may make execution time vary on a case by case basis

  3. 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;
	}

Activity

  1. JaredDavis22 commented on Aug 26, 2026

    @JaredDavis22
    ContributorAuthor

    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.

  2. JaredDavis22 commented on Aug 26, 2026

    @JaredDavis22
    ContributorAuthor

    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.

  3. JaredDavis22 commented on Nov 15, 2026

    @JaredDavis22
    ContributorAuthor

    fasterParsington fork has this in main.

    https://github.com/JaredDavis22/fasterParsington

  4. ctrueden commented on Nov 17, 2026

    @ctrueden
    Member

    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 Operators class.

    Still, the ~9% average speedup is enough that I am reluctant to reject this direction; my idea is to change Operator from class to interface, then rework Operators itself into an enum StandardOperator implements Operator. This would:

    • Maintain a single source of truth. Each standard operator is defined once, as an enum constant carrying its symbol, arity, associativity and precedence. No parallel OperatorKind list.
    • Shorter code elsewhere. standardList() becomes the enum's values plus the three groups, and evaluators switch on the operator itself.

    But it would be a breaking change:

    1. It would require Token to become an interface too, which Variable, Unresolved, Function and Group would all implement.
    2. Operator would need to drop Comparable<Operator>, because Enum<E> already implements Comparable<E> with a final compareTo that orders by declaration position. Any precedence ordering would have to move to a Comparator (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.

  5. added this to the 4.0.0 milestone on Nov 17, 2026
  6. JaredDavis22 commented on Nov 18, 2026

    @JaredDavis22
    ContributorAuthor

    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?

  7. ctrueden commented on Nov 19, 2026

    @ctrueden
    Member

    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 made Token and Operator classes 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 need Abstract and Default base 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 no Abstract or Default class layers with very little code duplication.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions