Skip to content

Generate atomic/unpadded/varhandle variants for xadd queue family - #416

Merged
nitsanw merged 4 commits into
JCTools:masterfrom
franz1981:xadd-queue-variant-generation
Jun 15, 2026
Merged

nitsanw merged 4 commits into
JCTools:masterfrom
franz1981:xadd-queue-variant-generation

Conversation

@franz1981

Copy link
Copy Markdown
Collaborator

Extend the existing code generators to produce all 5 variant types (atomic, atomic-unpadded, unpadded, varhandle, varhandle-unpadded) for MpUnboundedXaddArrayQueue, MpUnboundedXaddChunk, and their Mpsc/Mpmc specializations. This was the only queue family without generated variants.

Key generator changes:

  • Handle Chunk class name translation in translateQueueName()
  • Support getAndAdd/getAndIncrement method accessor patterns
  • Add xadd-specific field names (producerChunk, consumerChunk, etc.)
  • Skip final fields during accessor method patching
  • Preserve type arguments when replacing SpscArrayQueue references
  • Guard against double-translation of already-translated names
  • Translate static imports from Chunk classes

Source file adjustments:

  • Move $gen:ordered-fields directive on MpUnboundedXaddChunk
  • Remove SUPPORTS_GET_AND_ADD_LONG conditional in base queue
  • Replace Arrays.fill with soLongElement loop in MpmcUnboundedXaddChunk
  • Add calcLongElementOffset to VarHandleQueueUtil

Extend the existing code generators to produce all 5 variant types
(atomic, atomic-unpadded, unpadded, varhandle, varhandle-unpadded)
for MpUnboundedXaddArrayQueue, MpUnboundedXaddChunk, and their
Mpsc/Mpmc specializations. This was the only queue family without
generated variants.

Key generator changes:
- Handle Chunk class name translation in translateQueueName()
- Support getAndAdd/getAndIncrement method accessor patterns
- Add xadd-specific field names (producerChunk, consumerChunk, etc.)
- Skip final fields during accessor method patching
- Preserve type arguments when replacing SpscArrayQueue references
- Guard against double-translation of already-translated names
- Translate static imports from Chunk classes

Source file adjustments:
- Move $gen:ordered-fields directive on MpUnboundedXaddChunk
- Remove SUPPORTS_GET_AND_ADD_LONG conditional in base queue
- Replace Arrays.fill with soLongElement loop in MpmcUnboundedXaddChunk
- Add calcLongElementOffset to VarHandleQueueUtil
@franz1981
franz1981 requested a review from nitsanw April 23, 2026 20:36

@nitsanw nitsanw left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Please document the parser changes, I will review the generated code just now

@nitsanw

nitsanw commented Apr 24, 2026

Copy link
Copy Markdown
Contributor

Where are the tests?

@franz1981

Copy link
Copy Markdown
Collaborator Author

Where are the tests?

I haven't yet updated the existing tests to cover for the new impls, good point @nitsanw

franz1981 added 2 commits May 4, 2026 13:50
Add comments explaining the purpose of each new code path in the
generators: Chunk type/name renaming visitors, static import rewriting,
getAndAdd/getAndIncrement method patching, final field skipping,
double-translation guards, SpscArrayQueue pool queue replacement,
and _OFFSET-only static field removal.
Tests: Add atomic, atomic-unpadded, unpadded parameters to existing
jctools-core xadd test files. Create 4 new VarHandle test files in
jctools-core-jdk11.

Generator fixes found by running the new tests:

- Fix translateQueueName() ordering: check "ArrayQueue" before "Chunk"
  because inner hierarchy classes like MpUnboundedXaddArrayQueueProducerChunk
  contain both patterns and must be treated as ArrayQueue classes.

- Fix AtomicReferenceFieldUpdater type for generic fields: resolve
  single-letter type parameters (e.g. R) to their erased bound
  (e.g. MpUnboundedXaddAtomicChunk) instead of using Object.class,
  which causes ClassCastException at runtime.

- Same fix for VarHandle findVarHandle: resolve generic type params
  to erased bound for the field type class argument.
@franz1981

Copy link
Copy Markdown
Collaborator Author

@nitsanw addressed your comments and run the tests on the new variants - just one note: I have noticed we have no github workflow to handle tests on PRs as well (running on ARM, x86 and MacOS) - do you want to me add it to JCTools? It seems a good idea

@nitsanw

nitsanw commented May 5, 2026

Copy link
Copy Markdown
Contributor

Sound like a good idea.

@franz1981

Copy link
Copy Markdown
Collaborator Author

Sound like a good idea.

I would do it as a follow up or in parallel, if you agree!
Let me know if i'm missing something for this PR or is fine

VarHandle polymorphic signatures require exact type matching.
Passing int 1 to getAndAdd on a long field silently compiles but
can fail at runtime with strict VarHandle type checking enabled.
@franz1981

Copy link
Copy Markdown
Collaborator Author

PTAL again @nitsanw


/**
* Replaces {@code new SpscArrayQueue<R>(...)} with the unpadded pool queue variant appropriate
* for this generator (e.g. {@code new SpscAtomicUnpaddedArrayQueue<R>(...)}).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why unpadded? The unsafe variant is padded

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I changed that one (I have to check it applies to the existing unsafe variant) because is not supposed to be an hot path and have a bigger footprint

@nitsanw nitsanw left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

OK, I guess

@nitsanw
nitsanw merged commit 0b4a3f9 into JCTools:master Jun 15, 2026
2 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.

2 participants