Skip to content

Base: Add count argument to Base.summarysize #58874

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

topolarity
Copy link
Member

In many cases, number of allocations is more important than memory allocated (w.r.t. performance).

This adds a small utility to summarysize to allow counting the number of unique reachable objects, which can be useful to understand, e.g., if you are performing many small allocations or how many of the allocations in a function ended up reachable from the result.

@topolarity topolarity requested a review from gbaraldi July 2, 2025 10:24
Comment on lines +137 to +139
size::Int = (ss.count ? 1 : (7 * Core.sizeof(Int) + 6 * Core.sizeof(Int32)))
size += (ss.count ? 1 : (4 * nfields(obj) + ifelse(Sys.WORD_SIZE == 64, 4, 0)))
Copy link
Member Author

Choose a reason for hiding this comment

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

I don't know where these magic numbers were pulled from, so I'm not sure if this is 1 object or 2 (or 3?)

I'll need to reverse engineer before merging.

@jakobnissen
Copy link
Member

It would be good to specify in the docstring whether count returns the number of heap allocations or the number of Julia objects. Does a 1000-element Memory of integers count as one or 1000?

@topolarity topolarity force-pushed the ct/summarysize-count branch from 0a37de0 to 345a791 Compare July 2, 2025 11:35
Frequently the number of allocations can be more important for
performance than the amount of memory allocated.

This adds a small utility to `summarysize` to allow counting the number
of unique reachable objects, which can be useful to understand, e.g.,
if you are performing many small allocations or how many of the
allocations in a function ended up reachable from the result.
@topolarity topolarity force-pushed the ct/summarysize-count branch from 345a791 to f254a9d Compare July 2, 2025 11:53
end

nth_pointer_isdefined(obj, i::Int) = ccall(:jl_nth_pointer_isdefined, Cint, (Any, Csize_t), obj, i-1) != 0
get_nth_pointer(obj, i::Int) = ccall(:jl_get_nth_pointer, Any, (Any, Csize_t), obj, i-1)

"""
Base.summarysize(obj; exclude=Union{...}, chargeall=Union{...})::Int
Base.summarysize(obj; count = false, exclude=Union{...}, chargeall=Union{...})::Int
Copy link
Member Author

Choose a reason for hiding this comment

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

This might be clearer as:

Suggested change
Base.summarysize(obj; count = false, exclude=Union{...}, chargeall=Union{...})::Int
Base.summarysize(obj; count::Symbol = :size, exclude=Union{...}, chargeall=Union{...})::Int

so the new functionality would be summarysize(x; count=:allocations)

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.

3 participants