Skip to content

Commit

Permalink
Merge pull request #88 from JuliaData/jps/approx_size-opt
Browse files Browse the repository at this point in the history
Optimizations to `approx_size` and `ensure_memory_reserved`
  • Loading branch information
jpsamaroo authored Aug 1, 2024
2 parents c368763 + 8107627 commit 2d3bb7d
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 12 deletions.
8 changes: 6 additions & 2 deletions src/MemPool.jl
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,12 @@ include("datastore.jl")
Returns the size of `d` in bytes used for accounting in MemPool datastore.
"""
function approx_size(@nospecialize(d))
Base.summarysize(d) # note: this is accurate but expensive
function approx_size(d::T) where T
if Base.datatype_pointerfree(T)
return sizeof(d)
else
Base.summarysize(d) # note: this is accurate but expensive
end
end

function approx_size(d::Union{Base.BitInteger, Float16, Float32, Float64})
Expand Down
8 changes: 4 additions & 4 deletions src/datastore.jl
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ const MEM_RESERVE_LOCK = Threads.ReentrantLock()
const MEM_RESERVE_SWEEPS = Ref{Int}(3)

"""
When called, ensures that at least `MEM_RESERVED[] + size` bytes are available
When called, ensures that at least `MEM_RESERVED[]` bytes are available
to the OS. If there is not enough memory available, then a variety of calls to
the GC are performed to free up memory until either the reservation limit is
satisfied, or `max_sweeps` number of cycles have elapsed.
Expand All @@ -396,15 +396,15 @@ function ensure_memory_reserved(size::Integer=0; max_sweeps::Integer=MEM_RESERVE
max_sweeps == 0 && return

# Do a quick (cached) check, to optimize for many calls to this function when memory isn't tight
if Int(storage_available(CPURAMResource())) - size >= MEM_RESERVED[]
if Int(storage_available(CPURAMResource())) >= MEM_RESERVED[]
return
end

# Check whether the OS is running tight on memory
sweep_ctr = 0
while true
with(QUERY_MEM_OVERRIDE => true) do
Int(storage_available(CPURAMResource())) - size < MEM_RESERVED[]
Int(storage_available(CPURAMResource())) < MEM_RESERVED[]
end || break

# We need more memory! Let's encourage the GC to clear some memory...
Expand All @@ -425,7 +425,7 @@ function ensure_memory_reserved(size::Integer=0; max_sweeps::Integer=MEM_RESERVE
yield()

# Wait for send queue to clear
while SEND_QUEUE.processing
while SEND_QUEUE.processing || !isempty(SEND_QUEUE.queue)
yield()
end

Expand Down
11 changes: 5 additions & 6 deletions src/storage.jl
Original file line number Diff line number Diff line change
Expand Up @@ -163,12 +163,11 @@ struct CPURAMResource <: StorageResource end
if Sys.islinux()
function free_memory()
open("/proc/meminfo", "r") do io
# skip first 2 lines
readline(io)
readline(io)
line = readline(io)
free = match(r"MemAvailable:\s*([0-9]*)\s.*", line).captures[1]
parse(UInt64, free) * 1024
# TODO: Cache in TLS
buf = zeros(UInt8, 128)
readbytes!(io, buf)
free = match(r"MemAvailable:\s*([0-9]*)\s.*", String(buf)).captures[1]
return parse(UInt64, free) * 1024
end
end
else
Expand Down

0 comments on commit 2d3bb7d

Please sign in to comment.