Skip to content
This repository has been archived by the owner on Oct 22, 2024. It is now read-only.

Avoid setRange with potentially incompatible types #320

Merged
merged 5 commits into from
Nov 21, 2023
Merged

Conversation

natebosch
Copy link
Contributor

@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

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
Contributor

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
Contributor 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
Contributor

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
Contributor

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
Contributor 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
Contributor

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
Contributor

kevmoo commented Nov 8, 2023

I churned below you, @natebosch !

@natebosch
Copy link
Contributor Author

@rakudrama any concerns about this sublist approach?

@rakudrama
Copy link

@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
mosuem pushed a commit to dart-lang/core that referenced this pull request Oct 18, 2024
…ection#320)

Fixes dart-lang/collection#317

Use `sublist` on the input to get a list with the same runtime generic
as the input instead of constructing a new list with the static generic.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
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