Files
lujingze 3bc4be3c06 优化原生结果编码传输与浏览器缓存,记录八路性能基线
原生结果series通过字节索引直传,C端使用Ryu精确回读编码和64 KiB批量写出;网页采用Float64缓存和CSV工作线程,减少结果处理与保存等待。

补充八路AME曲线核查、全流程分阶段计时、独立编码基准和复现工具,固定后续优化采用修正八路及rtol=1e-8。C写出1.1808→0.1638 s,点击到可查看8.0100→6.9756 s。

验证:最终10项编码专项、29项相关后端回归通过;8份原生结果逐位一致,16次网页结果/CSV/刷新恢复通过。前端构建及缓存/CSV专项在本轮结果处理工作中通过。环境、原始大结果与临时构建不纳入Git。
2026-09-11 15:09:15 +00:00

64 lines
3.1 KiB
Python

"""Carry trusted C-generated series JSON without a Python float-array round trip.
Only native output plus its byte index may construct this transport object. Public
JSON still has the ordinary object/array schema; synchronous callers materialize
it explicitly. Bytes own their lifetime independently of the worker directory.
"""
from dataclasses import dataclass
import json
from pathlib import Path
@dataclass(frozen=True)
class NativeSeriesJson:
data: bytes
sample_count: int
def materialize(self) -> dict[str, list[float]]:
return json.loads(self.data)
def read_indexed_result(output: Path, index_path: Path) -> dict:
index = json.loads(index_path.read_bytes())
if not isinstance(index, dict) or type(index.get('version')) is not int or index['version'] != 1:
raise ValueError('Unsupported native result index.')
names = ('seriesStart', 'seriesEnd', 'resultBytes', 'sampleCount')
if any(type(index.get(key)) is not int for key in names):
raise ValueError('Invalid native result index integers.')
start, end, length, count = (index[key] for key in names)
if not (0 < start < end < length and count >= 0):
raise ValueError('Invalid native result index bounds.')
if output.stat().st_size != length:
raise ValueError('Incomplete native result file.')
raw = output.read_bytes()
if (len(raw) != length or not raw[:start].endswith(b'"series":')
or raw[start:start+1] != b'{' or raw[end-1:end] != b'}'
or not raw[end:].startswith(b',"final":')):
raise ValueError('Native result index does not match the output layout.')
# The C writer supplies exact boundaries. No search through strings or numeric
# arrays, and no large json.loads call: only diagnostics/final values are read.
metadata = json.loads(raw[:start] + b'{}' + raw[end:])
if not isinstance(metadata, dict) or metadata.get('series') != {}:
raise ValueError('Invalid native result metadata.')
metadata['series'] = NativeSeriesJson(raw[start:end], count)
return metadata
def serialize_result_parts(payload: dict) -> tuple[bytes, ...]:
"""Return one JSON object in three byte segments, without copying its series.
The only raw position is payload.result.series, produced by our C writer;
all model-provided labels, messages and keys use the standard JSON encoder.
"""
result = payload.get('result')
series = result.get('series') if isinstance(result, dict) else None
if not isinstance(series, NativeSeriesJson):
return (json.dumps(payload, ensure_ascii=False, separators=(',', ':')).encode('utf-8'),)
metadata = {key: value for key, value in result.items() if key != 'series'}
outer = {key: value for key, value in payload.items() if key != 'result'}
encoded = json.dumps(metadata, ensure_ascii=False, separators=(',', ':')).encode('utf-8')
remainder = json.dumps(outer, ensure_ascii=False, separators=(',', ':')).encode('utf-8')
prefix = b'{"result":' + encoded[:-1] + (b',' if metadata else b'') + b'"series":'
suffix = b'}' + (b',' + remainder[1:] if outer else b'}')
return prefix, series.data, suffix