Skip to content

Optimize WithArray arr (\v -> body)#140

Open
emwap wants to merge 2 commits intomasterfrom
dubious_mutable_optimization
Open

Optimize WithArray arr (\v -> body)#140
emwap wants to merge 2 commits intomasterfrom
dubious_mutable_optimization

Conversation

@emwap
Copy link
Copy Markdown
Member

@emwap emwap commented Jan 29, 2015

Substitute arr for v when arr is free in the body.

This is very nice for copy propagation, but not possible in the typed
representation.

Is this rewrite morally safe? I.e. is it enough to check that arr is free in the body or do we risk leaking an alias to arr?

/cc @pjonsson, @emilaxelsson, @josefs

Substitute `arr` for `v` when `arr` is free in the `body`.
This is very nice for copy propagation, but not possible in the typed
representation.
@josefs
Copy link
Copy Markdown
Contributor

josefs commented Jan 29, 2015

Clarification from mine and Anders' discussion: the variable arr does not occur in body.

This is morally safe in single threaded code if body does not return the array (which may be hard to check).

@pjonsson
Copy link
Copy Markdown
Member

The point of @josefs clarification was what I was after in my previous comment in the other pull request as well. That should be fixed in the comment in this commit as well.

Mutable is really just a different word for "very hard", especially in the context of concurrency. It sounds reasonable that this is safe in a single threaded context. I have no idea whether we are guaranteed to be in a single threaded context at all times though.

Substitute `arr` for `v` when `arr` is not used in the `body`.
This is very nice for copy propagation, but not possible in the typed
representation.
@emilaxelsson
Copy link
Copy Markdown
Member

Just a note: Peter has been talking about type checking the simplified code as a way to catch errors. Maybe there should be an unsafeFreezeArray to make rewrites like this pass the type checker (and then it could be done in the typed representation as well).

@josefs
Copy link
Copy Markdown
Contributor

josefs commented Jan 31, 2015

Another possibility is to do this kind of rewrite at a lower level representation, i.e. the imperative representation. At that point there is no distinction between mutable and immutable arrays.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants