Skip to content

Commit 05d0564

Browse files
authored
Profile: close files when assembling heap snapshot (#55356)
1 parent 1dffd77 commit 05d0564

File tree

1 file changed

+33
-31
lines changed

1 file changed

+33
-31
lines changed

stdlib/Profile/src/heapsnapshot_reassemble.jl

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -99,40 +99,42 @@ function assemble_snapshot(in_prefix, io::IO)
9999

100100
orphans = Set{UInt}() # nodes that have no incoming edges
101101
# Parse nodes with empty edge counts that we need to fill later
102-
nodes_file = open(string(in_prefix, ".nodes"), "r")
103-
for i in 1:length(nodes)
104-
node_type = read(nodes_file, Int8)
105-
node_name_idx = read(nodes_file, UInt)
106-
id = read(nodes_file, UInt)
107-
self_size = read(nodes_file, Int)
108-
@assert read(nodes_file, Int) == 0 # trace_node_id
109-
@assert read(nodes_file, Int8) == 0 # detachedness
110-
111-
nodes.type[i] = node_type
112-
nodes.name_idx[i] = node_name_idx
113-
nodes.id[i] = id
114-
nodes.self_size[i] = self_size
115-
nodes.edge_count[i] = 0 # edge_count
116-
# populate the orphans set with node index
117-
push!(orphans, i-1)
102+
open(string(in_prefix, ".nodes"), "r") do nodes_file
103+
for i in 1:length(nodes)
104+
node_type = read(nodes_file, Int8)
105+
node_name_idx = read(nodes_file, UInt)
106+
id = read(nodes_file, UInt)
107+
self_size = read(nodes_file, Int)
108+
@assert read(nodes_file, Int) == 0 # trace_node_id
109+
@assert read(nodes_file, Int8) == 0 # detachedness
110+
111+
nodes.type[i] = node_type
112+
nodes.name_idx[i] = node_name_idx
113+
nodes.id[i] = id
114+
nodes.self_size[i] = self_size
115+
nodes.edge_count[i] = 0 # edge_count
116+
# populate the orphans set with node index
117+
push!(orphans, i-1)
118+
end
118119
end
119120

120121
# Parse the edges to fill in the edge counts for nodes and correct the to_node offsets
121-
edges_file = open(string(in_prefix, ".edges"), "r")
122-
for i in 1:length(nodes.edges)
123-
edge_type = read(edges_file, Int8)
124-
edge_name_or_index = read(edges_file, UInt)
125-
from_node = read(edges_file, UInt)
126-
to_node = read(edges_file, UInt)
127-
128-
nodes.edges.type[i] = edge_type
129-
nodes.edges.name_or_index[i] = edge_name_or_index
130-
nodes.edges.to_pos[i] = to_node * k_node_number_of_fields # 7 fields per node, the streaming format doesn't multiply the offset by 7
131-
nodes.edge_count[from_node + 1] += UInt32(1) # C and JSON use 0-based indexing
132-
push!(nodes.edge_idxs[from_node + 1], i) # Index into nodes.edges
133-
# remove the node from the orphans if it has at least one incoming edge
134-
if to_node in orphans
135-
delete!(orphans, to_node)
122+
open(string(in_prefix, ".edges"), "r") do edges_file
123+
for i in 1:length(nodes.edges)
124+
edge_type = read(edges_file, Int8)
125+
edge_name_or_index = read(edges_file, UInt)
126+
from_node = read(edges_file, UInt)
127+
to_node = read(edges_file, UInt)
128+
129+
nodes.edges.type[i] = edge_type
130+
nodes.edges.name_or_index[i] = edge_name_or_index
131+
nodes.edges.to_pos[i] = to_node * k_node_number_of_fields # 7 fields per node, the streaming format doesn't multiply the offset by 7
132+
nodes.edge_count[from_node + 1] += UInt32(1) # C and JSON use 0-based indexing
133+
push!(nodes.edge_idxs[from_node + 1], i) # Index into nodes.edges
134+
# remove the node from the orphans if it has at least one incoming edge
135+
if to_node in orphans
136+
delete!(orphans, to_node)
137+
end
136138
end
137139
end
138140

0 commit comments

Comments
 (0)