Repository navigation
perf(web): 值位置的长字面量也不再构造临时 std::string - #28
Merged
Merged
Conversation
d53d113 把头名那一侧的 `const char*` 重载补齐了,但值那一侧没有 —— 而 `uvcpp_http_common.h` 里那段注释写着"值位置一个都没有",这句是错的。 长字面量确实落在值位置,只是不在 `set_header` 的调用点上,而在 `uvcpp_web_response::body()` 的 `ct` 形参上: text() -> body(s, "text/plain; charset=utf-8") 24 字符 html() -> body(s, "text/html; charset=utf-8") 23 json() -> body(..., "application/json; charset=utf-8") 31 json_str() -> 同上 31 `ct` 收 `const std::string&` ⇒ `resp.text("ok")` 先建一个临时串(第 1 次 分配),再被 push_back 拷进 http_header.value(第 2 次)。**benchmark 自己 跑的 /text 与 /json 两条路由走的就是这里。** 这一版让两个成员各自从 C 串 就地构造再移动进向量 ⇒ 两次降到一次(留下的那次是头表必须持有的值)。 本笔同时把那句已被证伪的注释改对,并给作者的门禁用例补上值侧判据 (改前形状 2 → 新形状 1;覆写 1 → 0)。拿掉新重载,那两条会红,对照组不动。 判据是分配计数(整数、无轮间散布):webapp 每请求 20.10 -> 19.08, http 10.92 -> 10.07(12 s 窗口,除以 1.25 归一)。**µs/rps 那侧分辨不出**: 1 次分配约 26~50 ns,而同一轮的交错 A/B 里 c2 与 c3 的散布地板是 0.13 µs —— 差值落在散布内,所以这笔不主张"更快",只主张"少一次分配"。 套件:全量构建后 96/96 通过。
`"text/plain; charset=utf-8"` 是 **25** 字符、`"text/html; charset=utf-8"` 是
**24**;我上一版在三处注释里把它们报成 24 / 23(`json()` 那 31 是对的)。
**代码与判据都没错。** 那两条 `static_assert` 判的是判据值 `kLongValue`(31,
算出来的),所以门禁一直是准的 —— 错的只有注释里报出来的数。
顺手把这几个长度也钉进编译期:上面那两条 `static_assert` 对注释里报的数一个都
管不着,而「印出来的数与算出来的数对不上」是这个仓库里已经出现过两次的错法。
static_assert(sizeof("text/plain; charset=utf-8") - 1 == 25u, ...);
static_assert(sizeof("text/html; charset=utf-8") - 1 == 24u, ...);
static_assert(sizeof("application/json; charset=utf-8") - 1 == 31u, ...);
它们只保证**注释里的数**不写错,不保证库里的 `ct` 字面量没被换成别的(真换了
两边一起变,这里不会红)—— 那是行为用例的事,注释里写明了。
门禁重跑:15/15 PASS,rc=0。判据一条没动,只改了注释、加了三条编译期前提。
`check_doc_lines.py` 判据 3 锁的是**内容哈希**。本 PR 在
`src/web/uvcpp_http_response.h`(+13 行)与 `src/webapp/uvcpp_web_response.cpp`
(+36 行)里插了行,于是文档里两条 `文件:行号` 引用指到了别人的内容上 ——
`basic` 作业(ubuntu / macos 两腿)就是这么红的。
改法**不是**直接 `--update`:那等于让引用静默指错。按脚本提示"先人工核一遍
引用还对不对",两条都核过:
- `doc/web-http-guide.md:229` 那一句说的就是 `static from_parser(...)`,
它被本 PR 从 `:155` 顶到 `uvcpp_http_response.h:168`(旧的 :155 现在是
`make()` 的形参行)⇒ 155 → 168。
- `doc/webapp-support-guide.md:314` 引的是「`status` 是 libuv 码、响应层
自己会翻」那段,整体下移 36 行 ⇒ 1164-1184 → 1200-1220(与 master 的
1200-1220 逐字节比对过,同 21 行)。
**验证**:刷完锁与 master 的锁逐行比 —— 只有这两条的行号变了,
**哈希两端逐字节相同**(`97e67d4e228ea020` / `c14d42880a33c69a`),
即引用指的还是同一段内容;其余 670 行一字未动(669 条目标区间)。
Antruly
added a commit
that referenced
this pull request
Sep 21, 2026
PR #28 那三笔把值位置的 `const char*` 重载补齐了,属于功能推送 ⇒ 按约定升一档 patch 开发版。README 两版与版本头同步到 1.2.17-dev,`IS_RELEASE` 保持 0。 Co-Authored-By: Claude Code <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
承接 d53d113 那笔:头名那一侧的
const char*重载补齐了,值那一侧没有 —— 而那段注释里写着「值位置一个都没有」,这句不对。长字面量确实落在值位置
只是不在
set_header的调用点上,而在uvcpp_web_response::body()的ct形参上:ct的字面量text()"text/plain; charset=utf-8"html()"text/html; charset=utf-8"json()/json_str()"application/json; charset=utf-8"ct收const std::string&⇒resp.text("ok")先建一个临时串(第 1 次分配),再被push_back拷进http_header.value(第 2 次)。bench/bench_server.cpp自己的/text与/json走的就是这条路。这笔做了什么
(const char*, const char*):两个成员各自从 C 串就地构造再移动进向量 ⇒ 两次分配降到一次(留下的那次是头表必须持有的值,省不掉)。tests/unit/http_name_alloc_unit.cpp补上值侧判据。判据(分配计数)
整数、无轮间散布 —— 端到端分不出的量,在这里是可以逐次核对的事实。12 s 窗口,读数按 1.25 归一(快照含 3 s 热身):
值侧判据加在作者那份用例里,形态与名字侧故意不同:值的字节必须存下来,所以"插入"是恰好 1(名字侧才是 0),改前形状是 2;覆写 1 → 0。
把新重载拿掉,那两条变红(1→2、0→1)、两条对照不动 —— 牙是准的。判据的两个前提用
static_assert钉在编译期(值必须超过三库里最大的 SSO 上限 22;覆写对照的初值必须比判据值宽),前提不成立是编译红,不必等哪条腿跑出个说不清的结果。时序那侧:这笔不主张更快
同轮交错 A/B(hical / c2=作者 / c3=带本笔,4 轮,conc=100,10 s,非 2xx 全 0):
c2-c3 = +0.0566 µs,散布上限 0.3511 ⇒ 不可分辨+0.0094 µs,散布上限 0.1289 ⇒ 不可分辨-150,散布上限 2028 ⇒ 不可分辨1 次分配约 26~50 ns,低于这台机器的分辨能力。所以这笔的理由是"少一次分配 + API 两侧对称",不是"更快"。
测试
全量构建后 96/96 通过(含这笔补的值侧判据)。