Skip to content

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.

@codecov
Copy link

codecov bot commented Feb 24, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 80.02%. Comparing base (4eb741f) to head (4b9f456).

Additional details and impacted files
@@           Coverage Diff            @@
##           develop    #8840   +/-   ##
========================================
  Coverage    80.01%   80.02%           
========================================
  Files         1700     1700           
  Lines       188342   188372   +30     
  Branches        73       73           
========================================
+ Hits        150710   150740   +30     
  Misses       37632    37632           

☔ 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.

@tautschnig tautschnig force-pushed the arrays/comprehension branch 2 times, most recently from 28d8e8f to 097ef61 Compare March 5, 2026 08:49
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 <kiro-agent@users.noreply.github.com>
@tautschnig tautschnig force-pushed the arrays/comprehension branch from 097ef61 to 4b9f456 Compare March 5, 2026 13:07
@tautschnig tautschnig assigned tautschnig and unassigned kroening Mar 6, 2026
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.

3 participants