tomaka / hlua

Rust library to interface with Lua
MIT License
510 stars 48 forks source link

Various optimizations #88

Closed tomaka closed 7 years ago

tomaka commented 7 years ago

This PR greatly reduces the number of calls to CString::new and adds a basic example.

Running cargo rustc --release --example basic -- --emit=asm on this example now gives the following assembly (reorganized by me to follow jumps) which is very satisfying:

    movq    $-2, 496(%rbp)
    leaq    _ZN4hlua3Lua3new5alloc17h7ba710ca9ac163c0E(%rip), %rcx
    xorl    %edx, %edx
    callq   lua_newstate
    movq    %rax, %rbx
    testq   %rbx, %rbx
    je  .LBB9_1
    leaq    _ZN4hlua3Lua3new5panic17h36910d3e619d5a53E(%rip), %rdx
    movq    %rbx, %rcx
    callq   lua_atpanic
    movq    %rbx, 440(%rbp)
    movb    $1, 448(%rbp)
    movl    $-1001000, %edx
    movl    $2, %r8d
    movq    %rbx, %rcx
    callq   lua_rawgeti
    leaq    str.c(%rip), %rdx
    movl    $1, %r8d
    movq    %rbx, %rcx
    callq   lua_pushlstring
    movl    $12, %edx
    movq    %rbx, %rcx
    callq   lua_pushinteger
    movl    $-3, %edx
    movq    %rbx, %rcx
    callq   lua_settable
    movl    $-2, %edx
    movq    %rbx, %rcx
    callq   lua_settop
    leaq    str.d(%rip), %rax
    movq    %rax, -80(%rbp)
    movl    $13, %eax
    movd    %rax, %xmm0
    movups  %xmm0, -72(%rbp)
    movq    $0, 72(%rbp)
    movq    $0, 32(%rsp)
    leaq    _ZN4hlua14functions_read6reader17h7178b18bf4af289cE(%rip), %rdx
    leaq    byte_str.4(%rip), %r9
    leaq    -80(%rbp), %r8
    movq    %rbx, %rcx
    callq   lua_load
    movl    %eax, %r14d
    cmpq    $1, 72(%rbp)
    je  .LBB9_59
    testl   %r14d, %r14d
    je  .LBB9_61

.LBB9_61:
    movl    $-1, %edx
    movq    %rbx, %rcx
    callq   lua_pushvalue
    movq    $0, 40(%rsp)
    movl    $0, 32(%rsp)
    xorl    %edx, %edx
    movl    $1, %r8d
    xorl    %r9d, %r9d
    movq    %rbx, %rcx
    callq   lua_pcallk
    movl    %eax, 456(%rbp)
    testl   %eax, %eax
    je  .LBB9_106

.LBB9_106:
    leaq    -80(%rbp), %r8
    movl    $-1, %edx
    movq    %rbx, %rcx
    callq   lua_tointegerx
    movq    %rax, %rdi
    movl    -80(%rbp), %esi
    movl    $-2, %edx
    movq    %rbx, %rcx
    callq   lua_settop

... below is the `assert_eq!` ...