Skip to content

[BUG] Extremely slow validation on repeated field with items validation #304

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

Open
andrey-filenko-quarks-tech opened this issue May 14, 2025 · 2 comments
Assignees
Labels
Bug Something isn't working

Comments

@andrey-filenko-quarks-tech
Copy link

andrey-filenko-quarks-tech commented May 14, 2025

Description

When I am using validation rules on items for repeated fields with env variable PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=upb, validation is extremely slow

Steps to Reproduce

Using the following message:

message RepeatedItems {
  repeated int64 items = 1 [
    (buf.validate.field).repeated = {
      items: {
        int64: {gt: 0}
      }
    }
  ];
}

Run this code:

import protovalidate
import time
from gen.tests.example.v1 import validations_pb2

msg = validations_pb2.RepeatedItems(items=[i + 1 for i in range(2000)])

start = time.perf_counter()
protovalidate.validate(msg)
end = time.perf_counter()
print(f"Finished one in {end-start:.2f} seconds")

Is executing about:

PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=upb python main.py
Finished one in 2.08 seconds

and with PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python:

PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python python main.py
Finished one in 0.01 seconds

But I guess both are pretty slow for just a validation call.

I have tried pre-warming up and doing it in a loop, however, numbers are the same after first call.

Environment

  • Operating System: macOS Sequoia
  • Compiler/Toolchain: Apple clang version 17.0.0 (clang-1700.0.13.3)
  • Protobuf Compiler & Version: buf v1.54.0
  • Protovalidate Version: v0.8.0
@andrey-filenko-quarks-tech andrey-filenko-quarks-tech added the Bug Something isn't working label May 14, 2025
@smaye81
Copy link
Member

smaye81 commented May 14, 2025

Hi @andrey-filenko-quarks-tech can you provide a bit more info:

  • Python version
  • Protobuf version
  • buf.gen.yaml (if not using Buf, show protoc command for generating code)

I was able to recreate this on my machine (Apple M1). However, I'm not seeing the 0.01s result for using the python backend. I actually see comparable (albeit slow) results for both:

$ PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=upb python3 main.py
Finished one in 3.83 seconds

$ PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python python3 main.py
Finished one in 3.92 second

Definitely something we should take a look at, but want to gather all the information first.

@andrey-filenko-quarks-tech
Copy link
Author

Hi @smaye81

  1. Python 3.11.5
  2. Protobuf Python Version: 6.30.1
  3. I have provided an example with slow validation by generating it in this pull request, so, I guess buf.gen.yaml is the same as in main branch of the current repository

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants