Performance issue for validating list with 20k elements

Description

We have a PUT REST endpoint, which accepts an autogenerated object, which is in fact a list of objects. We send a request there, which contains around 23k of elements. Elements in that list have a couple of concrete implementations, each has a couple of required fields and a list with a couple more elements of the same type. When hibernate validator is enabled on that endpoint - it takes somewhere between 30 and 50 seconds to validate everything. When I replace that validation with manually written methods - it takes around 0.5 seconds to validate the same. The sample project to reproduce this is here:

If you are on Linux, just run the following command in the root of the project (make sure your JAVA_HOME points to Java 17):

Or this on Windows:

And you’ll see these logs:

Which are basically about running three tests:

  • endpoint_with_explicit_validation - validation is done with manually written method

  • endpoint_with_validation - validation is done using Hibernate Validator

  • endpoint_without_validation - no validation at all

If you want to see what exactly is being validated - uncomment this line:

I’m using the latest Spring Boot version (3.0.1), Java 17.0.5 (Amazon Corretto) and Maven 3.8.6

P.S. I’m not sure if this should be a Bug or rather an Improvement, so left the default one there.

Environment

None

Activity

Show:

Anatoliy Balakirev January 11, 2023 at 8:27 PM

Hi Guillaume. Sure, will keep the project, no problem.

For the plugin, I’ve created a ticket for them here:

Will also try to create a PR with the fix there.

Guillaume Smet January 11, 2023 at 11:38 AM

can you keep your GitHub project around? I’d like to have it when we restart the work on optimizing this case.

Thanks.

As for openapi-generator-maven-plugin an easy first step would be for them to avoid adding @Valid for types whose package starts with java..

Feel free to ping me if you open an issue there and you need some help.

Anatoliy Balakirev January 10, 2023 at 4:37 PM

Hi Marko. Thanks for the hint! Indeed, when I remove those not needed @Valid annotations - it gets much much faster. Goes all the way down to the sub-second range (around 700ms) from 20-50 seconds and that’s even without ... Now I only need to figure out how to make openapi-generator-maven-plugin not to generate those But that’s a separate story anyway. Thanks for the help and I guess this ticket can be closed as duplicated then.

Marko Bekhta January 10, 2023 at 3:16 PM

hey, thanks for reporting the issue and providing the code examples. Please see linked issues - and

Looks like in your case you have a combination of both. You don’t really need to have @Valid unless you expect to do cascading validation (so places like private @Valid String currencyCode; shouldn’t use @Valid). And well the fact that you have a huge list of objects for cascading degrades the performance ( should address that part)

Anatoliy Balakirev January 10, 2023 at 2:50 PM

It could also be that this is actually an expected performance and there is not much we can do here, but I would at least like to get that feedback then, to make a decision on our side whether we should use Hibernate Validator to validate such a big objects or not.

Duplicate

Details

Assignee

Reporter

Bug Testcase Reminder (view)

Bug reports should generally be accompanied by a test case!

Bug Testcase Reminder (edit)

Bug reports should generally be accompanied by a test case!

Participants

Anatoliy Balakirev
Guillaume Smet
Marko Bekhta

Components

Affects versions

Priority

Created January 10, 2023 at 2:29 PM
Updated January 11, 2023 at 8:27 PM
Resolved January 11, 2023 at 8:29 AM