`unobfuscate()` re-runs `arg_match()` on every node, making large JSON bodies slow

オープン 初心者向け
#925 コメント 1 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
1/5
見積もり時間
1時間未満
初心者へのやさしさ
82/100
issue の種類
リファクタリング
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
r
領域
api, performance

調査の方向性

再帰処理は R/secret.R の unobfuscate() と unobfuscate_rec() にあり、リストの分岐で lapply(x, unobfuscate, ...) を呼び出しているため、ノードごとに arg_match() が再実行されます。issue の再現スクリプトを実行してベースラインの所要時間を取得し、変更の前後で identical() が成り立つことを確認してください。再帰呼び出しが unobfuscate_rec() に向けられ、出力が変わらず、テストスイートが引き続き通ることが完了の条件です。

索引モデルが issue の本文から書いたものです。

説明

unobfuscate() validates handle with arg_match() and then calls unobfuscate_rec(), but unobfuscate_rec() recurses by calling unobfuscate() again (x[] <- lapply(x, unobfuscate, handle = handle)). So arg_match() runs once for every element of the body, and arg_match() is relatively expensive because it looks up the calling frame.

This adds up for large JSON bodies. LLM chat requests made through ellmer are a common case: a request with a few dozen tool definitions plus the conversation history has thousands of nodes, and the cost is paid on every request, including every round of a tool loop. In our app, profiling a single tool-calling turn against a local mock server showed about 60% of the turn's wall time inside unobfuscate_rec() → arg_match().

Recursing into unobfuscate_rec() directly gives the same result and removes most of the overhead:

library(httr2)

# A request body shaped like an LLM chat request with 50 tool definitions
tool <- function(i) list(type = "function", `function` = list(
  name = paste0("tool_", i), description = strrep("x", 200),
  parameters = list(type = "object", properties = setNames(lapply(1:8, function(j)
    list(type = "string", description = "an argument")), paste0("arg", 1:8)),
    required = list("arg1"))))
body <- list(model = "m", messages = list(list(role = "user", content = "hi")), tools = lapply(1:50, tool))

req <- request("https://example.com") |> req_body_json(body)
system.time(for (i in 1:10) httr2:::unobfuscate(body))[["elapsed"]] / 10
#> [1] 0.3935
system.time(for (i in 1:10) req_dry_run(req, quiet = TRUE))[["elapsed"]] / 10
#> [1] 0.5372

# Recursing into unobfuscate_rec() instead of unobfuscate() skips the
# per-node arg_match() and gives the same result
ns <- asNamespace("httr2")
before <- httr2:::unobfuscate(body)
f <- ns$unobfuscate_rec
body(f) <- do.call(substitute, list(body(f), list(unobfuscate = quote(unobfuscate_rec))))
environment(f) <- ns
assignInNamespace("unobfuscate_rec", f, "httr2")
identical(httr2:::unobfuscate(body), before)
#> [1] TRUE
system.time(for (i in 1:10) httr2:::unobfuscate(body))[["elapsed"]] / 10
#> [1] 0.0096
system.time(for (i in 1:10) req_dry_run(req, quiet = TRUE))[["elapsed"]] / 10
#> [1] 0.0778

Suggested change in R/secret.R:

   } else if (is.list(x)) {
-    x[] <- lapply(x, unobfuscate, handle = handle)
+    x[] <- lapply(x, unobfuscate_rec, handle = handle)
     x

Tested with httr2 1.3.0.9000 (main at d08623e), rlang 1.3.0, R 4.4.3. Happy to send a PR if that helps.

主要言語
R
スター
274
フォーク
93
平均マージ
22時間 15分
マージ済み PR(30日)
15

環境構築

このプロジェクトには開発コンテナ、Dockerfile、コントリビューションガイドがありません。まず README を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

r-lib/httr2 のほかの issue

r-lib/httr2 の issue をすべて見る

似ている issue

R の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。