Skip to content

Instantly share code, notes, and snippets.

@jdm
Created April 22, 2026 18:51
Show Gist options
  • Select an option

  • Save jdm/f1eb95a2bc8b86d886c78e47e16d4fd4 to your computer and use it in GitHub Desktop.

Select an option

Save jdm/f1eb95a2bc8b86d886c78e47e16d4fd4 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
# Copyright 2025 The Servo Project Developers. See the COPYRIGHT
# file at the top-level directory of this distribution.
#
# Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
# http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
# <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
# option. This file may not be copied, modified, or distributed
# except according to those terms.
# usage: python3 untracked-allocations.py [file] [crate-name] [max stacks]
import operator
import sys
with open(sys.argv[1]) as f:
lines = f.readlines()
crates = {}
skippable_prefixes = [
"/Users/jdm/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/",
"/Users/jdm/.cargo/git/checkouts/",
"/Users/jdm/.rustup/toolchains/1.85.0-aarch64-apple-darwin/lib/rustlib/src/rust/library/",
"/Users/jdm/.rustup/toolchains/1.89.0-aarch64-apple-darwin/lib/rustlib/src/rust/library/",
"/Users/jdm/src/servo/target/debug/build/",
"/Users/jdm/src/alt-servo/target/debug/build/",
"/rustc/4d91de4e48198da2e33413efdcd9cd2cc0c46688/library/",
"/rustc/29483883eed69d5fb4db01964cdf2af4d86e9cb2/library/",
"/rust/deps/",
"???:0",
"/Users/jdm/src/servo/components/shared/",
"/Users/jdm/src/servo/components/",
"/Users/jdm/src/servo/ports/",
"/Users/jdm/src/alt-servo/components/shared/",
"/Users/jdm/src/alt-servo/components/",
"/Users/jdm/src/alt-servo/ports/",
]
skippable_crates = [
"thin-vec-0.2.14",
"alloc",
"core",
"std",
"bytes-1.10.1",
"servo_arc",
"smallvec-1.15.1",
"hashbrown-0.15.2",
"hashbrown-0.15.5",
"indexmap-2.10.0",
"slotmap-1.0.7",
"smallbitvec-2.6.0",
]
entries = []
current = None
for line in lines:
line = line.strip()
if line == "---":
if current:
if not current["highlight"]:
current["highlight"] = {"crate": "", "filename": "", "method": ""}
entries += [current]
current = {
"size": 0,
"highlight": None,
"frames": [],
}
continue
if line.isdigit():
current["size"] = int(line)
continue
if not line:
continue
current["frames"] += [line]
if not current["highlight"]:
# print(line)
(path, method) = line.split(" ", maxsplit=1)
original_path = path
# print(path)
for prefix in skippable_prefixes:
path = path.removeprefix(prefix)
if not path:
continue
# print(path)
crate = path.split("/")[0]
if not crate:
print(original_path)
raise "oops"
# print(crate)
if crate in skippable_crates:
continue
method = "::".join(method[1:-1].split("::")[:-1])
filename = "/".join(path.split("/")[1:]).split(":")[0]
current["highlight"] = {"crate": crate, "method": method, "filename": filename}
if crate not in crates:
crates[crate] = 0
crates[crate] += 1
print("Overall stats")
for crate in sorted(crates, key=lambda x: crates[x], reverse=True):
print(f"{crate}: {crates[crate]}")
if len(sys.argv) > 2:
print()
relevant = filter(lambda entry: entry["highlight"]["crate"] == sys.argv[2], entries)
count = 0
limit = int(sys.argv[3]) if len(sys.argv) > 3 else 10
for entry in sorted(relevant, key=lambda x: x["size"], reverse=True):
if count == limit:
break
print(f"{entry['size']} - {entry['highlight']['method']}")
count += 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment