Skip to content

Commit

Permalink
Merge pull request #164 from invenia/rf/precompile
Browse files Browse the repository at this point in the history
First pass at improving load times
  • Loading branch information
rofinn authored Aug 19, 2020
2 parents 6051407 + 4c092dc commit ad32ea8
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name = "Memento"
uuid = "f28f55f0-a522-5efc-85c2-fe41dfb9b2d9"
license = "MIT"
authors = ["Invenia Technical Computing"]
version = "1.1.0"
version = "1.1.1"

[deps]
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
Expand Down
2 changes: 2 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 @@ -65,4 +66,5 @@ function __init__()
Memento.register(LOGGER)
end

_precompile_()
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)
DefaultFormatter(fmt),
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}

function DefaultFormatter(
fmt_str::AbstractString=DEFAULT_FMT_STRING,
output_tz=localzone(),
output_tz=nothing,
)
#r"(?<={).+?(?=})
tokens = map(eachmatch(r"({.+?})|(.+?)", fmt_str)) do m
Expand All @@ -45,6 +45,7 @@ struct DefaultFormatter <: Formatter
end
end


"""
format(::DefaultFormatter, ::Record) -> String
Expand Down Expand Up @@ -77,7 +78,9 @@ 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 expensive, so we don't call it until it is required.
tzout = fmt.output_tz === nothing ? localzone() : 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
4 changes: 4 additions & 0 deletions src/precompile.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
function _precompile_()
# Precompiling this one methods seems to cut our allocations during package loading in half.
@assert precompile(Tuple{typeof(Base.log), Memento.Logger, Memento.DefaultRecord})
end

2 comments on commit ad32ea8

@rofinn
Copy link
Member Author

@rofinn rofinn commented on ad32ea8 Aug 19, 2020

Choose a reason for hiding this comment

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

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

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

Registration pull request created: JuliaRegistries/General/19790

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v1.1.1 -m "<description of version>" ad32ea8b70ff0dc1e96768b61ef484749640faef
git push origin v1.1.1

Please sign in to comment.