Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Format/size detection performance improvements #247

Merged
merged 6 commits into from
Dec 5, 2024

Conversation

b0o
Copy link
Contributor

@b0o b0o commented Nov 23, 2024

Adds fast, lua-based format detection and dimension reading for common image formats, falling back to ImageMagick when needed. This avoids spawning external processes for basic operations, making things noticeably snappier, especially when using image.nvim's API from other plugins (my use-case).

  • Extended built-in format detection using magic numbers instead of relying on ImageMagick
  • Direct header parsing for image dimensions (PNG, JPEG, WebP, GIF, BMP, ICO, HEIC, AVIF)
  • Added test suite with sample images and edge cases
  • Set up basic GitHub Actions CI for running tests

The lua implementations are significantly faster but slightly less forgiving with malformed files compared to ImageMagick. We fall back to the existing methods when lua-based parsing fails, so everything should be backward compatible. More test images might be beneficial, especially for heic/avif formats.

Benchmarking the time to run from_image:

  • With magick_rock:
    • For a 2880 x 1800 PNG file:
      • Before: 283.36ms
      • After: 0.50ms
    • For a 208 x 209 PNG file:
      • Before: 4.96ms
      • After: 0.36ms
  • With magick_cli:
    • For a 2880 x 1800 PNG file:
      • Before: 294.32ms
      • After: 0.48ms
    • For a 208 x 209 PNG file:
      • Before: 32.82ms
      • After: 0.16ms

Similar speedups for other formats.

Note: most of the logic in dimensions.lua was generated with Anthropic's Claude.

@b0o b0o changed the title Format/sized detection performance improvements Format/size detection performance improvements Nov 23, 2024
@3rd 3rd merged commit a45e2cd into 3rd:master Dec 5, 2024
2 checks passed
@3rd
Copy link
Owner

3rd commented Dec 5, 2024

Absolutely amazing effort, thank you so much on behalf of everyone!

@3rd
Copy link
Owner

3rd commented Jan 6, 2025

Hey @b0o, been having some issues with the tests, have you seen this before? https://github.com/3rd/image.nvim/actions/runs/12633430737/job/35199022999
It's weird that I can't run them on local NixOS either, with working luarocks, busted, etc.
I'm trying hard to like luarocks but as a whole package it's a pretty clunky experience, might replace busted with something else and possibly rip out as many rock-based deps as possible.

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.

2 participants