Skip to content

Fix #1363: copy calcPreferredSize() result instead of retaining the reference#5109

Merged
shai-almog merged 1 commit into
masterfrom
fix-1363-preferred-size-shared-instance
May 30, 2026
Merged

Fix #1363: copy calcPreferredSize() result instead of retaining the reference#5109
shai-almog merged 1 commit into
masterfrom
fix-1363-preferred-size-shared-instance

Conversation

@shai-almog
Copy link
Copy Markdown
Collaborator

Summary

Closes #1363 — the next-oldest open issue (filed Feb 2015, 9 comments).

Component.preferredSizeImpl() cached the Dimension instance returned from calcPreferredSize() directly. When a subclass's calcPreferredSize() returns a shared Dimension — for example ContainerList.Entry.calcPreferredSize() returning the single renderer's own preferredSize field — every entry's "cached" preferred size ends up pointing at the same instance. The next time the renderer is re-measured (for the next entry), every previously-measured entry's cached size silently changes too, and all entries collapse to the most recently computed size. That's the failure mode the 2015 reporter pinpointed in comment #3.

The fix is what the reporter proposed in their first comment: copy the width/height into the component's own Dimension instead of swapping the reference. To keep callers that have already retained getPreferredSize() working (so they observe the updated values rather than a stale snapshot), we update the existing Dimension in place on re-measure and only allocate on the first measure.

Test plan

  • New regression test ComponentPreferredSizeIsolationTest covers two cases:
    1. Two components whose calcPreferredSize() returns the same Dimension end up with independent preferred-size instances (assertNotSame).
    2. Mutating the source Dimension after the first getPreferredSize() call does not alter the cached value.
  • Before the fix both tests fail on master with the exact symptoms (expected: <40> but was: <999> and expected: not same but was: <width = 40 height = 20>).
  • After the fix both tests pass.
  • No regressions across 334 tests spanning Component, Container, InputComponent, the full layout suite (Box/Border/Flow/Grid/Layered/Coordinate/Group/Mig/Table), ContainerList and the existing SpanLabel Height incorrect setAllowEnableLayoutOnPaint Regression #3000 SpanLabel/LayeredLayout preferred-size regression.
  • CI verification.

Behaviour change for consumers

  • Components whose calcPreferredSize() returns a unique Dimension every call (the vast majority — Label.calcPreferredSize() for example uses getUIManager().getLookAndFeel().getLabelPreferredSize(this) which returns a fresh Dimension) see no observable change.
  • Components whose calcPreferredSize() returns a shared Dimension (the rare case that triggered com.codename1.ui.Component.preferredSizeImpl() is unsafe as it sets the preferredSize from calcPreferredSize() which may be overridden #1363) now correctly get their own cached values that don't mutate from under them.
  • Callers that hold a long-lived reference to getPreferredSize() continue to observe live values because we update the cached Dimension in place rather than swapping the reference on re-measure.

🤖 Generated with Claude Code

…1363)

Component.preferredSizeImpl() retained the Dimension instance returned
from calcPreferredSize() as its cached preferredSize. When a subclass's
calcPreferredSize() returns a shared Dimension — for example
ContainerList.Entry.calcPreferredSize() returning the single renderer
component's own preferredSize field — every "cached" preferredSize ends
up pointing at the same instance. Re-measuring the renderer for the
next entry then silently mutates every previously-measured entry, so
all entries collapse to the most recently computed size.

Switch to copying the width/height into a Component-owned Dimension:
- allocate a new Dimension on first measure
- update width/height in place on later re-measures (preserves the
  cached reference, so callers that already hold getPreferredSize()
  see the new values without a second lookup)

Closes #1363.

Adds maven/core-unittests/.../ComponentPreferredSizeIsolationTest.java
with two regression cases:
- two components whose calcPreferredSize() returns the same Dimension
  end up with independent preferred-size instances
- mutating the source Dimension after measurement does not alter the
  cached preferred size

Full layout/component test sweep (334 tests across
Component/Container/InputComponent + Box/Border/Flow/Grid/Layered/
Coordinate/Group/Mig/Table layouts + ContainerList and the
SpanLabel+LayeredLayout preferred-size regression at #3000) is green.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@github-actions
Copy link
Copy Markdown
Contributor

Cloudflare Preview

@github-actions
Copy link
Copy Markdown
Contributor

✅ Continuous Quality Report

Test & Coverage

Static Analysis

  • SpotBugs [Report archive]
    • ByteCodeTranslator: 0 findings (no issues)
    • android: 0 findings (no issues)
    • codenameone-maven-plugin: 0 findings (no issues)
    • core-unittests: 0 findings (no issues)
    • ios: 0 findings (no issues)
  • PMD: 0 findings (no issues) [Report archive]
  • Checkstyle: 0 findings (no issues) [Report archive]

Generated automatically by the PR CI workflow.

@shai-almog
Copy link
Copy Markdown
Collaborator Author

shai-almog commented May 30, 2026

Compared 122 screenshots: 122 matched.

Native Android coverage

  • 📊 Line coverage: 12.77% (7438/58229 lines covered) [HTML preview] (artifact android-coverage-report, jacocoAndroidReport/html/index.html)
    • Other counters: instruction 10.39% (37257/358503), branch 4.28% (1450/33874), complexity 5.39% (1754/32544), method 9.46% (1442/15249), class 15.52% (330/2126)
    • Lowest covered classes
      • kotlin.collections.kotlin.collections.ArraysKt___ArraysKt – 0.00% (0/6327 lines covered)
      • kotlin.collections.unsigned.kotlin.collections.unsigned.UArraysKt___UArraysKt – 0.00% (0/2384 lines covered)
      • org.jacoco.agent.rt.internal_b6258fc.asm.org.jacoco.agent.rt.internal_b6258fc.asm.ClassReader – 0.00% (0/1519 lines covered)
      • kotlin.collections.kotlin.collections.CollectionsKt___CollectionsKt – 0.00% (0/1148 lines covered)
      • org.jacoco.agent.rt.internal_b6258fc.asm.org.jacoco.agent.rt.internal_b6258fc.asm.MethodWriter – 0.00% (0/923 lines covered)
      • kotlin.sequences.kotlin.sequences.SequencesKt___SequencesKt – 0.00% (0/730 lines covered)
      • kotlin.text.kotlin.text.StringsKt___StringsKt – 0.00% (0/623 lines covered)
      • org.jacoco.agent.rt.internal_b6258fc.asm.org.jacoco.agent.rt.internal_b6258fc.asm.Frame – 0.00% (0/564 lines covered)
      • kotlin.collections.kotlin.collections.ArraysKt___ArraysJvmKt – 0.00% (0/495 lines covered)
      • kotlinx.coroutines.kotlinx.coroutines.JobSupport – 0.00% (0/423 lines covered)

✅ Native Android screenshot tests passed.

Native Android coverage

  • 📊 Line coverage: 12.77% (7438/58229 lines covered) [HTML preview] (artifact android-coverage-report, jacocoAndroidReport/html/index.html)
    • Other counters: instruction 10.39% (37257/358503), branch 4.28% (1450/33874), complexity 5.39% (1754/32544), method 9.46% (1442/15249), class 15.52% (330/2126)
    • Lowest covered classes
      • kotlin.collections.kotlin.collections.ArraysKt___ArraysKt – 0.00% (0/6327 lines covered)
      • kotlin.collections.unsigned.kotlin.collections.unsigned.UArraysKt___UArraysKt – 0.00% (0/2384 lines covered)
      • org.jacoco.agent.rt.internal_b6258fc.asm.org.jacoco.agent.rt.internal_b6258fc.asm.ClassReader – 0.00% (0/1519 lines covered)
      • kotlin.collections.kotlin.collections.CollectionsKt___CollectionsKt – 0.00% (0/1148 lines covered)
      • org.jacoco.agent.rt.internal_b6258fc.asm.org.jacoco.agent.rt.internal_b6258fc.asm.MethodWriter – 0.00% (0/923 lines covered)
      • kotlin.sequences.kotlin.sequences.SequencesKt___SequencesKt – 0.00% (0/730 lines covered)
      • kotlin.text.kotlin.text.StringsKt___StringsKt – 0.00% (0/623 lines covered)
      • org.jacoco.agent.rt.internal_b6258fc.asm.org.jacoco.agent.rt.internal_b6258fc.asm.Frame – 0.00% (0/564 lines covered)
      • kotlin.collections.kotlin.collections.ArraysKt___ArraysJvmKt – 0.00% (0/495 lines covered)
      • kotlinx.coroutines.kotlinx.coroutines.JobSupport – 0.00% (0/423 lines covered)

Benchmark Results

Detailed Performance Metrics

Metric Duration
Base64 payload size 8192 bytes
Base64 benchmark iterations 6000
Base64 native encode 1015.000 ms
Base64 CN1 encode 281.000 ms
Base64 encode ratio (CN1/native) 0.277x (72.3% faster)
Base64 native decode 808.000 ms
Base64 CN1 decode 349.000 ms
Base64 decode ratio (CN1/native) 0.432x (56.8% faster)
Image encode benchmark status skipped (SIMD unsupported)

@shai-almog
Copy link
Copy Markdown
Collaborator Author

shai-almog commented May 30, 2026

Compared 122 screenshots: 122 matched.
✅ Native iOS Metal screenshot tests passed.

Benchmark Results

  • VM Translation Time: 0 seconds
  • Compilation Time: 139 seconds

Build and Run Timing

Metric Duration
Simulator Boot 62000 ms
Simulator Boot (Run) 0 ms
App Install 12000 ms
App Launch 7000 ms
Test Execution 275000 ms

Detailed Performance Metrics

Metric Duration
Base64 payload size 8192 bytes
Base64 benchmark iterations 6000
Base64 native encode 421.000 ms
Base64 CN1 encode 1370.000 ms
Base64 encode ratio (CN1/native) 3.254x (225.4% slower)
Base64 native decode 251.000 ms
Base64 CN1 decode 925.000 ms
Base64 decode ratio (CN1/native) 3.685x (268.5% slower)
Base64 SIMD encode 406.000 ms
Base64 encode ratio (SIMD/native) 0.964x (3.6% faster)
Base64 encode ratio (SIMD/CN1) 0.296x (70.4% faster)
Base64 SIMD decode 415.000 ms
Base64 decode ratio (SIMD/native) 1.653x (65.3% slower)
Base64 decode ratio (SIMD/CN1) 0.449x (55.1% faster)
Image encode benchmark iterations 100
Image createMask (SIMD off) 58.000 ms
Image createMask (SIMD on) 9.000 ms
Image createMask ratio (SIMD on/off) 0.155x (84.5% faster)
Image applyMask (SIMD off) 117.000 ms
Image applyMask (SIMD on) 52.000 ms
Image applyMask ratio (SIMD on/off) 0.444x (55.6% faster)
Image modifyAlpha (SIMD off) 113.000 ms
Image modifyAlpha (SIMD on) 51.000 ms
Image modifyAlpha ratio (SIMD on/off) 0.451x (54.9% faster)
Image modifyAlpha removeColor (SIMD off) 152.000 ms
Image modifyAlpha removeColor (SIMD on) 62.000 ms
Image modifyAlpha removeColor ratio (SIMD on/off) 0.408x (59.2% faster)
Image PNG encode (SIMD off) 976.000 ms
Image PNG encode (SIMD on) 874.000 ms
Image PNG encode ratio (SIMD on/off) 0.895x (10.5% faster)
Image JPEG encode 496.000 ms

@shai-almog
Copy link
Copy Markdown
Collaborator Author

shai-almog commented May 30, 2026

Compared 122 screenshots: 122 matched.
✅ Native iOS screenshot tests passed.

Benchmark Results

  • VM Translation Time: 0 seconds
  • Compilation Time: 208 seconds

Build and Run Timing

Metric Duration
Simulator Boot 86000 ms
Simulator Boot (Run) 1000 ms
App Install 20000 ms
App Launch 8000 ms
Test Execution 364000 ms

Detailed Performance Metrics

Metric Duration
Base64 payload size 8192 bytes
Base64 benchmark iterations 6000
Base64 native encode 831.000 ms
Base64 CN1 encode 1962.000 ms
Base64 encode ratio (CN1/native) 2.361x (136.1% slower)
Base64 native decode 449.000 ms
Base64 CN1 decode 1228.000 ms
Base64 decode ratio (CN1/native) 2.735x (173.5% slower)
Base64 SIMD encode 569.000 ms
Base64 encode ratio (SIMD/native) 0.685x (31.5% faster)
Base64 encode ratio (SIMD/CN1) 0.290x (71.0% faster)
Base64 SIMD decode 496.000 ms
Base64 decode ratio (SIMD/native) 1.105x (10.5% slower)
Base64 decode ratio (SIMD/CN1) 0.404x (59.6% faster)
Image encode benchmark iterations 100
Image createMask (SIMD off) 58.000 ms
Image createMask (SIMD on) 11.000 ms
Image createMask ratio (SIMD on/off) 0.190x (81.0% faster)
Image applyMask (SIMD off) 182.000 ms
Image applyMask (SIMD on) 93.000 ms
Image applyMask ratio (SIMD on/off) 0.511x (48.9% faster)
Image modifyAlpha (SIMD off) 321.000 ms
Image modifyAlpha (SIMD on) 94.000 ms
Image modifyAlpha ratio (SIMD on/off) 0.293x (70.7% faster)
Image modifyAlpha removeColor (SIMD off) 370.000 ms
Image modifyAlpha removeColor (SIMD on) 131.000 ms
Image modifyAlpha removeColor ratio (SIMD on/off) 0.354x (64.6% faster)
Image PNG encode (SIMD off) 1285.000 ms
Image PNG encode (SIMD on) 900.000 ms
Image PNG encode ratio (SIMD on/off) 0.700x (30.0% faster)
Image JPEG encode 777.000 ms

@shai-almog shai-almog merged commit b62b847 into master May 30, 2026
24 checks passed
@shai-almog shai-almog deleted the fix-1363-preferred-size-shared-instance branch May 30, 2026 07:31
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.

com.codename1.ui.Component.preferredSizeImpl() is unsafe as it sets the preferredSize from calcPreferredSize() which may be overridden

1 participant