perf(crafter): avoid redundant SHA256 pass on every CAS upload#3142
perf(crafter): avoid redundant SHA256 pass on every CAS upload#3142matiasinsaurralde wants to merge 1 commit into
Conversation
…o CAS uploadAndCraft already computes the artifact's SHA256 via fileStats, then called Uploader.UploadFile which re-opened the file and re-hashed it before streaming. Switch to Uploader.Upload with the reader and digest from fileStats so the file is opened once and hashed once per material. Also fix a file-handle leak in fileStats when SHA256 or Seek fails. Chainloop-Trace-Sessions: 70c6c792-4598-426b-9e75-482bcb10498f Signed-off-by: Matías Insaurralde <matias@chainloop.dev>
AI Session Analysis
|
| Status | Policy | Material | Messages |
|---|---|---|---|
| ✅ Passed | ai-config-ai-agents-allowed |
ai-coding-session-70c6c7 |
- |
| ✅ Passed | ai-config-no-dangerous-commands |
ai-coding-session-70c6c7 |
- |
| ✅ Passed | ai-config-no-secrets |
ai-coding-session-70c6c7 |
- |
| ✅ Passed | ai-config-mcp-servers-allowed |
ai-coding-session-70c6c7 |
- |
Powered by Chainloop and Chainloop Trace
Summary
When uploading a material to a CAS backend, the file's SHA256 was being computed twice and the file opened three times:
fileStatsopens the file, computes SHA256, rewinds — producing an*os.Filethat the caller holds.uploadAndCraftthen calledUploader.UploadFile, which re-opened the file, recomputed SHA256, rewound, then streamed it.The reader from step 1 was already in the right state to be streamed. This change calls
Uploader.Uploadwith that reader and the digest already produced byfileStats, eliminating the redundant open + hash pass while leaving the CAS protocol and on-the-wire bytes unchanged.Per material, local work drops from
3 reads + 2 SHA256 passesto2 reads + 1 SHA256 pass. Savings scale linearly with material size.Also closes a file-handle leak in
fileStatswhen SHA256 or Seek returns an error.Uploader.UploadFileis retained — the CLI'sartifact uploadcommand still uses it.