-
Notifications
You must be signed in to change notification settings - Fork 1.1k
List(...) optimization to avoid intermediate array #17166
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
Merged
nicolasstucki
merged 7 commits into
scala:main
from
KuceraMartin:list-avoid-intermediate-array
Nov 28, 2023
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
aaf9ec7
List(...) optimization to avoid intermediate array (closes https://gi…
KuceraMartin ca7bd7d
Introduce Boxing for Singletons
Decel 90aea07
Initial pre-change cleanups
dwijnand 2ac7c1c
Fixup and finish List optimisation
dwijnand ea1731a
Implement list optimisation limit
dwijnand 3b9f0c9
Automate list optimisation threshhold cases
dwijnand bdb89d8
Document 8 & use Option extractor over | Null
dwijnand File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
object Test: | ||
|
||
var counter = 0 | ||
|
||
def next = | ||
counter += 1 | ||
counter.toString | ||
|
||
def main(args: Array[String]): Unit = | ||
//List.apply is subject to an optimisation in cleanup | ||
//ensure that the arguments are evaluated in the currect order | ||
// Rewritten to: | ||
// val myList: List = new collection.immutable.::(Test.this.next(), new collection.immutable.::(Test.this.next(), new collection.immutable.::(Test.this.next(), scala.collection.immutable.Nil))); | ||
val myList = List(next, next, next) | ||
assert(myList == List("1", "2", "3"), myList) | ||
|
||
val mySeq = Seq(next, next, next) | ||
assert(mySeq == Seq("4", "5", "6"), mySeq) | ||
|
||
val emptyList = List[Int]() | ||
assert(emptyList == Nil) | ||
|
||
// just assert it doesn't throw CCE to List | ||
val queue = scala.collection.mutable.Queue[String]() | ||
|
||
// test for the cast instruction described in checkApplyAvoidsIntermediateArray | ||
def lub(b: Boolean): List[(String, String)] = | ||
if b then List(("foo", "bar")) else Nil | ||
|
||
// from minimising CI failure in oslib | ||
// again, the lub of :: and Nil is Product, which breaks ++ (which requires IterableOnce) | ||
def lub2(b: Boolean): Unit = | ||
Seq(1) ++ (if (b) Seq(2) else Nil) | ||
|
||
// Examples of arity and nesting arity | ||
// to find the thresholds and reproduce the behaviour of nsc | ||
def examples(): Unit = | ||
val max1 = List[Object]("1", "2", "3", "4", "5", "6", "7") // 7 cons w/ 7 string heads + nil | ||
val max2 = List[Object]("1", "2", "3", "4", "5", "6", List[Object]()) // 7 cons w/ 6 string heads + 1 nil head + nil | ||
val max3 = List[Object]("1", "2", "3", "4", "5", List[Object]("6")) | ||
val max4 = List[Object]("1", "2", "3", "4", List[Object]("5", "6")) | ||
|
||
val over1 = List[Object]("1", "2", "3", "4", "5", "6", "7", "8") // wrap 8-sized array | ||
val over2 = List[Object]("1", "2", "3", "4", "5", "6", "7", List[Object]()) // wrap 8-sized array | ||
val over3 = List[Object]("1", "2", "3", "4", "5", "6", List[Object]("7")) // wrap 1-sized array with 7 | ||
val over4 = List[Object]("1", "2", "3", "4", "5", List[Object]("6", "7")) // wrap 2 | ||
|
||
val max5 = | ||
List[Object]( | ||
List[Object]( | ||
List[Object]( | ||
List[Object]( | ||
List[Object]( | ||
List[Object]( | ||
List[Object]( | ||
List[Object]( | ||
)))))))) // 7 cons + 1 nil | ||
|
||
val over5 = | ||
List[Object]( | ||
List[Object]( | ||
List[Object]( | ||
List[Object]( | ||
List[Object]( | ||
List[Object]( | ||
List[Object]( | ||
List[Object]( List[Object]() | ||
)))))))) // 7 cons + 1-sized array wrapping nil | ||
|
||
val max6 = | ||
List[Object]( // ::( | ||
"1", "2", List[Object]( // 1, ::(2, ::(::( | ||
"3", "4", List[Object]( // 3, ::(4, ::(::( | ||
List[Object]() // Nil, Nil | ||
) // ), Nil)) | ||
) // ), Nil)) | ||
) // ) | ||
// 7 cons + 4 string heads + 4 nils for nested lists | ||
|
||
val max7 = | ||
List[Object]( // ::( | ||
"1", "2", List[Object]( // 1, ::(2, ::(::( | ||
"3", "4", List[Object]( // 3, ::(4, ::(::( | ||
"5" // 5, Nil | ||
) // ), Nil)) | ||
) // ), Nil)) | ||
) // ) | ||
// 7 cons + 5 string heads + 3 nils for nested lists |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.