Skip to content

Conversation

@priyankeshh
Copy link
Contributor

Closes #351


Overview

This PR optimizes image processing performance and stability by improving the adjust_brightness_contrast function and caching system. The changes resolve memory leaks, CPU bottlenecks, and inefficient cache management, significantly boosting speed and reducing resource consumption.


Original Issues & Performance Bottlenecks

1️⃣ Memory Leaks

  • The image cache grew indefinitely without limits or cleanup, causing high memory usage.
  • Cached images persisted until restart, leading to out-of-memory (OOM) errors.
  • No eviction policy, so unused images stayed in memory indefinitely.

2️⃣ CPU Bottlenecks

  • Inefficient LUT (Lookup Table) calculations slowed down processing.
  • No parallel processing, leading to longer execution times for large images.
  • Redundant pixel calculations caused unnecessary CPU overhead.

3️⃣ Cache Inefficiencies

  • Weak cache key generation led to potential collisions and incorrect image retrieval.
  • No invalidation strategy, so outdated images remained in cache.
  • Unmanaged cache size, allowing excessive memory usage.

Optimizations & Solutions

✅ 1. Memory Leak Fixes

Implemented Cache Size Limits → Prevents cache from exceeding memory constraints.
LRU (Least Recently Used) Eviction Policy → Removes least-accessed images automatically.
Cache Invalidation Mechanism → Removes stale entries using a Time-To-Live (TTL) policy.

✅ 2. CPU Performance Enhancements

Optimized LUT Calculations → Pre-computed LUTs reduce expensive recalculations.
Enabled Parallel Processing → Uses Rayon for multi-threaded execution, significantly boosting speed.
Removed Redundant Pixel Operations → Cuts down unnecessary computations.

✅ 3. Improved Cache Management

More Robust Cache Key Generation → Uses image dimensions, pixel data, and operation parameters to prevent collisions.
Cache Size Management → Actively evicts entries when size limits are exceeded.
Cache Invalidation Strategy → Ensures outdated images are removed automatically.

🚀 Performance:

image

You can check the benchmark results by running:

cd frontend/src-tauri/
cargo run --example benchmark_runner

@Ashutoshx7
Copy link
Contributor

@priyankeshh linting test failed can u make a commit fixing this make sure to have proper structure and spacing

@priyankeshh
Copy link
Contributor Author

@priyankeshh linting test failed can u make a commit fixing this make sure to have proper structure and spacing

image

The linting error is happening due to this core folder which has ai generated code. I can delete that folder and make the commit which will fix it

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.

BUG: Image Processing Performance Degradation in adjust_brightness_contrast (frontend\src-tauri\src\services\mod.rs)

2 participants