Skip to content

.byref always optimizes sink call into a bitwise memcopy if =sink is disabled#24327

Closed
ringabout wants to merge 2 commits into
develfrom
pr_dujebfr
Closed

.byref always optimizes sink call into a bitwise memcopy if =sink is disabled#24327
ringabout wants to merge 2 commits into
develfrom
pr_dujebfr

Conversation

@ringabout

@ringabout ringabout commented Oct 18, 2024

Copy link
Copy Markdown
Member

closes #75
ref nim-lang/threading#79

Or only when =sink is disabled for byRef

Comment thread compiler/injectdestructors.nim Outdated
(isAnalysableFieldAccess(dest, c.owner) and isFirstWrite(dest, c)))) or
isNoInit(dest) or IsReturn in flags:
isNoInit(dest) or IsReturn in flags or
tfByRef in dest.typ.flags:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think the logic should be "it's .byref and also has its =sink disabled".

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

make sense

@ringabout ringabout changed the title .byref always optimizes sink call into a bitwise memcopy .byref always optimizes sink call into a bitwise memcopy if =sink is disabled Oct 19, 2024
@Araq

Araq commented Oct 19, 2024

Copy link
Copy Markdown
Member

This is still not correct as it can introduce leaks for code like var x = create(); x = create(). The idea here is that .byref types do not have any bitwise copies. We need to rethink this. It probably means the whole idea of =sink() .error is just wrong. =sink is fine and doesn't copy and shallow copies are prevented by .byref.

@darkestpigeon

darkestpigeon commented Oct 21, 2024

Copy link
Copy Markdown
Contributor

I usually use =copy() .error and =sink() .error for types that are used to communicate between multiple threads (perhaps some foreign) and the object needs to be pinned to a particular address. This both signals intent and prevents dumb errors (although moving an object by accident is rare). Will .byref cover this case correctly?

@Araq

Araq commented Oct 22, 2024

Copy link
Copy Markdown
Member

I think so, yes, but making =sink an .error just because you want to tie the object to a stack location is wrong regardless.

@ringabout ringabout closed this Sep 12, 2025
@ringabout
ringabout deleted the pr_dujebfr branch September 12, 2025 14:04
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.

Problem with the create* API of synchronization primitives

3 participants