我编写了一个小函数来获取结构内部字段的字节偏移,但是要实现这一点,我每次都要在字段上进行迭代,直到找到符号为止。但是在C语言中,offsetof()仅在编译时计算一次,因为偏移量不再在结构内部改变。我想知道如何尽可能地优化它,因为它是一个非常基本的功能,我需要经常为每个帧调用此功能(3D游戏引擎)。

function offsetof(type_, member::Symbol)
  for (i, item) in enumerate(fieldnames(type_))
    if item == member
      return fieldoffset(type_, i)
    end
    #print(typeof(i))
  end
  # what to do when symbol not in type_?
  throw("$type_ has no member named $member")
end


用法:

type ABC
    a::Int64
    b::Int64
    c::Int64
end
offsetof(ABC, :a) # 0
offsetof(ABC, :b) # 8
offsetof(ABC, :c) # 16


查看LLVM代码,其中有很多代码:

@code_llvm offsetof(Model_s, :fov)


输出:

; Function Attrs: uwtable
define i64 @julia_offsetof_61750(i8**, i8**) #0 !dbg !5 {
top:
  %2 = call i8**** @jl_get_ptls_states() #6
  %3 = alloca [13 x i8**], align 8
  %.sub = getelementptr inbounds [13 x i8**], [13 x i8**]* %3, i64 0, i64 0
  %4 = getelementptr [13 x i8**], [13 x i8**]* %3, i64 0, i64 8
  %5 = getelementptr [13 x i8**], [13 x i8**]* %3, i64 0, i64 2
  %6 = getelementptr [13 x i8**], [13 x i8**]* %3, i64 0, i64 3
  %7 = getelementptr [13 x i8**], [13 x i8**]* %3, i64 0, i64 4
  %8 = getelementptr [13 x i8**], [13 x i8**]* %3, i64 0, i64 5
  %9 = getelementptr [13 x i8**], [13 x i8**]* %3, i64 0, i64 6
  %10 = getelementptr [13 x i8**], [13 x i8**]* %3, i64 0, i64 7
  %11 = bitcast i8*** %4 to i8*
  call void @llvm.memset.p0i8.i32(i8* %11, i8 0, i32 40, i32 8, i1 false)
  %12 = bitcast [13 x i8**]* %3 to i64*
  %13 = bitcast i8*** %5 to i8*
  call void @llvm.memset.p0i8.i64(i8* %13, i8 0, i64 40, i32 8, i1 false)
  store i64 22, i64* %12, align 8
  %14 = getelementptr [13 x i8**], [13 x i8**]* %3, i64 0, i64 1
  %15 = bitcast i8**** %2 to i64*
  %16 = load i64, i64* %15, align 8
  %17 = bitcast i8*** %14 to i64*
  store i64 %16, i64* %17, align 8
  store i8*** %.sub, i8**** %2, align 8
  store i8** null, i8*** %10, align 8
  %18 = getelementptr [13 x i8**], [13 x i8**]* %3, i64 0, i64 12
  %19 = getelementptr [13 x i8**], [13 x i8**]* %3, i64 0, i64 11
  %20 = getelementptr [13 x i8**], [13 x i8**]* %3, i64 0, i64 10
  %21 = getelementptr [13 x i8**], [13 x i8**]* %3, i64 0, i64 9
  %22 = call i8** @jlsys_fieldnames_43495(i8** inttoptr (i64 375726352 to i8**))
  store i8** %22, i8*** %5, align 8
  %23 = getelementptr inbounds i8*, i8** %22, i64 1
  %24 = bitcast i8** %23 to i64*
  %25 = getelementptr i8*, i8** %22, i64 3
  %26 = bitcast i8** %25 to i64*
  %27 = bitcast i8** %22 to i8****
  %28 = bitcast i8**** %2 to i8*
  br label %L3

L3:                                               ; preds = %cont2, %top
  %"#temp#1.sroa.4.0" = phi i64 [ 1, %top ], [ %47, %cont2 ]
  %29 = load i64, i64* %24, align 8
  %30 = add i64 %29, 1
  %31 = icmp eq i64 %"#temp#1.sroa.4.0", %30
  br i1 %31, label %L33, label %if

if:                                               ; preds = %L3
  %32 = add i64 %"#temp#1.sroa.4.0", -1
  %33 = load i64, i64* %26, align 8
  %34 = icmp ult i64 %32, %33
  br i1 %34, label %idxend, label %oob

L33:                                              ; preds = %L3
  store i8** inttoptr (i64 77348880 to i8**), i8*** %4, align 8
  store i8** inttoptr (i64 77362488 to i8**), i8*** %21, align 8
  store i8** inttoptr (i64 375726352 to i8**), i8*** %20, align 8
  store i8** inttoptr (i64 377039808 to i8**), i8*** %19, align 8
  store i8** %1, i8*** %18, align 8
  %35 = call i8** @"jsys1_#print_to_string#229_39171"(i8** inttoptr (i64 77362448 to i8**), i8*** %4, i32 5)
  store i8** %35, i8*** %10, align 8
  call void @jl_throw(i8** %35)
  unreachable

oob:                                              ; preds = %if
  %36 = alloca i64, align 8
  store i64 %"#temp#1.sroa.4.0", i64* %36, align 8
  call void @jl_bounds_error_ints(i8** %22, i64* nonnull %36, i64 1)
  unreachable

idxend:                                           ; preds = %if
  %37 = load i8***, i8**** %27, align 8
  %38 = getelementptr i8**, i8*** %37, i64 %32
  %39 = load i8**, i8*** %38, align 8
  %40 = icmp eq i8** %39, null
  br i1 %40, label %fail, label %cont2

fail:                                             ; preds = %idxend
  call void @jl_throw(i8** inttoptr (i64 84505768 to i8**))
  unreachable

cont2:                                            ; preds = %idxend
  store i8** %39, i8*** %6, align 8
  %41 = call i8** @jl_gc_pool_alloc(i8* %28, i32 1512, i32 32)
  %42 = getelementptr i8*, i8** %41, i64 -1
  %43 = bitcast i8** %42 to i8***
  store i8** inttoptr (i64 109041296 to i8**), i8*** %43, align 8
  store i8** %41, i8*** %7, align 8
  %44 = getelementptr i8*, i8** %41, i64 1
  %45 = bitcast i8** %44 to i8***
  %46 = bitcast i8** %41 to i64*
  store i64 %"#temp#1.sroa.4.0", i64* %46, align 16
  store i8** %39, i8*** %45, align 8
  %47 = add i64 %"#temp#1.sroa.4.0", 1
  store i8** %39, i8*** %8, align 8
  store i8** %39, i8*** %9, align 8
  %48 = icmp eq i8** %39, %1
  br i1 %48, label %if3, label %L3

if3:                                              ; preds = %cont2
  %sext = shl i64 %"#temp#1.sroa.4.0", 32
  %49 = ashr exact i64 %sext, 32
  %50 = icmp eq i64 %49, %"#temp#1.sroa.4.0"
  br i1 %50, label %pass5, label %fail4

fail4:                                            ; preds = %if3
  call void @jl_throw(i8** inttoptr (i64 77358144 to i8**))
  unreachable

pass5:                                            ; preds = %if3
  %51 = trunc i64 %"#temp#1.sroa.4.0" to i32
  %52 = call i64 inttoptr (i64 1693737504 to i64 (i8**, i32)*)(i8** inttoptr (i64 375726352 to i8**), i32 %51)
  %53 = load i64, i64* %17, align 8
  store i64 %53, i64* %15, align 8
  ret i64 %52
}

最佳答案

这可能是Base.@pure表示法的用例。对于问题中的示例,它将去:

# note the Base.@pure notation at beginning of definition:

Base.@pure function offsetof(type_, member::Symbol)
  for (i, item) in enumerate(fieldnames(type_))
    if item == member
      return fieldoffset(type_, i)
    end
    #print(typeof(i))
  end
  # what to do when symbol not in type_?
  throw("$type_ has no member named $member")
end


现在,我们可以:

type ABC
    a::Int64
    b::Int64
    c::Int64
end
offsetof(ABC, :a) # 0


并且@code_llvmoffsetof仍然很长。但是,如果我们在函数中使用它,Julia将在编译时运行offsetof(这是允许的,因为纯函数应该依赖于它们的参数,除了返回从它们计算出的值外,什么也不做)。例如:

julia> f() = offsetof(ABC,:b)
f (generic function with 1 method)

julia> f()
0x0000000000000008

julia> @code_llvm f()

define i64 @julia_f_63287() #0 !dbg !5 {
top:
  %ptls_i8 = call i8* asm "movq %fs:0, $0;\0Aaddq $$-10928, $0", "=r,~{dirflag},~{fpsr},~{flags}"() #1
  ret i64 8
}


请注意,f()仅由ret i64 8组成,该0x8返回offsetof(ABC,:b),即的值。

这是描述@pure表示法的链接:https://github.com/JuliaLang/julia/issues/414(另请参见:https://github.com/JuliaLang/julia/issues/14324

请注意:纯符号可能会不断变化,并且可能在版本之间变化。它在0.6中起作用。将来,我们可以使用更智能的编译器(不断进行传播)来更轻松地实现此优化。

关于optimization - 将offsetof()优化到恒定值?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44668696/

10-12 20:39