State.close leaves closed upvalues in the open list, so calls get slower without bound

Đang mở
#153 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

@efegokdemir đang làm issue này rồi.

Từ ngày 7/11/2026.

  • #155 của @efegokdemir — đang mở

Đánh giá

Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức phù hợp với người mới
25/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Đình trệ
Công nghệ
go
Lĩnh vực
backend

Hướng nghiên cứu

Read stack.go and trace State.close alongside findUpValue to understand how the open upvalue list is maintained. The issue includes a reproduction and a proposed fix; verify the list no longer retains closed entries and run the relevant Go tests. A pull request is already open for this issue.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

State.close (stack.go) unlinks closed upvalues from l.upValues, but the loop advances p onto the node it has just unlinked:

for e := l.upValues; e != nil; e, p = e.next, e {
	if e.upValue.isInStackAbove(level) {
		e.upValue.close()
		if p != nil {
			p.next = e.next
		} else {
			l.upValues = e.next
		}
	}
}

When two neighbouring upvalues are closed in the same call, the second unlink writes to the removed node instead of the list. The second upvalue is closed correctly (its value is right), but it stays in the open list forever. Every later findUpValue (on each closure creation) and close walks the longer list, so code that creates closures gets slower the longer a State lives.

Reproduction:

L := lua.NewState()
lua.OpenLibraries(L)
lua.DoString(L, `
local function f()
  local a, b, c = 1, 2, 3
  local g = function() return a + b + c end
  return g()
end
for _ = 1, 100 do f() end`)
// l.upValues now holds 100 dead entries (one per call); it should hold none.

In our use (a planner called a few thousand times per State), the cost per call grew from about 155 µs to about 1.6 ms after 4,000 calls, and profiles were dominated by findUpValue / isInStackAt. With the fix below, the open list stays empty and a long simulation ran 2.2 times faster, with identical results.

Fix: only advance p past nodes that stay in the list.

 	var p *openUpValue
-	for e := l.upValues; e != nil; e, p = e.next, e {
+	for e := l.upValues; e != nil; e = e.next {
 		if e.upValue.isInStackAbove(level) {
 			e.upValue.close()
 			if p != nil {
 				p.next = e.next
 			} else {
 				l.upValues = e.next
 			}
+		} else {
+			p = e
 		}
 	}
Ngôn ngữ chính
Go
Star
3.5k
Fork
217
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Chuẩn bị môi trường

Dự án này không cung cấp dev container, Dockerfile hay hướng dẫn đóng góp, nên bạn cần tự thiết lập môi trường: hãy bắt đầu từ README và xem hướng dẫn đóng góp lần đầu của chúng tôi để biết các bước chung.

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của Shopify/go-lua

Tất cả issue của Shopify/go-lua

Issue tương tự

Thêm issue về Go

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.