Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

First pass at improving load times #164

Merged
merged 5 commits into from
Aug 19, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/Memento.jl
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ include("config.jl")
include("exceptions.jl")
include("memento_test.jl")
include("deprecated.jl")
include("precompile.jl")

# Initializing at compile-time will work as long as the loggers which are added do not
# contain references to stdout.
Expand All @@ -60,9 +61,22 @@ const _loggers = Dict{AbstractString, Logger}(

const LOGGER = getlogger(@__MODULE__)

# Cached values either to lazily load or precompile.
const _default_formatter = DefaultFormatter(DEFAULT_FMT_STRING)
const _localtz = Ref{Union{Dates.TimeZone, Nothing}}(nothing)

function getlocalzone()
if _localtz[] === nothing
global _localtz[] = localzone()
end

return _localtz[]
end
rofinn marked this conversation as resolved.
Show resolved Hide resolved

function __init__()
Memento.config!(DEFAULT_LOG_LEVEL)
Memento.register(LOGGER)
oxinabox marked this conversation as resolved.
Show resolved Hide resolved
end

_precompile_()
oxinabox marked this conversation as resolved.
Show resolved Hide resolved
end
3 changes: 2 additions & 1 deletion src/config.jl
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ function config!(
setpropagating!(logger, propagate)
handler = DefaultHandler(
stdout,
DefaultFormatter(fmt), Dict{Symbol, Any}(:is_colorized => colorized)
fmt == DEFAULT_FMT_STRING ? _default_formatter : DefaultFormatter(fmt),
rofinn marked this conversation as resolved.
Show resolved Hide resolved
Dict{Symbol, Any}(:is_colorized => colorized),
)
logger.handlers["console"] = handler
register(logger)
Expand Down
9 changes: 6 additions & 3 deletions src/formatters.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ Ex) "[{level} | {name}]: {msg}" will print message of the form
struct DefaultFormatter <: Formatter
fmt_str::AbstractString
tokens::Vector{Pair{Symbol, Bool}}
output_tz::Dates.TimeZone
output_tz::Union{Dates.TimeZone, Nothing}
Copy link
Member

@oxinabox oxinabox Aug 18, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Beyond the scope of this PR maybe:
I wonder if we should have a timezone in TimeZones.jl called User or UserLocal,
which has the behavour of that having this as nothing does; where it is a caching version of localzone.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I was talking to Curtis a bit about it, but I'd have to think more about how it should work and if that has some potential downfalls. Looks like this caching behaviour might be causing issues on Linux.


function DefaultFormatter(
fmt_str::AbstractString=DEFAULT_FMT_STRING,
output_tz=localzone(),
output_tz=nothing,
)
#r"(?<={).+?(?=})
tokens = map(eachmatch(r"({.+?})|(.+?)", fmt_str)) do m
Expand Down Expand Up @@ -77,7 +77,10 @@ function format(fmt::DefaultFormatter, rec::Record)
value = string(" stack:[", join(str_frames, ", "), "]")
elseif content === :date
value = if tmp_val isa ZonedDateTime
Dates.format(astimezone(tmp_val, fmt.output_tz), DATE_FMT_STRING)
# `localzone` is somewhat expensive, so we both delay calling it and
# cache the result with our `getlocalzone` function.
tzout = fmt.output_tz === nothing ? getlocalzone() : fmt.output_tz
Dates.format(astimezone(tmp_val, tzout), DATE_FMT_STRING)
elseif tmp_val isa DateTime
Dates.format(tmp_val, DATE_FMT_STRING)
else
Expand Down
38 changes: 38 additions & 0 deletions src/precompile.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
function _precompile_()
ccall(:jl_generating_output, Cint, ()) == 1 || return nothing
# Memento
isdefined(Memento, Symbol("##config!#72")) && precompile(Tuple{getfield(Memento, Symbol("##config!#72")), Base.Iterators.Pairs{Union{}, Union{}, Tuple{}, NamedTuple{(), Tuple{}}}, typeof(Memento.config!), String, String})
rofinn marked this conversation as resolved.
Show resolved Hide resolved
@assert precompile(Tuple{typeof(Memento._log), Memento.Logger, String, String})
@assert precompile(Tuple{typeof(Memento.config!), String})
@assert precompile(Tuple{typeof(Memento.getlogger), String})
@assert precompile(Tuple{typeof(Memento.getpath), Memento.Logger})
@assert precompile(Tuple{typeof(Memento.isroot), Memento.Logger})
@assert precompile(Tuple{typeof(Memento.register), Memento.Logger})

# Unknown
@assert precompile(Tuple{Type{Memento.DefaultHandler{F, O} where O<:IO where F}, Base.TTY, Memento.DefaultFormatter, Base.Dict{Symbol, Any}})
@assert precompile(Tuple{Type{Memento.DefaultRecord}, String, String, Int64, String})
isdefined(Memento, Symbol("#level_filter#36")) && precompile(Tuple{getfield(Memento, Symbol("#level_filter#36")){Memento.Logger}, Memento.DefaultRecord})

# Base
isdefined(Base, Symbol("##all#638")) && precompile(Tuple{getfield(Base, Symbol("##all#638")), typeof(identity), typeof(Base.all), typeof(identity), Array{Memento.Filter, 1}})
@assert precompile(Tuple{typeof(Base.all), typeof(identity), Array{Memento.Filter, 1}})
@assert precompile(Tuple{typeof(Base.get), Memento.Attribute{String}})
@assert precompile(Tuple{typeof(Base.log), Memento.Logger, Memento.DefaultRecord})
@assert precompile(Tuple{typeof(Base.print), Base.GenericIOBuffer{Array{UInt8, 1}}, Memento.Logger})
@assert precompile(Tuple{typeof(Base.print_to_string), Memento.Logger, Int})
@assert precompile(Tuple{typeof(Base.reverse!), Array{Memento.Logger, 1}, Int64, Int64})
@assert precompile(Tuple{typeof(Base.setindex!), Array{Memento.Logger, 1}, Memento.Logger, Int64})
@assert precompile(Tuple{typeof(Base.setindex!), Base.Dict{Any, Memento.Handler{F} where F<:Memento.Formatter}, Memento.DefaultHandler{Memento.DefaultFormatter, Base.TTY}, String})
@assert precompile(Tuple{typeof(Base.show), Base.GenericIOBuffer{Array{UInt8, 1}}, Memento.Logger})

# Manual
@assert precompile(Tuple{typeof(Memento.config!), String, String})
@assert precompile(Tuple{typeof(Memento.config!), Memento.Logger, String})
@assert precompile(Tuple{typeof(Base.log), Memento.Logger, String, String})
@assert precompile(Tuple{typeof(Memento.trace), Memento.Logger, String})
@assert precompile(Tuple{typeof(Memento.debug), Memento.Logger, String})
@assert precompile(Tuple{typeof(Memento.info), Memento.Logger, String})
@assert precompile(Tuple{typeof(Memento.warn), Memento.Logger, String})
@assert precompile(Tuple{typeof(Base.getindex), Base.Dict{AbstractString, Memento.Logger}, String})
end