BlogsShopifyLua Runtime in Go

Lua Runtime in Go

Lua Runtime in Go

1
posts
2015

This feature thread tracks the development and enhancement of Shopify's implementation of the Lua programming language in Go, known as go-lua. Initial efforts focused on creating a load generation tool that could simulate heavy traffic on Shopify's platform. To enable writing and executing new flows without full tool redeployments, a lightweight scripting language was needed. Lua was chosen for its simplicity and existing use within Shopify. The decision was made to implement Lua purely in Go (go-lua) rather than binding to the C implementation, offering advantages such as leveraging Go's garbage collector, reduced overhead for execution contexts, and easier mapping of concurrency and error handling semantics. The implementation involved manually porting the Lua 5.2 compiler and virtual machine from C to Go. Challenges encountered included performance optimizations for the interpreter's inner loop, particularly concerning Go's switch statement implementation and function call inlining, leading to the exploration of function table dispatch and careful rewriting of performance-critical code. Benchmarks showed go-lua performing slower than C Lua for certain operations, with differences attributed to debug hooks and call stack implementation. Limitations include the lack of support for coroutines, string pattern matching, string.dump, and weak reference tables.

2015

Announcing go-lua - Shopify

3/4/2015

This post announces the open-sourcing of go-lua, an implementation of the Lua programming language in Go. It details the motivation for building go-lua, which was to provide a scripting engine for Shopify's load generation tool, allowing for dynamic flow definition. The post outlines the technical decision to implement Lua in Go rather than using C bindings, highlighting benefits like garbage collector integration and reduced context creation overhead. It describes the implementation process, involving manual porting of the Lua 5.2 compiler and VM from C to Go. Key challenges discussed include performance optimizations for the interpreter's inner loop, specifically addressing Go's switch statement behavior and function call inlining, leading to the exploration of function table dispatch. The post also presents benchmark results comparing go-lua to C Lua and gopher-lua, and lists current limitations such as the absence of coroutines and weak references.