Make task context page packing logarithmic
This commit is contained in:
parent
9a48233983
commit
eb9355b003
2 changed files with 130 additions and 27 deletions
|
|
@ -1170,39 +1170,54 @@ class DocForgeService:
|
|||
"pagination": pagination,
|
||||
}
|
||||
|
||||
for kind, item in items[position:]:
|
||||
if consumed >= selected_limit:
|
||||
break
|
||||
destination = page_evidence if kind == "evidence" else page_omissions
|
||||
destination.append(item)
|
||||
consumed += 1
|
||||
candidates = items[position : position + selected_limit]
|
||||
|
||||
def populate(candidate_count: int) -> None:
|
||||
nonlocal consumed, response_limited
|
||||
page_evidence.clear()
|
||||
page_omissions.clear()
|
||||
for kind, item in candidates[:candidate_count]:
|
||||
destination = page_evidence if kind == "evidence" else page_omissions
|
||||
destination.append(item)
|
||||
consumed = candidate_count
|
||||
response_limited = candidate_count < len(candidates)
|
||||
|
||||
def fits(candidate_count: int) -> bool:
|
||||
populate(candidate_count)
|
||||
decorated = {
|
||||
**page_result(),
|
||||
"server_version": SERVER_VERSION,
|
||||
"content_warning": CONTENT_WARNING,
|
||||
"staleness": "current",
|
||||
}
|
||||
if self._encoded_length(decorated) <= maximum:
|
||||
continue
|
||||
destination.pop()
|
||||
consumed -= 1
|
||||
return self._encoded_length(decorated) <= maximum
|
||||
|
||||
lower = 0
|
||||
upper = len(candidates)
|
||||
while lower < upper:
|
||||
midpoint = (lower + upper + 1) // 2
|
||||
if fits(midpoint):
|
||||
lower = midpoint
|
||||
else:
|
||||
upper = midpoint - 1
|
||||
populate(lower)
|
||||
if lower == 0 and candidates:
|
||||
_, item = candidates[0]
|
||||
subject = "unknown"
|
||||
if isinstance(item, Mapping):
|
||||
item_payload = cast(Mapping[str, object], item)
|
||||
candidate = item_payload.get("node_id") or item_payload.get("subject")
|
||||
if isinstance(candidate, str) and candidate:
|
||||
subject = candidate[:256]
|
||||
page_omissions.append(
|
||||
{
|
||||
"code": "response_limit",
|
||||
"subject": subject,
|
||||
"detail_hash": canonical_hash(cast(object, item)),
|
||||
}
|
||||
)
|
||||
consumed = 1
|
||||
response_limited = True
|
||||
if consumed == 0:
|
||||
subject = "unknown"
|
||||
if isinstance(item, Mapping):
|
||||
item_payload = cast(Mapping[str, object], item)
|
||||
candidate = item_payload.get("node_id") or item_payload.get("subject")
|
||||
if isinstance(candidate, str) and candidate:
|
||||
subject = candidate[:256]
|
||||
page_omissions.append(
|
||||
{
|
||||
"code": "response_limit",
|
||||
"subject": subject,
|
||||
"detail_hash": canonical_hash(cast(object, item)),
|
||||
}
|
||||
)
|
||||
consumed = 1
|
||||
break
|
||||
return page_result()
|
||||
|
||||
def _page_context_result(
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue