Skip to content

Conversation

@andeplane
Copy link
Owner

@andeplane andeplane commented Nov 1, 2025

Summary

Fixes #185

Extracts common Data1D processing logic from the three sync modifiers into a shared helper function, reducing code duplication by ~80%.

Changes

  • Created shared processData1D() helper function in src/modifiers/modifier.ts
  • Extracted common logic for:
    • Getting data1DNames wrapper and extracting size
    • Checking if data1D needs to be created
    • Processing clearPerSync logic
    • Looping through data1DVector and extracting x/y values from WASM memory
    • Proper cleanup of all WASM objects

Benefits

  • Reduces code duplication: ~125 lines per file → ~50-60 lines per modifier
  • Easier maintenance: Fix bugs/improvements in one place
  • Consistent WASM memory cleanup: Ensures all modifiers handle cleanup the same way
  • Easier to add new modifiers: Future Data1D processing modifiers can reuse the helper

Files Modified

  • src/modifiers/modifier.ts (added helper function)

This pull request contains changes generated by a Cursor Cloud Agent

Open in Cursor Open in Web

- Created shared processData1D helper function in modifier.ts
- Refactored synccomputesmodifier.ts to use shared helper
- Refactored syncfixesmodifier.ts to use shared helper
- Refactored syncvariablesmodifier.ts to use shared helper
- Reduced code duplication from ~125 lines per file to ~10-15 lines
- Ensures consistent WASM memory cleanup across all modifiers

Fixes #185
@cursor
Copy link

cursor bot commented Nov 1, 2025

Cursor Agent can help with this pull request. Just @cursor in comments and I'll start working on changes in this branch.
Learn more about Cursor Agents

@andeplane andeplane changed the title Solve issue 185 and create PR refactor: Extract common Data1D processing logic from sync modifiers Nov 1, 2025
@andeplane
Copy link
Owner Author

/gemini review

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request does a great job of refactoring the common Data1D processing logic into a shared processData1D helper function, significantly reducing code duplication. This is a valuable improvement for maintainability.

I've reviewed the changes and have a few suggestions to further improve the new helper function and its usage, focusing on performance, clarity, and modern TypeScript practices. My comments include a small optimization, removing a redundant check, and fixing a typo in a comment. I've also suggested using object destructuring to make the code in the sync modifiers more idiomatic when updating state from the helper function's result.

- Call data1DNamesWrapper.size() once instead of twice
- Remove redundant data1D check in processData1D
- Use object destructuring in all three sync modifiers for cleaner code
@andeplane
Copy link
Owner Author

/gemini review

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request does a great job of refactoring duplicated code into a shared processData1D helper function, which significantly improves maintainability. The logic extraction is well-executed. My review includes a few suggestions to further improve the new helper function's clarity and performance, and to make the calling code in the modifiers more concise.

cursoragent and others added 4 commits November 1, 2025 11:53
…ssign

- Optimize loop in processData1D: separate j===0 logic and avoid fetching xValues when j>0
- Remove redundant Float32Array type cast (HEAPF32.subarray already returns Float32Array)
- Use Object.assign instead of destructuring in sync modifiers for better conciseness
- Separate j===0 and j>0 logic for better clarity
- Avoid fetching xValues when j>0 (performance improvement)
- Store numPoints once to avoid repeated calls
- Remove redundant Float32Array type cast
@andeplane
Copy link
Owner Author

/gemini review

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

The pull request successfully extracts common Data1D processing logic into a shared helper function, significantly reducing code duplication and improving maintainability across SyncComputesModifier, SyncFixesModifier, and SyncVariablesModifier. The changes are well-structured and address the stated objective of reducing redundancy and ensuring consistent WASM memory cleanup. The introduction of the processData1D function is a good refactoring step.

Comment on lines +77 to +86
if (j === 0) {
const xValuesPointer = lmpData.getXValuesPointer() / 4;
const xValues = input.wasm.HEAPF32.subarray(
xValuesPointer,
xValuesPointer + numPoints,
);
for (let k = lengthBeforeWeStart; k < numPoints; k++) {
data1D.data.push([xValues[k]]);
data1D.data[k].push(yValues[k]);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

The xValues array is only used when j === 0. However, it is declared inside the loop, which means memory is allocated for it in each iteration, even when it's not used. This could lead to unnecessary memory allocations and potential performance overhead, especially if data1DNamesSize is large. Consider moving the declaration and assignment of xValues outside the loop, or only assign it when j === 0.

        const xValuesPointer = lmpData.getXValuesPointer() / 4;
        const xValues = input.wasm.HEAPF32.subarray(
          xValuesPointer,
          xValuesPointer + numPoints,
        );
        for (let k = lengthBeforeWeStart; k < numPoints; k++) {
          data1D.data.push([xValues[k]]);
          data1D.data[k].push(yValues[k]);
        }

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.

refactor: Extract common Data1D processing logic from sync modifiers

3 participants