Aptos Resource Groups: How to Pack Data for Lower Gas
Resource Groups are an Aptos-specific storage optimization that bundles multiple related resources under a single account into one storage slot, reducing the per-item overhead that normally applies when each resource is stored separately. By packing data together, you pay less gas for writes and reads.
Why resource groups reduce gas
In Aptos Move, every resource stored under an account normally occupies its own storage slot. Each slot incurs a fixed cost for creation, modification, and deletion, plus the cost of the data itself. Resource Groups allow you to place several resources - for example, a user profile, a balance, and a settings struct - into a single slot. The fixed cost is paid once for the group, not once per resource. This can cut gas by 30 - 60% on operations that touch multiple resources in the same account, depending on how many resources you group.
The trade-off: all resources in a group are loaded and deserialized together when any one of them is accessed. If your application frequently reads only one member of a group, you might load unnecessary data. Profile your use case before committing.
How to Define and Use a Resource Group
A Resource Group is created by annotating a module with the resource_group attribute and specifying which structs belong to it. All structs in the group must be defined in the same module.
Step 1: Declare the group
In your Move module, add a resource_group declaration at the top level:
module my_addr::user_data {
use aptos_framework::resource_group;
resource_group! {
struct UserGroup {
// Members are specified below
}
}
// ...
}
The group name (here UserGroup) can be any valid identifier. It does not become a struct you use directly; it is a label for the group.
Step 2: Mark structs as group members
Each struct you want in the group must be annotated with #[resource_group_member(group = <group_name>)]. The struct itself remains a regular resource - you still use move_to and borrow_global as usual.
#[resource_group_member(group = UserGroup)]
struct Profile has key, store {
name: String,
avatar_url: String,
}
#[resource_group_member(group = UserGroup)]
struct Balance has key {
coin: u64,
}
#[resource_group_member(group = UserGroup)]
struct Settings has key {
notifications_enabled: bool,
}
Step 3: Use the resources normally
Write functions that create, read, and modify these structs exactly as you would without Resource Groups. The Move compiler and the runtime handle packing them into the same slot.
public entry fun create_profile(account: &signer, name: String, avatar_url: String) {
let profile = Profile { name, avatar_url };
move_to(account, profile);
// Balance and Settings can be added later; they will join the same group.
}
public fun get_name(addr: address): String acquires Profile {
borrow_global<Profile>(addr).name
}
When you call move_to for the first struct in the group, Aptos creates the group slot. Subsequent move_to calls for other group members add them to the same slot. The gas for the second and later move_to calls is lower because the slot already exists.
Gas Savings Example
Assume each resource slot costs a fixed 100 gas units (illustrative). Without Resource Groups, storing three separate resources costs 300 units for the slots plus the data cost. With a group, you pay one slot cost (100) plus a small overhead for grouping. For three resources, you save roughly 200 units per account creation. Updates that touch multiple group members also save because the slot is already loaded.
The exact savings depend on Aptos gas parameters, which can change. Use the Aptos gas profiler (covered in the "Aptos gas profiling" article on this site) to measure actual costs for your module.
Limitations and Pitfalls
- All-or-nothing loading: Reading any group member deserializes every member in the group. If your group is large and you only need one field, you pay for loading the rest. Keep groups small and logically connected.
- Cannot remove a member from a group: Once a struct is marked as a member, it is always in the group for that account. You cannot "unpack" it into its own slot later. Plan your grouping before deployment.
- Maximum group size: The Aptos virtual machine enforces a maximum number of resources per group (check the current limit in the Aptos documentation). Exceeding it causes transaction failure.
- Module upgrade restrictions: If you change the group definition or a member struct in an incompatible way, the upgrade may fail. Follow the upgrade rules from the "Aptos Move Module Upgrade Policies" article.
- No effect on existing accounts: Resource Groups only apply to new storage slots. Existing accounts that already have the resources stored individually will not be automatically grouped.
When to use resource groups
Use them when your application stores multiple small to medium-sized resources per user that are typically accessed together. Common candidates:
- User profile + preferences + reputation score
- Game inventory + stats + cooldown timer
- DeFi position + accumulated rewards + last update timestamp
Avoid grouping unrelated resources that are rarely read together - for example, a user's profile and a completely separate voting record. The gas saved on storage may be lost on unnecessary deserialization.
Not financial advice. suiboxer.xyz publishes market data and general information about digital assets. Crypto assets are volatile and you can lose everything you put in. Nothing here is a recommendation to buy, sell or hold, and we make no price predictions.
Prices are sourced from third parties and may be delayed or wrong. Verify anything you intend to act on against a primary source.