Map garbarge collection bug #204

Closed
opened 2019-12-10 17:51:58 +09:00 by zxq9 · 0 comments
zxq9 commented 2019-12-10 17:51:58 +09:00 (Migrated from gitlab.com)

Created by: UlfNorell

Reference counting for store maps is too conservative for nested map updates in some cases, leading to maps being copied instead of updated inplace. Example:

@compiler >= 4
payable contract MultiMap =

  record state = 
    { my_map          : map(string, map(int, custom)) }

  record custom = 
    { sender        : address
    , received_at   : int }

  entrypoint init () : state = { my_map = {} }
    
  entrypoint my_map() : map(string, map(int, custom)) = state.my_map
  
  entrypoint size(key: string) : int = Map.size(state.my_map[key = {}])

  payable stateful entrypoint set (key: string) : unit =
    put(state{ my_map[key = {}][size(key)] = new_custom() })
    
  stateful function new_custom() : custom =
    { sender        = Call.caller,
      received_at   = Chain.timestamp }

Adding two keys makes the outer map big enough to be given store handling and successive sets to one of the keys then requires linear gas instead of constant, since the inner map is copied instead of updated inplace.

Work-around: avoid nested maps.

*Created by: UlfNorell* Reference counting for store maps is too conservative for nested map updates in some cases, leading to maps being copied instead of updated inplace. Example: ```sophia @compiler >= 4 payable contract MultiMap = record state = { my_map : map(string, map(int, custom)) } record custom = { sender : address , received_at : int } entrypoint init () : state = { my_map = {} } entrypoint my_map() : map(string, map(int, custom)) = state.my_map entrypoint size(key: string) : int = Map.size(state.my_map[key = {}]) payable stateful entrypoint set (key: string) : unit = put(state{ my_map[key = {}][size(key)] = new_custom() }) stateful function new_custom() : custom = { sender = Call.caller, received_at = Chain.timestamp } ``` Adding two keys makes the outer map big enough to be given store handling and successive `set`s to one of the keys then requires linear gas instead of constant, since the inner map is copied instead of updated inplace. Work-around: avoid nested maps.
Sign in to join this conversation.
No Milestone iris
No project
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: QPQ-AG/sophia#204
No description provided.