Skip to content
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

Document requirements for atsign-pure #25244

Closed
timholy opened this issue Dec 22, 2017 · 9 comments
Closed

Document requirements for atsign-pure #25244

timholy opened this issue Dec 22, 2017 · 9 comments
Assignees
Labels
needs docs Documentation for this change is required

Comments

@timholy
Copy link
Sponsor Member

timholy commented Dec 22, 2017

There have been quite a number of mis-uses of @pure, and in some cases the resulting discussion about why reduces to "ask Jameson." Let's make it "read the docs" instead. Here's a possible head start:

"""
    Base.@pure function f(args...)
        #body
    end

Marks function `f` as "pure," which means that the compiler should be allowed to replace `#body` with its resulting output. Marking a function as pure allows the operations to be performed at compile time rather than run time, potentially improving performance. In practice, it is only useful to mark functions taking only type or hard-coded constant inputs, since only in such cases does the compiler know the runtime values of the arguments.

To be a candidate for `@pure`, `f` must satisfy strict requirements:

  - its output depends only on its arguments (it cannot depend on any additional state)
  - it cannot have any side effects (no changes to any global variables, etc.)
  - it cannot engage in I/O, even on an error path

Marking an impure function with `@pure` can lead to serious errors, including both incorrect results and runtime crashes.
"""
@timholy timholy added the needs docs Documentation for this change is required label Dec 22, 2017
@fredrikekre
Copy link
Member

see #24817

@tpapp
Copy link
Contributor

tpapp commented Jun 1, 2018

I wonder if adding an 1.0 milestone would make sense; this should be fixed before Julia is released to reduce misunderstandings.

@StefanKarpinski
Copy link
Sponsor Member

It's a good idea to clarify certainly, but documenting a non-exported macro is hardly release-blocking.

@tpapp
Copy link
Contributor

tpapp commented Feb 6, 2019

In the meantime (since the 1.0 release), it has become very common on the discourse forum to suggest/apply optimizations by simply annotating code with @pure. I think that a large fraction of these applications don't fulfil the requirements (I won't link examples as I don't want to single out anyone).

The fact that it is not exported or documented in detail does not seem to be enough to discourage users from using @pure. In the meantime, the compiler is getting smarter and smarter, making its use less necessary than users seem to believe.

It would be great to at least extend the documentation of @pure with examples of violations of the requirements, eg subtle changes in global state, including throwing errors, method definitions, etc.

@chethega
Copy link
Contributor

chethega commented Feb 6, 2019

Example (on 1.0.3), dependency on method redefinition:

julia> @noinline f(x)=x+1;
julia> Base.@pure g(x) = f(x);
julia> h()=g(3);
julia> h()
4
julia> f(x)=x+2;
julia> h()
4

@tpapp
Copy link
Contributor

tpapp commented Jul 5, 2019

I wonder if this could be revisited once eg #32368 is merged.

@non-Jedi
Copy link
Contributor

Base.@pure at least has a docstring as of #27949. I will say that the current docstring is somewhat unsatisfying as far as actually understanding usage of @pure in Base. Many places where @pure is used rely on generic functions, and some even have side-effects.

@laborg
Copy link
Contributor

laborg commented Oct 18, 2023

In the meantime Base.@pure macro was documented and deprecated, so this issue can be closed.

@laborg laborg closed this as completed Oct 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs docs Documentation for this change is required
Projects
None yet
Development

No branches or pull requests

8 participants