Skip to content

Optimize Kotlin primary constructor fixture generation#1283

Merged
seongahjo merged 1 commit into
naver:mainfrom
char-yb:optimize-kotlin-constructor
May 28, 2026
Merged

Optimize Kotlin primary constructor fixture generation#1283
seongahjo merged 1 commit into
naver:mainfrom
char-yb:optimize-kotlin-constructor

Conversation

@char-yb
Copy link
Copy Markdown
Contributor

@char-yb char-yb commented May 22, 2026

Summary

This PR optimizes Kotlin primary constructor-based fixture generation without adding new dependencies or changing public APIs.

It reduces per-sample overhead in Kotlin object creation paths, which can become more visible when tests run under JVM instrumentation tools such as Kover.

(Optional): Description

The previous implementation always used callBy(...) and built intermediate maps for every generated Kotlin object. callBy(...) is still necessary when optional constructor parameters should be omitted, but it is more expensive than needed when all parameters are already available.

This PR keeps the existing optional-parameter behavior intact and only uses the faster KFunction.call(*args) path when every constructor parameter can be provided.

Changes include:

  • Reduce repeated actualType() / cachedKotlin() calls in PrimaryConstructorArbitraryIntrospector.match().
  • Avoid mapKeys allocation in the constructor invocation path by building the name-value map directly.
  • Use KFunction.call(*args) when no optional constructor parameter needs to be skipped.
  • Keep callBy(...) when optional parameters must be skipped.
  • Preserve explicit null behavior for nullable optional parameters.
  • Change constructor parameter name lookup from List.contains to Set.contains.
  • Add regression tests for optional default values and nullable optional default values.

Local quick JMH benchmark:

Version Score
Before 1214.915 ms/op
After 904.979 ms/op

Benchmark command:

./gradlew :fixture-monkey-benchmarks:fixture-monkey-benchmark-kotlin:jmh \
  -Pjmh.quick \
  '-Pjmh.include=.*primaryConstructorGenerateKotlinOrderSheetWithFixtureMonkey.*'

How Has This Been Tested?

./gradlew :fixture-monkey-kotlin:test :fixture-monkey-kotlin:ktlintCheck
./gradlew :fixture-monkey-tests:kotlin-tests:test

I also verified the benchmark above locally to compare the Kotlin primary constructor fixture generation path before and after this change.

Is the Document updated?

No.

This PR does not change public APIs, user-facing behavior, configuration, or documented usage. It only optimizes the existing Kotlin primary constructor generation path.

Please feel free to share any feedback, especially if there are concerns about the constructor invocation behavior or benchmark scope.

@CLAassistant
Copy link
Copy Markdown

CLAassistant commented May 22, 2026

CLA assistant check
All committers have signed the CLA.

Copy link
Copy Markdown
Contributor

@seongahjo seongahjo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@seongahjo seongahjo added this to the 1.1.21 milestone May 28, 2026
@seongahjo seongahjo merged commit d337a72 into naver:main May 28, 2026
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants