Skip to content

Instantly share code, notes, and snippets.

@Azzaare
Last active September 13, 2024 02:43
Show Gist options
  • Save Azzaare/df0de142726aeedff802514a7ecb14b3 to your computer and use it in GitHub Desktop.
Save Azzaare/df0de142726aeedff802514a7ecb14b3 to your computer and use it in GitHub Desktop.
Azzaare's startup.jl
# Add this file to ~/.julia/config/ (mkdir config if necessary)
try
using Coverage
catch e
@warn "Error initializing Coverage: trying install" exception = (e, catch_backtrace())
using Pkg
Pkg.add("Coverage")
end
try
using Debugger
catch e
@warn "Error initializing Debugger: trying install" exception = (e, catch_backtrace())
using Pkg
Pkg.add("Debugger")
end
try
using DrWatson
catch e
@warn "Error initializing DrWatson: trying install" exception = (e, catch_backtrace())
using Pkg
Pkg.add("DrWatson")
end
try
using ExplicitImports
catch e
@warn "Error initializing ExplicitImports: trying install" exception = (e, catch_backtrace())
using Pkg
Pkg.add("ExplicitImports")
end
try
using Infiltrator
catch e
@warn "Error initializing Infiltrator: trying install" exception = (e, catch_backtrace())
using Pkg
Pkg.add("Infiltrator")
end
try
using JET
catch e
@warn "Error initializing JET: trying install" exception = (e, catch_backtrace())
using Pkg
Pkg.add("JET")
end
try
using LocalRegistry
catch e
@warn "Error initializing LocalRegistry: trying install" exception = (e, catch_backtrace())
using Pkg
Pkg.add("LocalRegistry")
end
try
using OhMyREPL
catch e
@warn "Error initializing OhMyREPL: trying install" exception = (e, catch_backtrace())
using Pkg
Pkg.add("OhMyREPL")
end
try
using PackageCompatUI
catch e
@warn "Error initializing PackageCompatUI: trying install" exception = (e, catch_backtrace())
using Pkg
Pkg.add("PackageCompatUI")
end
try
using PkgDependency
catch e
@warn "Error initializing PkgDependency: trying install" exception = (e, catch_backtrace())
using Pkg
Pkg.add("PkgDependency")
end
try
using PkgGraph
catch e
@warn "Error initializing PkgGraph: trying install" exception = (e, catch_backtrace())
using Pkg
Pkg.add("PkgGraph")
end
try
using Revise
catch e
@warn "Error initializing Revise: trying install" exception = (e, catch_backtrace())
using Pkg
Pkg.add("Revise")
end
try
using TestEnv
catch e
@warn "Error initializing TestEnv: trying install" exception = (e, catch_backtrace())
using Pkg
Pkg.add("TestEnv")
end
try
using Term
install_term_repr()
catch e
@warn "Error initializing Term: trying install" exception = (e, catch_backtrace())
using Pkg
Pkg.add("Term")
end
# Utility functions
"""
analyze_mallocs(dirs::V) where {S<:AbstractString,V<:AbstractVector{S}}
Analyze memory allocations in the specified directories.
This function processes multiple directories to collect and analyze memory allocation
information. It uses the `analyze_malloc` function (not shown here) to process each
directory individually, then combines and sorts the results.
# Arguments
- `dirs::V`: A vector of strings, where each string is a path to a directory to be analyzed.
# Returns
- `Vector`: A sorted vector of memory allocation information. The exact structure of the
elements depends on the implementation of `analyze_malloc`, but they are sorted in
ascending order based on the number of bytes allocated.
# Notes
- This function uses `Coverage.sortbybytes` for sorting, implying a dependency on a
`Coverage` module.
- The sorting is done in-place to optimize memory usage.
# Example
```julia
dirs = ["path/to/dir1", "path/to/dir2"]
results = analyze_mallocs(dirs)
```
"""
function analyze_mallocs(dirs::V) where {S<:AbstractString,V<:AbstractVector{S}}
mallocs = collect(Iterators.flatten(map(analyze_malloc, dirs)))
sort!(mallocs, lt=Coverage.sortbybytes)
return mallocs
end
"""
subtype_tree(root_type; level=1, indent=4, lasts=Dict(1 => true), ancestor=0)
Generate and print a hierarchical tree representation of the subtype structure for a given type.
This function recursively explores the subtype hierarchy of `root_type` and prints it as a
tree structure. It handles both downward (subtypes) and upward (supertypes) traversal.
# Arguments
- `root_type`: The type to start the subtype tree from.
# Keyword Arguments
- `level::Int=1`: Current depth level in the type hierarchy (used for recursion).
- `indent::Int=4`: Number of spaces for each indentation level.
- `lasts::Dict{Int,Bool}=Dict(1 => true)`: Tracks whether each level is the last in its branch.
- `ancestor::Int=0`: Number of levels to traverse upwards in the type hierarchy.
# Behavior
1. If `ancestor > 0`, it recursively calls itself with the supertype.
2. Prints the root type at the first level.
3. Iterates through subtypes, printing them with appropriate tree structure characters.
4. Recursively calls itself for each subtype to explore deeper levels.
# Tree Structure Characters
- `│`: Vertical line for continuing branches
- `├`: T-shaped character for non-last items
- `└`: L-shaped character for last items in a branch
- `───`: Horizontal lines connecting items
# Example
```julia
subtype_tree(Number)
This will print the subtype hierarchy of `Number`.
# Notes
The function modifies the `lasts` dictionary in-place to track the last elements at each level.
The tree structure adapts based on whether an item is the last in its branch.
"""
function subtype_tree(root_type;
level=1, indent=4, lasts=Dict(1 => true), ancestor=0
)
if ancestor > 0
return subtype_tree(supertype(root_type); indent, ancestor=ancestor - 1)
end
# Root type printing
level == 1 && println(root_type)
# Determine the correct vertival character
vc(lvl) = get!(lasts, lvl, false) ? " " : "│"
st = subtypes(root_type)
for (i, s) in enumerate(st)
# Markers for entering and leaving levels
i == 1 && setindex!(lasts, false, level)
i == length(st) && setindex!(lasts, true, level)
# Horizontal character
hc = get!(lasts, level, false) ? "└" : "├"
# Actual printing
lines = mapreduce(l -> vc(l) * repeat(" ", indent), *, 1:(level-1); init="")
println(lines * hc * "───" * string(s))
# Next child
subtype_tree(s; level=level + 1, indent, lasts)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment