[seurat] fix SCTransform object bloat on qs2/saveRDS#3
Merged
Conversation
.seurat_sct_fast_vst built its returned `arguments` list via as.list(environment()), capturing local helper fns. The turbo path temporarily swaps sctransform::row_gmean for a cached_row_gmean closure whose enclosing env is the whole fast_SCTransform frame (full umi matrix + CSR intermediates). That list is persisted into SCTModel@arguments, so qs2/saveRDS of the SCT object walks the closure env and drags the matrix in — the saved file balloons ~3x (324MB vs 112MB baseline on a 5k-cell set, scaling with cell count) while object.size stays blind (does not recurse into closure envs). Upstream sctransform stores only scalar params here. Drop function-valued entries from `arguments` before it is persisted. Output unchanged: scale.data bit-identical to pre-fix; qs2 back to baseline size; SCT speedup preserved (4.05x); filter runs once per call (~8us). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
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.
Problem
Community report: "after using autozyme the object saved with qs2 becomes very large." Reproduced — it is SCTransform-specific.
pbmc68k, 5,000-cell subsample,
SCTransform(obj)defaults,qs2::qs_save:object.sizeis ~identical for both arms (215 MB) — it does not recurse into closure environments, butqs2/saveRDSserialize them in full.Root cause
SCTModel@arguments$row_gmean= 212 MB on disk, 0.01 MB by object.size. The turbo SCT path temporarily swapssctransform::row_gmeanfor acached_row_gmeanclosure whose env is the wholefast_SCTransformframe (full UMI matrix)..seurat_sct_fast_vstdoesarguments <- as.list(environment())and strips onlyumi/cell_attr, so the poisoned closure rides intoarguments, which is persisted intoSCTModel@arguments. Grows without bound in cell count. Standard Normalize→Scale→PCA→cluster pipeline is unaffected.Fix
Drop function-valued entries from
argumentsbefore persisting (upstream sctransform stores only scalar params there).Validation
scale.databit-identical to pre-fix (all.equal == TRUE).Follow-up (not in this PR)
Version bump + package rebuild needed for installed users.