Skip to content

Commit fde16d1

Browse files
authored
add show methods (#26)
1 parent 0ff7cca commit fde16d1

File tree

5 files changed

+72
-2
lines changed

5 files changed

+72
-2
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "AllocArrays"
22
uuid = "5c00bae2-1499-4716-9206-27f63fd08a44"
33
authors = ["Eric P. Hanson"]
4-
version = "1.0.0"
4+
version = "1.1.0"
55

66
[deps]
77
Bumper = "8ce10254-0962-460f-a3d8-1f77fea1446e"

src/alloc_interface.jl

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,10 @@ By default uses a growable [`AutoscalingAllocBuffer`](@ref) (currently), or a `A
123123
UncheckedBumperAllocator() = UncheckedBumperAllocator(AutoscalingAllocBuffer())
124124
UncheckedBumperAllocator(n_bytes::Int) = UncheckedBumperAllocator(AllocBuffer(n_bytes))
125125

126+
function Base.show(io::IO, b::UncheckedBumperAllocator)
127+
return print(io, UncheckedBumperAllocator, "(", b.buf, ")")
128+
end
129+
126130
"""
127131
reset!(B::UncheckedBumperAllocator)
128132
@@ -231,6 +235,22 @@ By default uses a growable [`AutoscalingAllocBuffer`](@ref) (currently), or a `A
231235
BumperAllocator() = BumperAllocator(AutoscalingAllocBuffer())
232236
BumperAllocator(n_bytes::Int) = BumperAllocator(AllocBuffer(n_bytes))
233237

238+
function Base.show(io::IO, b::BumperAllocator)
239+
# pass through the buf, since we auto-wrap with `UncheckedBumperAllocator` in the constructor,
240+
# and that's an implementation detail
241+
return print(io, BumperAllocator, "(", b.bumper.buf, ")")
242+
end
243+
244+
function Base.show(io::IO, ::MIME"text/plain", b::BumperAllocator)
245+
print(io, b)
246+
# for 3-arg show, we'll add a detail about the memory locations if we are tracking any
247+
if length(b.mems) > 0
248+
plural = length(b.mems) == 1 ? "" : "s"
249+
print(io, " (tracking ", length(b.mems), " CheckedAllocArray memory location$plural)")
250+
end
251+
return nothing
252+
end
253+
234254
Base.lock(B::BumperAllocator) = lock(B.lock)
235255
Base.unlock(B::BumperAllocator) = unlock(B.lock)
236256

src/autoscaling_alloc_buffer.jl

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,33 @@ mutable struct AutoscalingAllocBuffer
99
const max_history_size::Int
1010
end
1111

12+
function Base.show(io::IO, ::MIME"text/plain", b::AutoscalingAllocBuffer)
13+
tot_used = Base.format_bytes(amount_used(b))
14+
tot_capacity = Base.format_bytes(total_capacity(b))
15+
16+
if amount_used(b.main_buffer) == amount_used(b) == 0
17+
main_buffer_used_str = ""
18+
else
19+
main_buffer_used = string(round(100 * (amount_used(b.main_buffer) / amount_used(b));
20+
digits=1), "%")
21+
main_buffer_used_str = " [$main_buffer_used in main buffer]"
22+
end
23+
main_buffer_capacity = string(round(100 *
24+
(total_capacity(b.main_buffer) / total_capacity(b));
25+
digits=1), "%")
26+
27+
print(io, b)
28+
if isempty(b.additional_buffers)
29+
print(io, " ($tot_used used, capacity $tot_capacity, all in main buffer)")
30+
else
31+
plural = length(b.additional_buffers) == 1 ? "" : "s"
32+
print(io, " ($tot_used used$main_buffer_used_str, capacity $tot_capacity [$main_buffer_capacity in main buffer] with $(length(b.additional_buffers)) additional buffer$plural)")
33+
end
34+
return nothing
35+
end
36+
37+
Base.show(io::IO, ::AutoscalingAllocBuffer) = print(io, AutoscalingAllocBuffer, "()")
38+
1239
"""
1340
AutoscalingAllocBuffer(initial_buffer_size::Int=$DEFAULT_INITIAL_BUFFER_SIZE;
1441
max_history_size=$DEFAULT_HISTORY_SIZE)
@@ -23,7 +50,6 @@ This means:
2350
- for repeated runs of the same size, a single contiguous buffer will be used, which should approximately match the performance of a tuned `AllocBuffer`
2451
- `AutoscalingAllocBuffer` can reuse allocated memory between runs like `AllocBuffer`, but with safety from overrunning a fixed buffer size and OOMing like `SlabBuffer`. Additionally, `AutoscalingAllocBuffer` separately tracks the memory used by the main buffer vs the additional buffers allocated dynamically, so small unexpected additional allocations don't double the memory consumption (unlike a second slab being allocated).
2552
26-
2753
!!! note
2854
The default `initial_buffer_size` and `max_history_size` are subject to change in non-breaking releases of AllocArrays.jl in order to tune performance in common cases. Additionally, the internal heuristics likewise may change.
2955
"""

test/autoscaling_alloc_buffer.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ using Bumper: alloc!, reset_buffer!
3030
# still not too many additional buffers
3131
@test length(b.additional_buffers) < 10
3232

33+
@testset "show" begin
34+
@test sprint(show, b) == "AutoscalingAllocBuffer()"
35+
@test sprint(show, MIME"text/plain"(), b) == "AutoscalingAllocBuffer() (7.668 MiB used [0.0% in main buffer], capacity 9.690 MiB [0.0% in main buffer] with 7 additional buffers)"
36+
end
3337
# now check reset: we should have more capacity than used last time,
3438
# while having no additional buffers
3539
used = amount_used(b)
@@ -46,4 +50,10 @@ using Bumper: alloc!, reset_buffer!
4650
reset_buffer!(b)
4751
end
4852
@test total_capacity(b) < 2n
53+
54+
@testset "show" begin
55+
cap = Base.format_bytes(total_capacity(b))
56+
@test sprint(show, b) == "AutoscalingAllocBuffer()"
57+
@test sprint(show, MIME"text/plain"(), b) == "AutoscalingAllocBuffer() (0 bytes used, capacity $cap, all in main buffer)"
58+
end
4959
end

test/runtests.jl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,18 @@ end
5252
# Constructor does not recurse
5353
a = AllocArray(1:4)
5454
@test AllocArray(a).arr === a.arr
55+
56+
b = BumperAllocator()
57+
@test sprint(show, b) == "BumperAllocator(AutoscalingAllocBuffer())"
58+
@test sprint(show, MIME"text/plain"(), b) == "BumperAllocator(AutoscalingAllocBuffer())"
59+
x = with_allocator(b) do
60+
similar(c, 100, 100)
61+
end
62+
@test sprint(show, b) == "BumperAllocator(AutoscalingAllocBuffer())"
63+
@test sprint(show, MIME"text/plain"(), b) == "BumperAllocator(AutoscalingAllocBuffer()) (tracking 1 CheckedAllocArray memory location)"
64+
65+
b = UncheckedBumperAllocator()
66+
@test sprint(show, b) == "UncheckedBumperAllocator(AutoscalingAllocBuffer())"
67+
@test sprint(show, MIME"text/plain"(), b) == "UncheckedBumperAllocator(AutoscalingAllocBuffer())"
68+
5569
end

0 commit comments

Comments
 (0)