Skip to content

Comments

Use array comprehension for large byte_extract lowering#8840

Open
tautschnig wants to merge 1 commit intodiffblue:developfrom
tautschnig:arrays/comprehension
Open

Use array comprehension for large byte_extract lowering#8840
tautschnig wants to merge 1 commit intodiffblue:developfrom
tautschnig:arrays/comprehension

Conversation

@tautschnig
Copy link
Collaborator

lower_byte_extract_array_vector expands byte_extract of an array type into an element-by-element array_exprt. For large arrays (e.g., char[100000] in a union), this creates N sub-expressions that are each recursively lowered and simplified, resulting in O(N^2) behavior.

When the array size exceeds MAX_FLATTENED_ARRAY_SIZE (1000), use array_comprehension_exprt instead. This path already exists for arrays with non-constant size; now it is also used for large constant-size arrays. This reduces the lowering from O(N) expressions to O(1).

Performance on union_large_array (char[100000] in a union):
Before: >120s with --arrays-uf-always
After: 2.3s with --arrays-uf-always

Co-authored-by: Kiro

  • Each commit message has a non-empty body, explaining why the change was made.
  • n/a Methods or procedures I have added are documented, following the guidelines provided in CODING_STANDARD.md.
  • n/a The feature or user visible behaviour I have added or modified has been documented in the User Guide in doc/cprover-manual/
  • Regression or unit tests are included, or existing tests cover the modified code (in this case I have detailed which ones those are in the commit message).
  • My commit message includes data points confirming performance improvements (if claimed).
  • My PR is restricted to a single feature or bugfix.
  • n/a White-space or formatting changes outside the feature-related changed lines are in commits of their own.

Copy link

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 lower_byte_extract_array_vector to avoid element-by-element expansion for large constant-sized arrays by switching to an array_comprehension_exprt representation once the array exceeds MAX_FLATTENED_ARRAY_SIZE, reducing expression blow-up and improving lowering performance.

Changes:

  • Include magic.h to access MAX_FLATTENED_ARRAY_SIZE.
  • Route large constant-sized array byte-extract lowering through array_comprehension_exprt instead of generating N lowered sub-expressions.
  • Add comments documenting the O(N²) behavior and rationale for the comprehension-based lowering.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

lower_byte_extract_array_vector expands byte_extract of an array
type into an element-by-element array_exprt. For large arrays
(e.g., char[100000] in a union), this creates N sub-expressions
that are each recursively lowered and simplified, resulting in
O(N^2) behavior.

When the array size exceeds MAX_FLATTENED_ARRAY_SIZE (1000), use
array_comprehension_exprt instead. This path already exists for
arrays with non-constant size; now it is also used for large
constant-size arrays. This reduces the lowering from O(N)
expressions to O(1).

Performance on union_large_array (char[100000] in a union):
  Before: >120s with --arrays-uf-always
  After:  2.3s with --arrays-uf-always

Remove thorough-arrays-uf-always tag from union_large_array.desc
since the test now completes in 2 seconds.

Co-authored-by: Kiro
@codecov
Copy link

codecov bot commented Feb 24, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 80.01%. Comparing base (15eb10a) to head (a31877d).

Additional details and impacted files
@@           Coverage Diff            @@
##           develop    #8840   +/-   ##
========================================
  Coverage    80.00%   80.01%           
========================================
  Files         1700     1700           
  Lines       188252   188254    +2     
  Branches        73       73           
========================================
+ Hits        150613   150629   +16     
+ Misses       37639    37625   -14     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

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