Performance Issue: Slow Table Iteration

未關閉
#136 0 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

@efegokdemir 已經在處理了。

開始於 2026年11月1日。

  • #152 來自 @efegokdemir —— 未關閉

評估

難度
4/5
預估耗時
3-5 天
新手友好度
35/100
Issue 類型
缺陷
描述清晰度
基本清楚
活躍度
停滯
技術堆疊
go, lua
領域
performance

研究方向

Start by running the perf_test.lua script from the issue through the lua_executor.go driver and timing the pairs() loop separately from the insertion loop. The payload does not name the table or iteration source files, so find where pairs and next are implemented in the VM's table code. Done means the 50,000-key iteration takes time comparable to the insertion phase, and you can show that with a before-and-after timing.

由索引模型根據 Issue 內容生成。

描述

A significant performance degradation has been observed when iterating over large tables in Gopher-lua compared to the standard Lua implementation. The iteration time for a table with 50,000 entries is approximately 10,000 times slower in Gopher-lua.

Environment for first test

  • Operating System: MacOs
  • Gopher-lua version: v0.0.0-20240527182111-9ab1540f3f5f
  • Go version: go version go1.22.3 darwin/arm64
  • Standard Lua version (for comparison): Lua 5.4.7 Copyright (C) 1994-2024 Lua.org, PUC-Rio

Environment for last test

  • Operating System: Linuc
  • CPU: Intel(R) Atom(TM) CPU C2338 @ 1.74GHz
  • Standard Lua version (for comparison): Lua 5.1.5 Copyright (C) 1994-2012 Lua.org, PUC-Rio

Steps to Reproduce

  1. Create a Lua script (perf_test.lua) with the following content:
local function get_time()
    return os.clock()
end

local table = {}
local start_time = get_time()

for i = 100000000, 100049999 do
    local key = tostring(i) .. "_MySuperKey"
    table[key] = i
end

local insertion_time = get_time() - start_time
print(string.format("Insertion time: %.6f seconds", insertion_time))

local count = 0
start_time = get_time()

for _ in pairs(table) do
    count = count + 1
end

local iteration_time = get_time() - start_time
print(string.format("Iteration time: %.6f seconds", iteration_time))
print(string.format("Count: %d", count))
  1. Run the script using both the standard Lua interpreter and Gopher-lua.

There is the go code to execute the script lua_executor.go:

package main

import "bytes"
import "fmt"
import "io/ioutil"
import "os"

import "github.com/Shopify/go-lua"

func main() {
	if len(os.Args) < 2 {
		fmt.Println("Usage: go run lua_executor.go <path_to_lua_script>")
		os.Exit(1)
	}

	scriptPath := os.Args[1]
	scriptContent, err := ioutil.ReadFile(scriptPath)
	if err != nil {
		fmt.Printf("Error reading script file: %v\n", err)
		os.Exit(1)
	}

	L := lua.NewState()
	lua.OpenLibraries(L)

	err = L.Load(bytes.NewReader(scriptContent), scriptPath, "t")
	if err != nil {
		fmt.Printf("Error executing Lua script: %v\n", err)
		os.Exit(1)
	}

	L.ProtectedCall(0, 0, 0)
}

Observed Results

Gopher-lua
Insertion time: 0.069506 seconds
Iteration time: 32.081409 seconds
Count: 50000
Standard Lua 5.4
Insertion time: 0.047063 seconds
Iteration time: 0.003339 seconds
Count: 50000
Standard Lua 5.1
Insertion time: 0.294900 seconds
Iteration time: 0.016081 seconds
Count: 50000

Expected Results

The iteration time for Gopher-lua should be comparable to that of standard Lua, allowing for some performance differences due to the Go implementation. However, a difference of this magnitude (32 seconds vs 0.003 seconds) suggests a significant issue.

Additional Information

  • The insertion time is relatively comparable between the two implementations.
  • The iteration time in Gopher-lua is approximately 9,608 times slower than in standard Lua for this test case.
  • This performance issue could significantly impact applications that rely on frequent iterations over large tables.

Possible Causes

The extreme slowdown during iteration might be due to:

  1. Inefficient implementation of the pairs() function in Gopher-lua.
  2. Suboptimal internal representation of tables in Gopher-lua.
  3. Possible memory management issues during iteration.

Impact

This performance issue severely limits the usability of lua Go stack for applications that require frequent iterations over large tables, which is a common operation in many Lua scripts.

We would greatly appreciate your investigation into this matter and any insights you could provide on potential optimizations or workarounds.

主要語言
Go
星號
3.5k
分支
217
PR 合併指標
30 天內沒有已合併 PR

環境準備

這個專案沒有提供開發容器、Dockerfile 或貢獻指南,環境需要你自己搭建:先看它的 README,通用步驟見我們的新手貢獻指南。

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

Shopify/go-lua 的其他 Issue

查看 Shopify/go-lua 的全部 Issue

相似的 Issue

更多 Go Issue

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。