From d0d654179e921b91fe2cfb9a47b0215202076a36 Mon Sep 17 00:00:00 2001 From: cflmy Date: Mon, 30 Nov 2026 12:22:44 +0800 Subject: [PATCH] fix: allocate HTTP byte buffers by actual size and build headers once MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fix marqdo-ext-http#13: `http.文本字节` allocated a fixed 8 MiB Memory region on every call, and `http.构建请求字节` called it once for the request line and once per header, so a 6-header request spent ~56 MiB of the default 64 MiB run budget and a second request failed with `resource.exhausted`. - `文本字节`: request the minimal page count for the actual text (4-byte UTF-8 worst case per codepoint, 128-page cap, 1 page minimum). - `构建请求字节`: concatenate the request line and all headers into one text, encode once, then concatenate the body; per-header fixed allocations are gone. - Public signatures and produced request bytes are unchanged (`构建请求` text API stays compatible). - Regression case: under the default budget, build the same 6-header request 8 times and assert the final byte length/request text; the old code failed on the second build. - docs/evidence records the real CLI/Bundle run. No CLI/Host change, no new Host request, no release; real loopback multi-request exchange (#3) and the optional streaming response view remain open/UNVERIFIED. --- docs/evidence-2026-11-30-byte-budget.md | 40 +++++++++++++++++++++++++ index.mq.md | 30 ++++++++++++++----- tests/cases/http.json | 12 ++++++++ tests/http.mq.md | 22 +++++++++++++- 4 files changed, 96 insertions(+), 8 deletions(-) create mode 100644 docs/evidence-2026-11-30-byte-budget.md diff --git a/docs/evidence-2026-11-30-byte-budget.md b/docs/evidence-2026-11-30-byte-budget.md new file mode 100644 index 0000000..5773c47 --- /dev/null +++ b/docs/evidence-2026-11-30-byte-budget.md @@ -0,0 +1,40 @@ +# HTTP 字节编码预算修正 — 2026-11-30 + +Issue: [marqdo-ext-http#13](https://git.cflmy.cn/cflmy/marqdo-ext-http/issues/13) +(consumer: `marqdo-ext-ai` AI-U2). Branch `codex/issue-13-byte-budget`. + +## 根因与修复 + +- `http.文本字节` 每次调用固定 `memory.new(128,128)`(8 MiB),而运行时 region + 在 run 内不回收;`构建请求字节` 对请求行与每个头分别调用,6 头请求约 7 次 + 8 MiB 分配,默认 64 MiB 预算下第二次请求即 `resource.exhausted`。 +- 修复: + - `文本字节` 按实际文本码点数申请最小页数(UTF-8 上界 4 字节/码点,上限 + 128 页 = 8 MiB),空文本 1 页; + - `构建请求字节` 先把请求行 + 所有头拼成一个文本,再单次编码,最后 concat + 正文,避免每头一次固定分配; + - `字节文本` 本已按长度计算页数,无需改动; + - 公开函数签名与请求字节逐字节结果不变(`构建请求` 文本 API 兼容)。 + +## 验证(真实执行) + +环境:Linux x86_64 / glibc 2.43;CLI `marqdo 3.0.0-rc.6`(candidate +`0f87f856…`);Bundle `marqdo-stdlib 0.2.0`。 + +```text +$ MARQDO_BIN= MARQDO_STDLIB_DIR=<0.2.0 Bundle> sh scripts/test.sh +PASS http +1 cases passed +``` + +新增回归用例(`tests/http.mq.md` + `tests/cases/http.json`):在默认内存预算下, +用 6 个头连续构造 8 次请求,末次字节长度 169,末次请求文本逐字节符合 HTTP/1.1 +(`POST /v1/chat ... Accept: application/json\r\n\r\n{}`)。修复前该循环在第二次 +构造即触及 64 MiB 预算(问题单真实探针)。 + +## 未验证 / 边界 + +- 真实 127.0.0.1 loopback 多请求闭环(#3)未在本环境执行;本用例验证的是 + 报文构造的预算与字节正确性,不是网络交换。 +- 未改 CLI/Host/其他组件;未新增 Host 请求;未创建 release/tag。 +- 响应体的分块/大响应线性处理(问题单可选第 3 项)未实现,保持 open。 diff --git a/index.mq.md b/index.mq.md index 7f80a04..8b6ecf8 100644 --- a/index.mq.md +++ b/index.mq.md @@ -138,9 +138,24 @@ import: `params(文本)` -把 UTF-8 文本编码为字节列表,上限 8 MiB。 +把 UTF-8 文本编码为字节列表:按实际文本长度申请最小 Memory 页数(UTF-8 +最多 4 字节/码点),上限 128 页(8 MiB)。不再每次固定申请 8 MiB,使同一 +程序内的多次报文构造共享默认 64 MiB 预算。 -**内存 = memory.new(128, 128)** +**点 = text.codepoints(文本)** + +**上界 = 长度(点) * 4** + +**页 = (上界 + 65535) // 65536** + +`if 页 < 1` +**页 = 1** +`end` +`if 页 > 128` +**页 = 128** +`end` + +**内存 = memory.new(页, 页)** **字节数 = utf8.编码(文本, 内存, 0)** @@ -399,9 +414,9 @@ import: `params(方法, 路径, 头列表, 体字节)` 构建 HTTP/1.1 请求字节。方法、路径、头名/值按 UTF-8 编码;正文使用 -传入的字节列表。 +传入的字节列表。头部先拼成一个文本再编码一次,避免每个头一次固定内存分配。 -**结果 = 文本字节(方法 + " " + 路径 + " HTTP/1.1" + "\r\n")** +**头部 = 方法 + " " + 路径 + " HTTP/1.1" + "\r\n"** **下标 = 0** @@ -409,14 +424,15 @@ import: **头 = 头列表[下标]** -**结果 = list.concat(结果, 文本字节(文本(头[0]) + ": " + 文本(头[1]) + "\r\n"))** +**头部 = 头部 + 文本(头[0]) + ": " + 文本(头[1]) + "\r\n"** **下标 = 下标 + 1** `end` -**结果 = list.concat(结果, [13, 10])** -`return list.concat(结果, 体字节)` +**头部字节 = 文本字节(头部 + "\r\n")** + +`return list.concat(头部字节, 体字节)` ## 构建请求 diff --git a/tests/cases/http.json b/tests/cases/http.json index 89a4b99..0db011b 100644 --- a/tests/cases/http.json +++ b/tests/cases/http.json @@ -41,6 +41,18 @@ { "type": "text", "value": "POST / HTTP/1.1\r\n\r\nAB" + }, + { + "type": "int", + "decimal": "8" + }, + { + "type": "int", + "decimal": "169" + }, + { + "type": "text", + "value": "POST /v1/chat HTTP/1.1\r\nHost: example.test\r\nAuthorization: Bearer x\r\nContent-Type: application/json\r\nContent-Length: 2\r\nConnection: close\r\nAccept: application/json\r\n\r\n{}" } ], "type": "list" diff --git a/tests/http.mq.md b/tests/http.mq.md index 8eb5459..ab4ffdd 100644 --- a/tests/http.mq.md +++ b/tests/http.mq.md @@ -21,4 +21,24 @@ import: **请求文本 = http.字节文本(请求字节)** -`return [响应.状态, 响应.原因, 响应.头["Content-Type"], 响应.体, 请求, 响应字节.value.status, 正文.value, 请求文本.value]` +**头列表 = [["Host", "example.test"], ["Authorization", "Bearer x"], ["Content-Type", "application/json"], ["Content-Length", "2"], ["Connection", "close"], ["Accept", "application/json"]]** + +**次数 = 0** + +**末长度 = 0** + +**末文本 = ""** + +`while 次数 < 8` + +**请求字节 = http.构建请求字节("POST", "/v1/chat", 头列表, [123, 125])** + +**末长度 = 长度(请求字节)** + +**末文本 = http.字节文本(请求字节).value** + +**次数 = 次数 + 1** + +`end` + +`return [响应.状态, 响应.原因, 响应.头["Content-Type"], 响应.体, 请求, 响应字节.value.status, 正文.value, 请求文本.value, 次数, 末长度, 末文本]` -- 2.54.1