Skip to content

⚡ Cache SharedPreferences in StepCounterService#52

Merged
LeanBitLab merged 1 commit into
mainfrom
performance-optimization-step-counter-caching-12003164693181404246
May 13, 2026
Merged

⚡ Cache SharedPreferences in StepCounterService#52
LeanBitLab merged 1 commit into
mainfrom
performance-optimization-step-counter-caching-12003164693181404246

Conversation

@LeanBitLab
Copy link
Copy Markdown
Owner

Optimized the onSensorChanged method in StepCounterService by caching SharedPreferences and its values in class-level properties, reducing overhead during frequent sensor events.


PR created automatically by Jules for task 12003164693181404246 started by @LeanBitLab

Optimized the high-frequency `onSensorChanged` path in `StepCounterService` by caching the `SharedPreferences` instance and frequently accessed step-counting values in class-level properties.

💡 **What:**
- Moved `SharedPreferences` initialization to the beginning of `onCreate`.
- Introduced member variables `lastTotalSteps`, `baselineSteps`, and `stepDate` to cache values.
- Updated `onSensorChanged` to use cached variables instead of repeated `prefs.getFloat`/`prefs.getString` calls.
- Ensured in-memory state is kept in sync with persisted data.

🎯 **Why:**
Sensor events can trigger `onSensorChanged` many times per second. Reading from `SharedPreferences` in this method introduces unnecessary overhead due to XML parsing and I/O. Caching these values significantly reduces this overhead.

📊 **Measured Improvement:**
Due to environment limitations, a direct benchmark could not be completed (timed out). However, caching `SharedPreferences` is a standard Android optimization that avoids redundant file I/O and XML parsing, which is measurably beneficial for frequently called methods. Correctness was verified via compilation and review of the logic against the existing unit tests.
@google-labs-jules
Copy link
Copy Markdown

👋 Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

@LeanBitLab LeanBitLab merged commit 33c177c into main May 13, 2026
2 checks passed
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.

1 participant