-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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
Logging macros generated code quality #25909
Comments
If you take the julia> function foo(x)
@error "1"
@error "2"
return x
end
foo (generic function with 1 method)
julia> foo(2)
┌ Error: 1
└ @ Main REPL[15]:2
┌ Error: 2
└ @ Main REPL[15]:3
2 |
Perhaps we should rename |
Ha, yeah, I should've executed this code instead of only staring at lowered code. Anyhow, most of the issue still remains. Instead comparing |
This lowers to: import Base.CoreLogging
function bar(a)
@assert a == 0
x = rand(UInt32)
if x == 0
throw(1)
elseif x == 1
level = CoreLogging.Info
std_level = CoreLogging.convert(CoreLogging.LogLevel, level)
if std_level >= CoreLogging._min_enabled_level[]
logstate = CoreLogging.current_logstate()
if std_level >= logstate.min_enabled_level
logger = logstate.logger
_module = Main
id = :Main_5106d4ea
group = Symbol("REPL[1]")
if CoreLogging.shouldlog(logger, level, _module, group, id)
create_msg = (logger, level, _module, group, id, file, line) -> begin
msg = "$x"
CoreLogging.handle_message(logger, level, msg, _module, group, id, file, line; )
end
file = "REPL[1]"
line = 10
CoreLogging.dispatch_message(logger, level, _module, group, id, file, line, create_msg)
end
end
end
CoreLogging.nothing
end
return nothing
end Or, simplified (but exhibiting the same issue): function bar(a)
@assert a == 0
x = rand(UInt32)
if x == 0
throw(1)
elseif x == 1
level = CoreLogging.Info
logstate = CoreLogging.current_logstate()
logger = logstate.logger
create_msg = (logger, level, _module, group, id, file, line) -> begin
msg = "$x"
CoreLogging.handle_message(logger, level, msg, _module, group, id, file, line)
end
create_msg(logger, level, Main, Symbol("REPL[1]"), :Main_5106d4ea, "REPL", 10)
end
return nothing
end At this point, it looks like a closure capture issue. And indeed, passing the message as an additional argument to So this is just another dup of #15276. Sorry for the noise. |
So does let x=x
@info "$x"
end give better code? |
Yeah. But that's hard to generate from within the logging macro's. |
It would be really nice if every logging call did not introduce a new anonymous function. |
Ah, I thought this might fall afoul of #15276, I was hoping that you'd wave your magic compiler wand Jeff, and make that one go away before 0.7 ;-) At the moment closure creation is actually optional as the public logging API didn't end up depending on it. So I think we can simply lift the try/catch out of At the moment I don't have a good mental model of the cost in creating a closure vs inlining the extra try/catch stuff at every logging call site. I suppose inlining it with the macro is strictly easier on the compiler. I'll make a PR for this. |
As a side note, generating the |
I'm not too worried about #15276 here; creating large numbers of functions is just generally hard on the compiler and takes up a lot of space. |
I noticed some badly inferred generated code after switching from
info
to@info
, and wondered whether this is known/expected.Repro:
With
info
, this generates code with twoUnion{}
return paths, and otherwise no badly typed values (ie. nothing red incode_warntype
).Using
@info
, there's now aCore.Box
, an abstractBase.CoreLogging.AbstractLogger
, and anAny
fromBase.CoreLogging.shouldlog
. Performance is bad too, increased from ~3ns to ~45ns (tested on latest master).Part of this issue relies on the exact structure of
bar
, eg. removing the@assert
removes the box and restores performance, but keeps theBase.CoreLogging.AbstractLogger
. So there's probably something more subtle going on here.cc @c42f
EDIT: changed from
error
toinfo
, I was confused about the behavior of@error
.The text was updated successfully, but these errors were encountered: