-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
Add @allocations macro for getting number of allocations
#47367
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
Conversation
base/timing.jl
Outdated
| and [`@elapsed`](@ref). | ||
| ```julia-repl | ||
| julia> @allocs rand(10^6) |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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
| end | ||
|
|
||
| """ | ||
| @allocs |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 ?
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
😂 "ballocs"
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@allocs macro for getting number of allocations@allocations macro for getting number of allocations
|
Any more comments on this (naming-related or otherwise) before we merge this? |

Adds an
@allocs@allocationsmacro (name TBD) which behaves similarly to@allocatedexcept 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_countso 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
@allocatedto get a single-number sumarry, because it's just a much easier and more discoverable interface than something likeBase.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!