Skip to content

SpelCompiler is unable to compile too often #23963

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
tonyschwartz opened this issue Nov 9, 2019 · 5 comments
Closed

SpelCompiler is unable to compile too often #23963

tonyschwartz opened this issue Nov 9, 2019 · 5 comments
Labels
in: core Issues in core modules (aop, beans, core, context, expression)

Comments

@tonyschwartz
Copy link

tonyschwartz commented Nov 9, 2019

I need to allow allow a user to build an expression that will be used to evaluate a boolean result against millions of records per second, so I need to ensure the expression is compiled. There is an apparent problem with the way the spring expression framework attempts to compile the expression.

Let's say I have this expression: productType == 'Firewall' and categoryId == 400

The issue is: If the first 100 records that are sent through this expression never contain productType="Firewall", then categoryId is never evaluated by the parser due to short-circuiting of the boolean "and" operator. Deep in the code, in PropertyOrFieldReference, this.cachedReadAccessor is always null for the categoryId field. Because of this, the spring expression parser will never be able to compile this expression.

Does anyone have any recommendations for me?

I'd like to use this framework for this use case, but I need to ensure these expressions are compiled.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Nov 9, 2019
@aclement
Copy link
Contributor

aclement commented Nov 12, 2019

Yeah, it is optimizing itself based on the shape of data it sees. I feel like there is a bug to investigate here but I am thinking about ways to workaround that for now.

You could:

  • run the expression over items that do have a productType (test items that have the right structure) to create the right fields in the ast nodes then it will compile, then apply it to the data in question. Whether this works might depend on how the objects differ between those that have the productType and those that don't - should I be guessing the ones missing it are missing the field entirely? If they are entirely different it'll crash the first time you hit an object missing the field.

  • adjust it to a (#productType(#this)=='Firewall' and categoryId==400) and add an implementation of that productType function to the evaluation context. It should be compilable even if that function is returning null for the first few objects (obviously you don't want that function using reflection though, but I'm not quite sure about the domain objects involved here - but maybe the function can use an 'instanceof' check (if (object instanceof ThingWithProductType) return ((ThingWithProductType)object).getProductType())?

If you shared a bit more on the object types being evaluated against, I might be able to come up with other options.

@sbrannen sbrannen added in: core Issues in core modules (aop, beans, core, context, expression) status: waiting-for-feedback We need additional information before we can continue and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Nov 13, 2019
@sbrannen sbrannen changed the title SpelCompiler Unable to compile too often SpelCompiler is unable to compile too often Nov 13, 2019
@sbrannen
Copy link
Member

If you cannot register a custom function, you can always invoke a static method from one of your classes or a method on a bean in the ApplicationContext.

@spring-projects-issues
Copy link
Collaborator

If you would like us to look at this issue, please provide the requested information. If the information is not provided within the next 7 days this issue will be closed.

@spring-projects-issues spring-projects-issues added the status: feedback-reminder We've sent a reminder that we need additional information before we can continue label Nov 20, 2019
@spring-projects-issues
Copy link
Collaborator

Closing due to lack of requested feedback. If you would like us to look at this issue, please provide the requested information and we will re-open the issue.

@spring-projects-issues spring-projects-issues removed status: waiting-for-feedback We need additional information before we can continue status: feedback-reminder We've sent a reminder that we need additional information before we can continue labels Nov 27, 2019
@sbrannen
Copy link
Member

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core Issues in core modules (aop, beans, core, context, expression)
Projects
None yet
Development

No branches or pull requests

4 participants