Skip to content

Fix large file memory explosion (Issue #457)#498

Closed
Lcstyle wants to merge 10 commits intopop-os:masterfrom
Lcstyle:feature/integrated-enhancements
Closed

Fix large file memory explosion (Issue #457)#498
Lcstyle wants to merge 10 commits intopop-os:masterfrom
Lcstyle:feature/integrated-enhancements

Conversation

@Lcstyle
Copy link
Copy Markdown

@Lcstyle Lcstyle commented Jan 23, 2026

Summary

Root Cause Analysis

Found that cosmic-text's shape_until_scroll() uses f32::INFINITY as scroll_end when buffer height isn't set. This causes ALL lines to be shaped on file load, before the window even renders.

Each character requires ~200 bytes of glyph/layout data:

  • ShapeGlyph: ~100 bytes (positions, font info, metrics, colors)
  • LayoutGlyph: ~100 bytes (positions, font info, bidi level, etc.)

Result: 60MB file × 313x multiplier = 18.8GB RAM → OOM crash on systems with <16GB RAM

Fix

Set a minimal buffer height (100px) before loading files >1MB. This limits initial shaping to ~5 visible lines. The proper height is set during rendering, and additional lines are shaped on-demand as user scrolls.

Results

File Size Before Fix After Fix Improvement
60MB (8M lines) 18.8 GB 0.2 GB 94x reduction

Test Plan

  • Open 60MB text file - no crash, ~200MB RAM usage
  • Scroll through large file - responsive, lines shaped on demand
  • Open small files (<1MB) - unchanged behavior
  • Session restore works
  • Auto-save works
  • Save As to existing file switches to that tab

🤖 Generated with Claude Code

LinuxBoy-96 and others added 10 commits December 27, 2025 19:21
Root cause: cosmic-text's shape_until_scroll() uses INFINITY as scroll_end
when buffer height is not set, causing ALL lines to be shaped on file load.
Each character requires ~200 bytes of glyph/layout data, so a 60MB file
was consuming 18.8GB RAM (313x multiplier).

Fix: Set a minimal buffer height (100px) before loading files >1MB.
This limits initial shaping to ~5 visible lines instead of all lines.
The proper height is set during rendering, and additional lines are
shaped on-demand as user scrolls.

Results:
- Before: 60MB file -> 18.8GB RAM (crashes on 8-16GB systems)
- After:  60MB file -> 0.2GB RAM (94x memory reduction)

Also includes PR pop-os#479 Save As fix:
- Added resolve_path() helper for canonical path resolution
- Added set_path() and save_as() methods to EditorTab
- SaveAsResult now checks if file is already open in another tab

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
For files larger than the configurable threshold (default 5MB), skip
the FNV-1a hash computation which iterates over every byte. This
eliminates startup lag for large files.

Features disabled for large files:
- Undo-to-saved detection (check_and_reset_if_unchanged)
- Backup content change detection

New config option: max_hash_file_size_mb
- Default: 5 MB
- Set to 0 to always compute hash (not recommended for large files)
- Configurable per-user in cosmic-edit settings

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@mmstick
Copy link
Copy Markdown
Member

mmstick commented Jan 23, 2026

You need to rebase this on pop-os/master. There are a lot of unrelated commits in this pull request that make it impossible to review. I'd recommend creating a clean branch and cherry-picking 31a36bf by itself. Perhaps 2963083 should be in a separate PR so they can be reviewed separately.

@Lcstyle
Copy link
Copy Markdown
Author

Lcstyle commented Jan 23, 2026

Closing in favor of cleaner, separate PRs:

This addresses the review feedback about having too many unrelated commits in one PR.

@Lcstyle Lcstyle closed this Jan 23, 2026
@Lcstyle
Copy link
Copy Markdown
Author

Lcstyle commented Jan 23, 2026

@mmstick it might be a little messy but I am running the updated build locally, loads 200mb instantly, scroll bar works, no more OOM crashes.. I'm not a RUST developer so please have mercy on the code, claude did generate it but the design was mostly mine. BTW Cosmic Desktop is my new primary OS so i need to make this work and make it better everyday. I am running cosmic on Fedora43, thanks for all your hard work so far, it's beautiful and very very fast.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Issue: crashes when opening large files

4 participants