Skip to content

Foundation Classes - Precompute Jacobi coefficients#778

Merged
dpasukhi merged 9 commits intoOpen-Cascade-SAS:IRfrom
dpasukhi:constexpr_opt
Nov 2, 2025
Merged

Foundation Classes - Precompute Jacobi coefficients#778
dpasukhi merged 9 commits intoOpen-Cascade-SAS:IRfrom
dpasukhi:constexpr_opt

Conversation

@dpasukhi
Copy link
Copy Markdown
Member

  • Add PLib_JacobiPolynomial_Coeffs.pxx with large constexpr tables and a JacobiCoefficientsCache + GetJacobiCoefficients() fast lookup to serve precomputed TNorm/CofA/CofB/Denom arrays for all constraint levels and degrees.
  • Replace several runtime-initialized per-instance arrays/handles with zero-overhead constexpr data; remove myTNorm/myCofA/myCofB/myDenom handles from the class.
  • Modernize PLib_JacobiPolynomial implementation:
    • Use constructor initializer list and validate inputs there.
    • Use constexpr lookup tables for weights/transforms and memcpy/std::fill_n to speed copying into TColStd arrays.
    • Replace switch-based pointer selection with indexed arrays for cleaner selection of DB pointers.
    • Simplify numerical loops, make offsets/indices const where possible and use clearer variable names.
    • Use GetJacobiCoefficients() in D0123 (and callers) to remove on-demand initialization and reduce per-call overhead.
  • Change static data types to constexpr double for stronger optimization and clearer intent.
  • Various micro-optimizations and safety fixes (avoid NULL, tighten const correctness) to improve performance and reduce runtime allocations.

@dpasukhi dpasukhi requested a review from Copilot October 30, 2025 08:10
@dpasukhi dpasukhi self-assigned this Oct 30, 2025
@dpasukhi dpasukhi added 2. Enhancement New feature or request 1. Foundation Classes Containers, system calls wrappers, smart pointers and other low level of OCCT code 1. Coding Coding rules, trivial changes and misprints labels Oct 30, 2025
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR optimizes the PLib_JacobiPolynomial class by precomputing Jacobi polynomial coefficients and replacing runtime initialization with compile-time constexpr data. The main goals are to eliminate per-instance memory allocations and improve performance through zero-overhead abstractions.

Key changes:

  • Replace runtime coefficient computation with precomputed constexpr lookup tables
  • Remove dynamic Handle-based member variables (myTNorm, myCofA, myCofB, myDenom) from the class
  • Modernize implementation with C++17 features and more efficient memory operations

Reviewed Changes

Copilot reviewed 3 out of 4 changed files in this pull request and generated 3 comments.

File Description
PLib_JacobiPolynomial_Data.pxx Convert static data arrays from Standard_Real to constexpr double for compile-time optimization
PLib_JacobiPolynomial.hxx Remove Handle-based member variables and make remaining members const
PLib_JacobiPolynomial.cxx Replace runtime initialization with constexpr lookup tables, modernize with C++17 features, and optimize memory operations

Comment thread src/FoundationClasses/TKMath/PLib/PLib_JacobiPolynomial.cxx Outdated
Comment thread src/FoundationClasses/TKMath/PLib/PLib_JacobiPolynomial.cxx Outdated
Comment thread src/FoundationClasses/TKMath/PLib/PLib_JacobiPolynomial.cxx Outdated
@dpasukhi dpasukhi marked this pull request as ready for review October 30, 2025 11:24
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

Copilot reviewed 6 out of 7 changed files in this pull request and generated 2 comments.

Comment thread src/FoundationClasses/TKMath/PLib/PLib_JacobiPolynomial.cxx Outdated
Comment thread src/FoundationClasses/TKMath/PLib/PLib_JacobiPolynomial.cxx Outdated
- Add PLib_JacobiPolynomial_Coeffs.pxx with large constexpr tables and a
  JacobiCoefficientsCache + GetJacobiCoefficients() fast lookup to serve
  precomputed TNorm/CofA/CofB/Denom arrays for all constraint levels and
  degrees.
- Replace several runtime-initialized per-instance arrays/handles with
  zero-overhead constexpr data; remove myTNorm/myCofA/myCofB/myDenom
  handles from the class.
- Modernize PLib_JacobiPolynomial implementation:
  - Use constructor initializer list and validate inputs there.
  - Use constexpr lookup tables for weights/transforms and memcpy/std::fill_n
    to speed copying into TColStd arrays.
  - Replace switch-based pointer selection with indexed arrays for cleaner
    selection of DB pointers.
  - Simplify numerical loops, make offsets/indices const where possible and
    use clearer variable names.
  - Use GetJacobiCoefficients() in D0123 (and callers) to remove on-demand
    initialization and reduce per-call overhead.
- Change static data types to constexpr double for stronger optimization and
  clearer intent.
- Various micro-optimizations and safety fixes (avoid NULL, tighten const
  correctness) to improve performance and reduce runtime allocations.
…r from the .hxx,

and correct several indexing/assignment issues:
- Replace unsafe pointer-based fill_n usage on ChangeValue with explicit per-column loops
  for TabWeights row 0 to ensure proper 2D array access.
- Fix coefficient offsets in ToCoefficients to account for array lower bounds (use ibegC and
  ibegJC), avoiding incorrect indexing into JacCoeff/Coefficients.
- Remove duplicate/unused include.
…last computed

error when the loop breaks early. This ensures MaxError is up-to-date when
NewDegree is set and returned.
Move the assignment of MaxError to occur after the tolerance/convergence check and potential break so MaxError always reflects the last computed Error value.
…strengthen tests

- PLib_JacobiPolynomial.cxx:
  - Remove unused <algorithm> and <cstring> includes.
  - Replace std::memcpy uses with explicit element-wise loops when filling TabWeights and TabMax to ensure safe 2D/1D array access.
  - Adjust ordering when populating row 0 in Weights to avoid overwriting database-supplied values.
  - Keep/ensure WorkDegree validation to fail early for invalid constraints.

- PLib_JacobiPolynomial_Test.cxx:
  - Expand ConstructorEdgeCases to validate minimum WorkDegree for each constraint level (C0, C1, C2) and keep a reasonable max-degree check.

These changes remove unsafe memory operations, fix potential out-of-bounds/aliasing issues, and make tests more explicit about valid degree/constraint combinations.
- Implement WorkDegree() and NivConstr() inline in PLib_JacobiPolynomial.hxx.
- Remove the #include of PLib_JacobiPolynomial.lxx from the header and delete the .lxx file.
- Remove PLib_JacobiPolynomial.lxx and PLib_JacobiPolynomial_Data.pxx entries from FILES.cmake.
Replace the manual ternary mapping (and inline throw for invalid orders) with
the centralized PLib::NivConstr helper, simplifying the constructor and
reusing existing validation logic.
- Replace magic integers/values with constexpr constants (NB gauss points, invalid value,
  max degree) and introduce constexpr lookup tables for DB pointers.
- Use std::array stack buffer in MaxError to avoid dynamic allocation and compute TabMax
  once into a local buffer.
- Replace ad-hoc pointer arithmetic and duplicated static tables with descriptive aDbPointer /
  aTrPointer variables and explicit 2D array copies for safe access.
- Rename function parameters and many local variables to consistent "the..." / "a..." / "an..."
  style for clarity; update header prototypes accordingly.
- Tighten validation branches, add braces, and normalize return/assignment logic in Points,
  Weights, MaxValue, MaxError, ReduceDegree, AverageError, ToCoefficients and D0123.
- Misc cleanup: move/clarify private section in header and remove hardcoded numeric literals.
@dpasukhi dpasukhi requested a review from Copilot November 2, 2025 00:12
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

Copilot reviewed 6 out of 7 changed files in this pull request and generated 4 comments.

Comment thread src/FoundationClasses/TKMath/PLib/PLib_JacobiPolynomial.cxx
Comment thread src/FoundationClasses/TKMath/PLib/PLib_JacobiPolynomial.cxx
Comment thread src/FoundationClasses/TKMath/PLib/PLib_JacobiPolynomial.hxx
Comment thread src/FoundationClasses/TKMath/PLib/FILES.cmake
@dpasukhi dpasukhi merged commit 7e9b74c into Open-Cascade-SAS:IR Nov 2, 2025
23 checks passed
@github-project-automation github-project-automation bot moved this from Todo to Done in Maintenance Nov 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

1. Coding Coding rules, trivial changes and misprints 1. Foundation Classes Containers, system calls wrappers, smart pointers and other low level of OCCT code 2. Enhancement New feature or request

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

2 participants