Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

@nullOut annotation#10959

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Draft
lrytz wants to merge2 commits intoscala:2.13.x
base:2.13.x
Choose a base branch
Loading
fromlrytz:lazy-list-null-out
Draft

Conversation

lrytz
Copy link
Member

Includes changes from#10937.

Continuation of#7990.


this: @nullOut orlocal: @nullOut sets the local variable slot to
null after loading the value on the stack.

Mixin forwarders, static mixin accessors and bridges set the receiver
local tonull if the target method is annotated@nullOut.


As a test, use@nullOut for the forwarder / aliasIterableOnceOps./:.
It uses@nullOut to clear
thethis reference before callingfoldLeft. The annotation is
also on the method itself to ensure mixin forwarders (generated in
the subclass) and the static mixin accessor (generated in the trait)
null out the receiver before invoking/:.

After removingLazyList.foldLeft, it inherits the implementation
from LinearSeqOps. That method again uses@nullOut twice, once
to clearthis when it's no longer needed, and once on the method
for the generated accessors.

I think we shouldnot use@nullOut everywhere in the standard
library, only on methods that cannot be overridden in LazyList.
So, for final methods like/: ormkString.

NthPortal reacted with thumbs up emoji
@scala-jenkinsscala-jenkins added this to the2.13.17 milestoneDec 13, 2024
@SethTisue
Copy link
Member

😎🆒

@som-snytt
Copy link
Contributor

I don't understand Seth's emojis so ❤️ and 🚀.

@lrytz
Copy link
MemberAuthor

lrytz commentedDec 18, 2024
edited
Loading

I took another look atmkString, as the motivating example isRandom.alphanumeric.take(bigNumber).mkString.

@nullOut works to prevent livethis references on the stack through all the forwarders. But unfortunately,LazyList overridesaddString (which is the actual implementation ofmkString) to ensure cycles are detected.

scala>valll:LazyList[Int]=1#::2#::3#:: llvalll:LazyList[Int]=LazyList(<not computed>)scala> ll.mkStringvalres0:String=123<cycle>

Even though that could be considered an inconsistency, it seems desired (scala/collection-strawman#550,scala/bug#8680).

Cycle detection uses the 2x/1x iterator idea (Floyd's Algorithm). So for a very long non-cyclic LazyList, the slow pointer points to the middle of the list when the fast one reaches the end. So at least half of the list cannot be GC'd.

I don't see how we could fix it.

@lrytzlrytzforce-pushed thelazy-list-null-out branch 2 times, most recently from260cea2 to8f785ecCompareDecember 18, 2024 16:29
@SethTisue
Copy link
Member

@-ing@NthPortal and @scala/collections

Copy link
Contributor

@NthPortalNthPortal left a comment

Choose a reason for hiding this comment

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

neat! I don't know enough the compiler to be confident in saying anything about the implementation, but it look decent to me

@lrytz
Copy link
MemberAuthor

Hello, optimizer!

https://github.com/scala/scala/blob/v2.13.15/src/compiler/scala/tools/nsc/backend/jvm/opt/CopyProp.scala#L143

...// current field value is `this`, which won't be gc'd anyway

@lrytzlrytzforce-pushed thelazy-list-null-out branch 2 times, most recently from8fd5c74 to4fa73feCompareJanuary 30, 2025 21:00
lrytzand others added2 commitsJanuary 31, 2025 08:47
`this: @nullOut` or `local: @nullOut` sets the local variable slot to`null` after loading the value on the stack.Mixin forwarders, static mixin accessors and bridges set the receiverlocal to `null` if the target method is annotated `@nullOut`, forexample.Co-authored-by: Stefan Zeiger <szeiger@novocode.com>
Needs a restarr to work!The forwarder / alias `IterableOnceOps./:` uses `@nullOut` to clearthe `this` reference before calling `foldLeft`. The annotation isalso on the method itself to ensure mixin forwarders (generated inthe subclass) and the static mixin accessor (generated in the trait)null out the receiver before invoking `/:`.After removing `LazyList.foldLeft`, it inherits the implementationfrom LinearSeqOps. That method again uses `@nullOut` twice, onceto clear `this` when it's no longer needed, and once on the methodfor the generated accessors.I think we should *not* use `@nullOut` everywhere in the standardlibrary, only on methods that cannot be overridden in LazyList, like`/:`.`@nullOut` works correctly on `mkString` too, but unfortunately the`addString` override in LazyList prevents GC of processed elementsdue to cycle detection.
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@NthPortalNthPortalNthPortal left review comments

Assignees
No one assigned
Labels
None yet
Projects
None yet
Milestone
2.13.17
Development

Successfully merging this pull request may close these issues.

5 participants
@lrytz@SethTisue@som-snytt@NthPortal@scala-jenkins

[8]ページ先頭

©2009-2025 Movatter.jp