Announcing go-lua

Today, we’re excited to release go-lua as an Open Source project. Go-lua is an implementation of the Lua programming language written purely in Go. We use go-lua as the core execution engine of our load generation tool. This post outlines its creation, provides examples, and describes some challenges encountered along the way.

Francis Bogsanyi
8 min readintermediate
--
View Original

Overview

Shopify has released go-lua, an Open Source implementation of the Lua programming language written in Go. This article discusses the motivation behind its development, the challenges faced during its creation, and provides examples of its application in load generation tools.

What You'll Learn

1

How to implement a Lua interpreter using Go

2

Why using Go for scripting languages can improve performance

3

How to leverage Lua's features for load generation

Prerequisites & Requirements

  • Familiarity with Lua programming language
  • Understanding of Go programming language
  • Experience with concurrent programming(optional)

Key Questions Answered

What motivated Shopify to create go-lua?
Shopify needed a scalable load generation tool that could handle high concurrency and allow for dynamic flow scripting without requiring tool redeployment. The existing Ruby-based tools could not meet these demands, prompting the shift to Go and the development of go-lua.
How does go-lua compare to existing Lua implementations?
Go-lua is designed to be a pure Go implementation of Lua, avoiding the complexities of binding to C implementations. This allows for better memory management and performance optimizations, although it may not match the speed of C-based Lua interpreters in all scenarios.
What are the performance benchmarks for go-lua?
When computing fib(35), go-lua is approximately 6x slower than the C Lua interpreter. Gopher-lua is about 20% faster than go-lua, primarily due to its simpler stack model and reduced overhead from debug hooks.
What limitations exist in go-lua's implementation?
Go-lua aims for compatibility with Lua 5.2 but lacks support for coroutines, string pattern matching, and weak reference tables. While most core libraries are implemented, these notable exceptions limit its functionality.

Key Statistics & Figures

Performance comparison of Fibonacci function
6x slower than C Lua interpreter
When computing fib(35
Performance comparison with Gopher-lua
20% faster than go-lua
Gopher-lua's performance advantage is attributed to its simpler stack model.

Technologies & Tools

Some links below are affiliate links. We may earn a commission if you make a purchase.

Backend
Go
Used to implement the go-lua interpreter.
Scripting
Lua
The programming language that go-lua interprets.

Key Actionable Insights

1
Consider using go-lua for load generation tasks that require high concurrency and dynamic scripting capabilities.
This approach allows for real-time adjustments to test flows without redeploying the tool, significantly enhancing flexibility during testing.
2
Leverage Go's garbage collector when implementing embedded languages to improve memory management.
Using Go's heap for Lua objects can lead to performance benefits, such as zero-overhead sharing of strings between languages.
3
Implement function table dispatch in Go to optimize performance in bytecode interpretation.
This method has shown to provide significant performance improvements over traditional switch-case implementations in Go.

Common Pitfalls

1
Relying on C bindings for Lua can lead to performance issues due to incompatible concurrency mechanisms.
Using a pure Go implementation like go-lua avoids these issues, allowing for better memory management and performance.

Related Concepts

Lua Programming
Go Programming
Load Generation Techniques