Skip to content

(fix): similar! to preserve view semantics#40

Closed
mgyoo86 wants to merge 1 commit intomasterfrom
fix/similar
Closed

(fix): similar! to preserve view semantics#40
mgyoo86 wants to merge 1 commit intomasterfrom
fix/similar

Conversation

@mgyoo86
Copy link
Copy Markdown
Member

@mgyoo86 mgyoo86 commented Mar 31, 2026

Summary

similar!(pool, x) always returned a raw Array even when x was a view from acquire_view!. This broke the semantic contract that similar! should preserve the "kind" of its input array.

Bug

aa = acquire_view!(pool, Float64, 10)  # SubArray
bb = similar!(pool, aa)                # Expected: SubArray, Got: Array

All 4 _similar_impl! methods unconditionally called _acquire_impl!, ignoring whether the input was a view.

Fix

Added Array-specific dispatch to _similar_impl! (4 methods -> 8 methods):

  • _similar_impl!(pool, x::Array, ...) -> _acquire_impl! (returns Array)
  • _similar_impl!(pool, x::AbstractArray, ...) -> _acquire_view_impl! (returns view)

Julia's dispatch guarantees Array (concrete type) is always matched before AbstractArray, so existing behavior for plain arrays is unchanged.

similar!(pool, x) previously always returned a raw Array via _acquire_impl!,
even when x was a SubArray/ReshapedArray from acquire_view!. Add Array-specific
dispatch to _similar_impl! so plain Arrays route to _acquire_impl! while
view-like AbstractArrays route to _acquire_view_impl!.
@codecov
Copy link
Copy Markdown

codecov bot commented Mar 31, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 96.04%. Comparing base (3118fd0) to head (a452775).

Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff           @@
##           master      #40   +/-   ##
=======================================
  Coverage   96.03%   96.04%           
=======================================
  Files          14       14           
  Lines        3232     3240    +8     
=======================================
+ Hits         3104     3112    +8     
  Misses        128      128           
Files with missing lines Coverage Δ
src/convenience.jl 98.76% <100.00%> (+0.06%) ⬆️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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 fixes similar!(pool, x) so it preserves “view semantics” when the template x is a view returned by acquire_view!, instead of always returning a raw Array.

Changes:

  • Split _similar_impl! dispatch to treat x::Array differently from other AbstractArray inputs.
  • Route non-Array templates through _acquire_view_impl! so views stay views.
  • Add tests covering similar! behavior for 1D/2D views, type changes, and dimension changes.

Reviewed changes

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

File Description
src/convenience.jl Updates _similar_impl! dispatch to return pooled Array for Array templates and pooled views for non-Array templates.
test/test_convenience.jl Adds a dedicated testset ensuring similar! preserves view-ness when the input is a view.

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

@mgyoo86
Copy link
Copy Markdown
Member Author

mgyoo86 commented Mar 31, 2026

Closing: Base.similar() always returns Array regardless of input type — the original behavior was correct, not a bug. similar\!(pool, view)Array is consistent with Julia's contract. Users who need a view should use acquire_view\!(pool, eltype(x), size(x)) directly.

@mgyoo86 mgyoo86 closed this Mar 31, 2026
@mgyoo86 mgyoo86 deleted the fix/similar branch March 31, 2026 21:13
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