| 
                        副标题[/!--empirenews.page--]
                           
在 GitHub 玩耍时,偶然发现了 gopher-lua ,这是一个纯 Golang 实现的 Lua 虚拟机。我们知道 Golang 是静态语言,而 Lua 是动态语言,Golang 的性能和效率各语言中表现得非常不错,但在动态能力上,肯定是无法与 Lua 相比。那么如果我们能够将二者结合起来,就能综合二者各自的长处了(手动滑稽。 
在项目 Wiki 中,我们可以知道 gopher-lua 的执行效率和性能仅比 C 实现的 bindings 差。因此从性能方面考虑,这应该是一款非常不错的虚拟机方案。 
Hello World 
这里给出了一个简单的 Hello World 程序。我们先是新建了一个虚拟机,随后对其进行了 DoString(...) 解释执行 lua 代码的操作,最后将虚拟机关闭。执行程序,我们将在命令行看到 "Hello World" 的字符串。 
- package main  
 - import (  
 -     "github.com/yuin/gopher-lua"  
 - )  
 - func main() {  
 -     l := lua.NewState()  
 -     defer l.Close()  
 -     if err := l.DoString(`print("Hello World")`); err != nil {  
 -         panic(err)  
 -     }  
 - }  
 - // Hello World 
 
  
提前编译 
在查看上述 DoString(...) 方法的调用链后,我们发现每执行一次 DoString(...) 或 DoFile(...) ,都会各执行一次 parse 和 compile 。 
- func (ls *LState) DoString(source string) error {  
 -     if fn, err := ls.LoadString(source); err != nil {  
 -         return err  
 -     } else {  
 -         ls.Push(fn)  
 -         return ls.PCall(0, MultRet, nil)  
 -     }  
 - }  
 - func (ls *LState) LoadString(source string) (*LFunction, error) {  
 -     return ls.Load(strings.NewReader(source), "<string>")  
 - }  
 - func (ls *LState) Load(reader io.Reader, name string) (*LFunction, error) {  
 -     chunk, err := parse.Parse(reader, name)  
 -     // ...  
 -     proto, err := Compile(chunk, name)  
 -     // ...  
 - } 
 
  
从这一点考虑,在同份 Lua 代码将被执行多次(如在 http server 中,每次请求将执行相同 Lua 代码)的场景下,如果我们能够对代码进行提前编译,那么应该能够减少 parse 和 compile 的开销(如果这属于 hotpath 代码)。根据 Benchmark 结果,提前编译确实能够减少不必要的开销。 
- package glua_test  
 - import (  
 -     "bufio"  
 -     "os"  
 -     "strings"  
 -     lua "github.com/yuin/gopher-lua"  
 -     "github.com/yuin/gopher-lua/parse"  
 - )  
 - // 编译 lua 代码字段  
 - func CompileString(source string) (*lua.FunctionProto, error) {  
 -     reader := strings.NewReader(source)  
 -     chunk, err := parse.Parse(reader, source)  
 -     if err != nil {  
 -         return nil, err  
 -     }  
 -     proto, err := lua.Compile(chunk, source)  
 -     if err != nil {  
 -         return nil, err  
 -     }  
 -     return proto, nil  
 - }  
 - // 编译 lua 代码文件  
 - func CompileFile(filePath string) (*lua.FunctionProto, error) {  
 -     file, err := os.Open(filePath)  
 -     defer file.Close()  
 -     if err != nil {  
 -         return nil, err  
 -     }  
 -     reader := bufio.NewReader(file)  
 -     chunk, err := parse.Parse(reader, filePath)  
 -     if err != nil {  
 -         return nil, err  
 -     }  
 -     proto, err := lua.Compile(chunk, filePath)  
 -     if err != nil {  
 -         return nil, err  
 -     }  
 -     return proto, nil  
 - }  
 - func BenchmarkRunWithoutPreCompiling(b *testing.B) {  
 -     l := lua.NewState()  
 -     for i := 0; i < b.N; i++ {  
 -         _ = l.DoString(`a = 1 + 1`)  
 -     }  
 -     l.Close()  
 - }  
 - func BenchmarkRunWithPreCompiling(b *testing.B) {  
 -     l := lua.NewState()  
 -     proto, _ := CompileString(`a = 1 + 1`)  
 -     lfunc := l.NewFunctionFromProto(proto)  
 -     for i := 0; i < b.N; i++ {  
 -         l.Push(lfunc) 
 -          _ = l.PCall(0, lua.MultRet, nil)  
 -     }  
 -     l.Close()  
 - }  
 - // goos: darwin  
 - // goarch: amd64  
 - // pkg: glua  
 - // BenchmarkRunWithoutPreCompiling-8         100000             19392 ns/op           85626 B/op         67 allocs/op  
 - // BenchmarkRunWithPreCompiling-8           1000000              1162 ns/op            2752 B/op          8 allocs/op  
 - // PASS  
 - // ok      glua    3.328s 
 
  
虚拟机实例池 
在同份 Lua 代码被执行的场景下,除了可使用提前编译优化性能外,我们还可以引入虚拟机实例池。 
                                                (编辑:52站长网) 
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! 
                     |