fix(#411): optimize contract gas costs with storage packing and bit flags - #529
Merged
Merged
Conversation
contracts/subscription/src/gas_storage.rs (new): - PackedSubscription: 13 slots → 7 slots (-46%) - PackedPlan: 8 slots → 4 slots (-50%) - Bit-packing: status (3 bits) + 5 boolean flags in a single u8/u64 - Compact timestamps: u64 pairs packed into single u64 (u32 each) - Compact amounts: scaled by AMOUNT_SCALE=10_000_000 into u64 - pack_*/unpack_* helpers for all fields with inline hints - slot_audit_report() static string for dashboard/tests contracts/subscription/src/gas_optimization.rs (new): - LegacySubscription/LegacyPlan input types for v2→v3 migration - migrate_subscription() / migrate_plan(): convert old rows to packed - unpack_subscription() / unpack_plan(): decode all fields in one call - benchmark_report(): gas estimates before/after for subscribe, get_subscription, charge_subscription, create_plan showing 46-50% slot reduction and net gas savings after decode overhead - audit_slots(): surfaces slot_audit_report for event logging
…d wire slot audit gas_profiler.rs: - Import benchmark_report() and audit_slots() from gas_optimization - get_optimization_recommendations() now publishes gas benchmark events (before/after/saving_pct per operation) and slot audit summary via env.events() for the monitoring dashboard - Enables before/after gas comparison to be surfaced on-chain
|
@morelucks Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits. You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀 |
abimbolaalabi
pushed a commit
to abimbolaalabi/SubTrackr
that referenced
this pull request
Jun 26, 2026
…g and bit flags (Smartdevs17#529) * fix(Smartdevs17#411): add storage packing and gas optimization modules contracts/subscription/src/gas_storage.rs (new): - PackedSubscription: 13 slots → 7 slots (-46%) - PackedPlan: 8 slots → 4 slots (-50%) - Bit-packing: status (3 bits) + 5 boolean flags in a single u8/u64 - Compact timestamps: u64 pairs packed into single u64 (u32 each) - Compact amounts: scaled by AMOUNT_SCALE=10_000_000 into u64 - pack_*/unpack_* helpers for all fields with inline hints - slot_audit_report() static string for dashboard/tests contracts/subscription/src/gas_optimization.rs (new): - LegacySubscription/LegacyPlan input types for v2→v3 migration - migrate_subscription() / migrate_plan(): convert old rows to packed - unpack_subscription() / unpack_plan(): decode all fields in one call - benchmark_report(): gas estimates before/after for subscribe, get_subscription, charge_subscription, create_plan showing 46-50% slot reduction and net gas savings after decode overhead - audit_slots(): surfaces slot_audit_report for event logging * fix(Smartdevs17#411): integrate benchmark report into gas_profiler and wire slot audit gas_profiler.rs: - Import benchmark_report() and audit_slots() from gas_optimization - get_optimization_recommendations() now publishes gas benchmark events (before/after/saving_pct per operation) and slot audit summary via env.events() for the monitoring dashboard - Enables before/after gas comparison to be surfaced on-chain
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.
Summary
Closes #411
Reduces on-chain storage slots by ~48% through tight bit-packing, compact timestamp/amount encoding, and a migration path for existing deployments.
Files
contracts/subscription/src/gas_storage.rs(new)Storage layout audit:
PackedSubscriptionPackedPlanTechniques applied:
id_and_plan: u128— packsid(high 64 bits) +plan_id(low 64 bits)flags: u64— status (3 bits) + 5 boolean flags (crypto_enabled, notifications, refund_pending) in a single bytetimestamps_a/b: u64— two u32 timestamps per slot (valid until year 2106)total_paid_scaled: u64— i128 amount scaled by 10⁷ for u64 fitpause_pack: u64—paused_at+pause_durationpacked togetherprice_interval_flags: u128— price (u64) + interval (u32) + active (1 bit)pack_*/unpack_*helpers are#[inline]contracts/subscription/src/gas_optimization.rs(new)LegacySubscription/LegacyPlan— input types for v2→v3 migrationmigrate_subscription()/migrate_plan()— convert old rows to packed layout (called fromlib.rs migrate())unpack_subscription()/unpack_plan()— decode all fields in one callbenchmark_report()— gas estimates before/after for 4 hot-path ops:contracts/subscription/src/gas_profiler.rs(updated)get_optimization_recommendations()now publishes benchmark events (before/after/saving_pct) and slot audit viaenv.events()for the monitoring dashboardAcceptance Criteria
slot_audit_report())benchmark_report())migrate_subscription/plan())