Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid setRange with potentially incompatible types #320

Merged
merged 5 commits into from
Nov 21, 2023
Merged

Conversation

natebosch
Copy link
Member

Fixes #317

@natebosch natebosch requested a review from lrhn November 2, 2023 18:20
test('mergeSort works when runtime generic is a subtype of the static type',
() {
// Regression test for https://github.com/dart-lang/collection/issues/317
final length = 32; // _mergeSortLimit
Copy link
Member

Choose a reason for hiding this comment

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

Should probably test above and below the limit - the limit selects a different path.

If the limit is changed, how does this git updated? Perhaps use a value significantly larger.

Copy link
Member

Choose a reason for hiding this comment

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

Just pick a fairly large number, like 1024.
Whatever the limit is for using insertion-sort, it'll be below that.

Copy link
Member Author

Choose a reason for hiding this comment

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

Below the limit should be covered by other tests. This test only needs to ensure that the type error would occur. I'll bump it up to a larger number for safety, I don't think it's worth making the constant public for testing.

test('mergeSort works when runtime generic is a subtype of the static type',
() {
// Regression test for https://github.com/dart-lang/collection/issues/317
final length = 32; // _mergeSortLimit
Copy link
Member

Choose a reason for hiding this comment

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

Just pick a fairly large number, like 1024.
Whatever the limit is for using insertion-sort, it'll be below that.

@@ -394,8 +394,9 @@ void _merge<E, K>(
}
// First list empties first. Reached by break above.
target[targetOffset++] = secondElement;
target.setRange(
targetOffset, targetOffset + (secondEnd - cursor2), secondList, cursor2);
for (var i = targetOffset; i < targetOffset + (secondEnd - cursor2); i++) {
Copy link
Member

Choose a reason for hiding this comment

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

Can't _movingInsertionSort above have the same issue?
It contains a setRange call (line 307).

I'd rather change the creation of scratchSpace above to:

var scratchSpace = elements.sublist(0, secondLength);

That is guaranteed to create a list of the same kind and type as the original (documented as such), and actually does so for typed-data lists, where it can be a saving.

Or use

var scratchSpace = elements.take(secondLength).toList(growable: false);

to at least get a non-growable list (since it's never grown anyway).

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, I missed that there are a couple code paths where this could hit a moving insertion sort. I'm not sure how to construct an input list to exercise that path.

I'll go with the sublist approach to fix them reliably.

@@ -394,8 +394,9 @@ void _merge<E, K>(
}
// First list empties first. Reached by break above.
target[targetOffset++] = secondElement;
target.setRange(
targetOffset, targetOffset + (secondEnd - cursor2), secondList, cursor2);
for (var i = targetOffset; i < targetOffset + (secondEnd - cursor2); i++) {
Copy link
Member

Choose a reason for hiding this comment

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

This does extra computation in both condition and assignment.
I predict it would be more efficient as:

  for (var i = 0, count = secondEnd - cursor2; i < count; i++) {
    target[targetOffset + i] = secondList[cursor2 + i];
  }

(If these are platform lists, the compiler can make it a mem-move with known offsets and count. Obviously only possible if it's used monomorphically, but how often does someone use mergeSort to begin with? Even if not, less computation inside the inner loop is always good.)

@kevmoo
Copy link
Member

kevmoo commented Nov 8, 2023

I churned below you, @natebosch !

@natebosch
Copy link
Member Author

@rakudrama any concerns about this sublist approach?

@rakudrama
Copy link
Member

@rakudrama any concerns about this sublist approach?

LGTM

@natebosch natebosch merged commit 2d57a82 into master Nov 21, 2023
6 checks passed
@natebosch natebosch deleted the merge-type branch November 21, 2023 00:44
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.

mergeSort fails when static type of a list does not match a runtime type
4 participants