Thanks to visit codestin.com
Credit goes to Github.com

Skip to content

Conversation

@SteveDiamond
Copy link
Collaborator

Description

Addresses follow up comments on #3031

Type of change

  • New feature (backwards compatible)
  • New feature (breaking API changes)
  • Bug fix
  • Other (Documentation, CI, ...)

Contribution checklist

  • Add our license to new files.
  • Check that your code adheres to our coding style.
  • Write unittests.
  • Run the unittests and check that they’re passing.
  • Run the benchmarks to make sure your change doesn’t introduce a regression.

@SteveDiamond SteveDiamond self-assigned this Dec 23, 2025
@SteveDiamond SteveDiamond requested a review from PTNobel December 23, 2025 19:05
@claude
Copy link

claude bot commented Dec 23, 2025

Code Review

Thanks for addressing the follow-up comments from PR #3031! I've reviewed the changes and overall they look good. Here are my findings:

Strengths

  1. Good Feature Addition: The new order parameter for build_matrix() adds useful flexibility for choosing between CSC and CSR sparse formats with proper validation.

  2. Comprehensive Test Coverage: The new test_build_matrix_order() test is well-structured and covers:

    • Both CSC and CSR output formats
    • Consistency between formats (same values)
    • Invalid input validation
    • Both SCIPY and COO backends via parametrization
  3. Improved Variable Naming: The rename from lhs/rhs in div() and rmul() improves clarity:

    • In div(): lhsrhs (correct: operand is divisor)
    • In rmul(): lhsrhs (correct: constant multiplier is on right in code flow)
  4. Better Documentation: Enhanced docstrings with parameter descriptions and return type annotations.

  5. Unnecessary Copy Removed: coo_backend.py:815 - removed redundant .copy() (already copying unconditionally on line 825).

  6. Dead Code Cleanup: Removed unused pre-allocation arrays in coo_matmul() (lines 391-394).


🔍 Minor Issues & Suggestions

1. Inconsistent Type Hints (cvxpy/lin_ops/backends/base.py:199)

The change from:

self.variable_ids = variable_ids if variable_ids is not None else None

to:

self.variable_ids = variable_ids

This is cleaner, but could be even clearer. The conditional was redundant (no-op), so the removal is correct. However, consider if variable_ids should have a default empty set instead of None when not provided, to avoid potential None-checking throughout the codebase. If None is intentional, this is fine.

2. Return Type Annotation Consistency

In base.py, the return type uses union syntax:

sp.csc_array | sp.csr_array

This is Python 3.10+ syntax. Verify the minimum Python version requirement. If supporting older versions, use:

Union[sp.csc_array, sp.csr_array]

3. Test Coverage: Missing Rust Backend

The test_build_matrix_order() test only parametrizes over SCIPY_CANON_BACKEND and COO_CANON_BACKEND, but the Rust backend also implements the order parameter. Consider adding it to the parametrization:

@pytest.mark.parametrize("backend_name", [
    s.SCIPY_CANON_BACKEND, 
    s.COO_CANON_BACKEND,
    pytest.param(s.RUST_CANON_BACKEND, marks=pytest.mark.skipif(...))
])

🧪 Testing Recommendations

  1. Edge Case: Empty Matrix: Test build_matrix() with empty constraint lists or zero-size matrices to ensure both CSC and CSR handle it correctly.

  2. Integration Test: Consider adding a higher-level integration test that verifies CSR format works end-to-end in actual optimization problems (if not already covered elsewhere).

  3. Performance Note: Document when CSR might be preferred over CSC (e.g., row-based operations), though this may be outside the scope of this PR.


🔒 Security & Performance

  • ✅ No security concerns identified
  • ✅ No performance regressions expected (the changes are additive with sensible defaults)
  • ✅ Memory efficiency improved by removing unnecessary allocations

📝 Code Quality

  • ✅ Follows existing code style
  • ✅ Proper error handling with clear error messages
  • ✅ Good documentation

Verdict

Approve with minor suggestions. The changes are well-implemented and improve code quality. The suggestions above are minor improvements for consideration but don't block merging.

Great work on the cleanup! 🎉

Copy link
Collaborator

@Transurgeon Transurgeon left a comment

Choose a reason for hiding this comment

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

changes look good to me!
nice catch on the rhs vs. lhs for the constant data, I agree it is making a bit more sense now.

@PTNobel
Copy link
Collaborator

PTNobel commented Dec 31, 2025

@SteveDiamond @Transurgeon can you review my commit? I had intended order to determine the flattening used for the first 2 tensor dimensions.

@github-actions
Copy link

Benchmarks that have stayed the same:

   before           after         ratio
 [21bf98cf]       [9dc0dd38]
      293±0ms          315±0ms     1.08  slow_pruning_1668_benchmark.SlowPruningBenchmark.time_compile_problem
     38.9±0ms         40.8±0ms     1.05  matrix_stuffing.SmallMatrixStuffing.time_compile_problem
      1.10±0s          1.14±0s     1.04  gini_portfolio.Cajas.time_compile_problem
      11.4±0s          11.8±0s     1.04  simple_LP_benchmarks.SimpleLPBenchmark.time_compile_problem
      256±0ms          263±0ms     1.03  simple_QP_benchmarks.SimpleQPBenchmark.time_compile_problem
     14.2±0ms         14.4±0ms     1.01  simple_QP_benchmarks.ParametrizedQPBenchmark.time_compile_problem
      1.87±0s          1.88±0s     1.01  simple_QP_benchmarks.UnconstrainedQP.time_compile_problem
      934±0ms          942±0ms     1.01  simple_LP_benchmarks.SimpleScalarParametrizedLPBenchmark.time_compile_problem
      12.9±0s          13.0±0s     1.01  finance.CVaRBenchmark.time_compile_problem
      1.07±0s          1.07±0s     1.00  finance.FactorCovarianceModel.time_compile_problem
      4.97±0s          4.97±0s     1.00  optimal_advertising.OptimalAdvertising.time_compile_problem
      1.37±0s          1.37±0s     1.00  matrix_stuffing.ParamConeMatrixStuffing.time_compile_problem
      167±0ms          167±0ms     1.00  high_dim_convex_plasticity.ConvexPlasticity.time_compile_problem
      22.4±0s          22.3±0s     1.00  sdp_segfault_1132_benchmark.SDPSegfault1132Benchmark.time_compile_problem
      4.56±0s          4.55±0s     1.00  huber_regression.HuberRegression.time_compile_problem
      280±0ms          279±0ms     1.00  matrix_stuffing.ParamSmallMatrixStuffing.time_compile_problem
      345±0ms          343±0ms     0.99  gini_portfolio.Yitzhaki.time_compile_problem
      236±0ms          234±0ms     0.99  gini_portfolio.Murray.time_compile_problem
      676±0ms          668±0ms     0.99  matrix_stuffing.ConeMatrixStuffingBench.time_compile_problem
      5.17±0s          5.11±0s     0.99  svm_l1_regularization.SVMWithL1Regularization.time_compile_problem
     14.4±0ms         14.3±0ms     0.99  simple_LP_benchmarks.SimpleFullyParametrizedLPBenchmark.time_compile_problem
      2.85±0s          2.81±0s     0.98  quantum_hilbert_matrix.QuantumHilbertMatrix.time_compile_problem
      524±0ms          514±0ms     0.98  semidefinite_programming.SemidefiniteProgramming.time_compile_problem
      859±0ms          834±0ms     0.97  simple_QP_benchmarks.LeastSquares.time_compile_problem
      1.75±0s          1.64±0s     0.94  tv_inpainting.TvInpainting.time_compile_problem

@SteveDiamond SteveDiamond merged commit 74867aa into master Dec 31, 2025
49 checks passed
@SteveDiamond SteveDiamond deleted the steven/large-DPP-comments branch December 31, 2025 18:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants