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

Add @allocations macro for getting number of allocations #47367

Merged
merged 4 commits into from
Nov 4, 2022

Conversation

nickrobinson251
Copy link
Contributor

@nickrobinson251 nickrobinson251 commented Oct 28, 2022

Adds an @allocs @allocations macro (name TBD) which behaves similarly to @allocated except for returning a count of the allocations during evaluation of the expression rather than the total size in bytes of the memory allocated.

This uses Base.gc_alloc_count so returns a single number combining all types of allocations (malloc, realloc, poolalloc, bigalloc), like what is printed via @time. The more granular break down is still available from @timed (or printed via @timev). i think the single number is useful for some cases (when you're happy to treat all allocations as equal).

The motivation is to make it just as easy to get number of allocations as it is to get number of bytes allocated. For example, at RAI we have some people investigating sources of allocations and are often more concerned about the number of allocations than the size of memory allocated, yet i still see people reach for @allocated to get a single-number sumarry, because it's just a much easier and more discoverable interface than something like Base.gc_alloc_count((@timed foo()).gcstats).

Opened this as a PR as it wasn't too much harder than opening an issue - discussion welcome! Would appreciate pointers on implementation and testing too, if we're happy with this in prospect. Thanks!

base/timing.jl Show resolved Hide resolved
base/timing.jl Show resolved Hide resolved
base/timing.jl Outdated
and [`@elapsed`](@ref).

```julia-repl
julia> @allocs rand(10^6)
Copy link
Contributor Author

@nickrobinson251 nickrobinson251 Oct 28, 2022

Choose a reason for hiding this comment

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

i just copied the example from @allocated, happy to change or add others if people prefer... it's not obvious to me why the answer here is 2 not 1 😂 which may make it a good or a bad example depending on your point of view

test/misc.jl Outdated

# sanity check `@allocs` returns what we expect in some very simple cases
@test (@alloc "a") == 0
@test (@alloc "a" * "b") == 1
Copy link
Contributor Author

Choose a reason for hiding this comment

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

i didn't see any tests for @allocated so wasn't sure how to approach testing... advice appreciated!

base/timing.jl Outdated
@@ -430,6 +430,33 @@ macro allocated(ex)
end
end

"""
@allocs
Copy link
Contributor Author

Choose a reason for hiding this comment

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

name suggestions welcome, this just felt obvious/easy

Copy link
Member

Choose a reason for hiding this comment

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

maybe @nallocs similar to ndigits ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

changed to @allocations for now, as it leads to a less bad name in BenchmarkTools.jl (where the corresponding name would be @ballocations)

Copy link
Member

Choose a reason for hiding this comment

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

😂 "ballocs"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, would have been unfortunate

Copy link
Member

Choose a reason for hiding this comment

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

@nickrobinson251 nickrobinson251 changed the title Add @allocs macro for getting number of allocations Add @allocations macro for getting number of allocations Oct 31, 2022
@nickrobinson251
Copy link
Contributor Author

Any more comments on this (naming-related or otherwise) before we merge this?

@quinnj quinnj merged commit 242cdc5 into JuliaLang:master Nov 4, 2022
@nickrobinson251 nickrobinson251 deleted the npr/allocs-macro branch November 4, 2022 16: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.

4 participants