-
Notifications
You must be signed in to change notification settings - Fork 87
Avoid setRange with potentially incompatible types #320
Changes from 1 commit
223033d
d75e68b
0502a0f
e3149c6
ed0f6d0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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++) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This does extra computation in both condition and assignment. 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 |
||
target[i] = secondList[(i - targetOffset) + cursor2]; | ||
} | ||
} | ||
|
||
/// Sort [elements] using a quick-sort algorithm. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -366,6 +366,15 @@ void main() { | |
reverse(l, 0, 6); | ||
expect(l, equals([2, 1, 4, 3, 6, 5])); | ||
}); | ||
|
||
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just pick a fairly large number, like 1024. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
// In order list, first half empties first during merge. | ||
final list = List<int>.generate(length, (i) => i); | ||
natebosch marked this conversation as resolved.
Show resolved
Hide resolved
|
||
expect(() => mergeSort<num>(list), returnsNormally); | ||
}); | ||
} | ||
|
||
class C { | ||
|
There was a problem hiding this comment.
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: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
to at least get a non-growable list (since it's never grown anyway).
There was a problem hiding this comment.
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.