Compile-time constants

  1. Local constant declaration is ignored, they have no effect.
  2. It is inlined, the extra code you get are simply due to different type.
julia> let
           FLAGA = 1<<32
           FLAGB = 1<<33

           global foo
           function foo(x::Int)
               return (0== (x & (FLAGA & FLAGB)))
           end
       end
foo (generic function with 1 method)

julia> @code_native foo(1)
        .text
; Function <invalid> {
; Location: REPL[1]
        pushq   %rbp
        movq    %rsp, %rbp
;}
; Function foo {
; Location: REPL[1]:7
        movb    $1, %al
        popq    %rbp
        retq
        nopl    (%rax,%rax)
;}

julia> @code_warntype foo(1)
Variables:
  #self# <optimized out>
  x::Int64

Body:
  begin
      return (0 === (Base.and_int)(x::Int64, (Base.and_int)($(QuoteNode(4294967296)), $(QuoteNode(8589934592)))::Int64)::Int64)::Bool
  end::Bool

Also note that how much easier it is to see if the constant is inlined if you use the right tool (i.e. code_warntype)

1 Like