Coder Social home page Coder Social logo

lookingglass.jl's Introduction

LookingGlass.jl

Build Status

This package contains a collection of code reflection and investigation utilities that can be (potentially) useful for understanding, analyzing, and investigating julia code and the julia compiler.

Most of the reflection functions are named hierarchically by what they reflect over. For example:

  • func_specializations(f) lists all specializations for a given function.
  • module_functions(m::Module) lists all the functions in a given module.
  • module_globals_names(m::Module) lists the names of all global variables in a given module.

Function Utilities

I've used func_specializations() and func_backedges() quite a bit when trying to understand decisions the compiler makes.

For example, we can see that julia doesn't specialize functions for Type arguments by default:

julia> isintegertype(t) = t <: Integer
isintegertype (generic function with 1 method)

julia> isintegertype(Int)
true

julia> isintegertype(Float32)
false

julia> keys(LookingGlass.func_specializations(isintegertype))
Base.KeySet for a Dict{Tuple{Method,DataType},Core.TypeMapEntry} with 1 entry. Keys:
  (isintegertype(t) in Main at none:1, Tuple{typeof(isintegertype),Type})

But it does specialize on Type arguments if you access them via a where clause:

julia> isintegertype(::Type{T}) where T = T <: Integer
isintegertype (generic function with 2 methods)

julia> isintegertype(Int)
true

julia> isintegertype(Float32)
false

julia> keys(LookingGlass.func_specializations(isintegertype))
Base.KeySet for a Dict{Tuple{Method,DataType},Core.TypeMapEntry} with 3 entries. Keys:
  (isintegertype(t) in Main at none:1, Tuple{typeof(isintegertype),Type})
  (isintegertype(::Type{T}) where T in Main at none:1, Tuple{typeof(isintegertype),Type{Float32}})

And you can use func_backedges(f) to observe inlining, among other things.

julia> foo(x) = 2x
foo (generic function with 1 method)

julia> bar(x) = foo(x) + 1
bar (generic function with 1 method)

julia> foo(2)
4

julia> LookingGlass.func_backedges(foo)
Dict{Any,Array{Any,1}} with 2 entries:
  (foo(x) in Main at none:1, Tuple{typeof(foo),Int64}) => Any[]
  :MethodTable                                         => Any[]

Module Utilities

These functions provide reflection over Modules. This can be useful for example, when working on multithreading a package, where you may want to check for potential places where data races can occur -- all global mutable state. This can be covered via:

julia> # Non-const global variables
julia> LookingGlass.module_recursive_globals_names(FixedPointDecimals, constness=:nonconst, mutability=:all)
Dict{Module,Array{Symbol,1}} with 1 entry:
  FixedPointDecimals => Symbol[]

julia> # And const-mutable global variables
julia> LookingGlass.module_recursive_globals_names(FixedPointDecimals, constness=:const, mutability=:mutable)
Dict{Module,Array{Symbol,1}} with 1 entry:
  FixedPointDecimals => Symbol[]

So we can see that FixedPointDecimals looks good. :) (Note that of course this doesn't cover all potential data races in a package, just some obvious good places to start looking.)

lookingglass.jl's People

Contributors

nhdaly avatar cscherrer avatar juliatagbot avatar

Stargazers

ebigram avatar Orestis Ousoultzoglou avatar Qi Zhang avatar Sashmit Bhaduri avatar Ondřej Slámečka avatar Krisztián Schäffer avatar  avatar Sebastian Micluța-Câmpeanu avatar Henrique Becker avatar Posnet avatar Nikolas Göbel avatar Malte Sandstede avatar Joel Mason avatar Nick Robinson avatar John Lapeyre avatar Jan Weidner avatar Peter avatar Takafumi Arakaki avatar Frames White avatar Mathieu Besançon avatar Sebastian Pfitzner avatar Eric Hanson avatar Yuri avatar

Watchers

James Cloos avatar  avatar hzgzh avatar  avatar

lookingglass.jl's Issues

TagBot trigger issue

This issue is used to trigger TagBot; feel free to unsubscribe.

If you haven't already, you should update your TagBot.yml to include issue comment triggers.
Please see this post on Discourse for instructions and more details.

If you'd like for me to do this for you, comment TagBot fix on this issue.
I'll open a PR within a few hours, please be patient!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.