Skip to content

EmbeddingUtils.toPrimitive(List<Float> floats) is suboptimal and can be twice as fast #4346

@wpanas

Description

@wpanas

Bug description
A clear and concise description of what the bug is about.

EmbeddingUtils.toPrimitive(List<Float> floats) unnecessarily converts List<Float> to Float[] that is later converted to float[]. I achieved twice as much operations per seconds by directly converting from List<Float> to float[]

Benchmark                    Mode  Cnt        Score       Error  Units
MyBenchmark.toPrimitive     thrpt    5  1671525,687 ± 19770,013  ops/s
MyBenchmark.toPrimitiveNew  thrpt    5  3107477,889 ± 29323,152  ops/s

Environment
Please provide as many details as possible: Spring AI version, Java version, which vector store you use if any, etc

  • Spring AI 1.0.1
  • I benchmarked it on Java 21

Steps to reproduce
Steps to reproduce the issue.

  • Run benchmark with long List<Float>. I created lists of 768 floats, but text-embedding-3-large can be vector of 3072 floats.

Expected behavior
A clear and concise description of what you expected to happen.
I'll create a PR for fixing it and I'd like you to merge it 😄

Minimal Complete Reproducible example
Please provide a failing test or a minimal complete verifiable example that reproduces the issue.
Bug reports that are reproducible will take priority in resolution over reports that are not reproducible.

Metadata

Metadata

Assignees

Type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions