Skip to content

Commit

Permalink
Replace if with switch in Array.Storable()
Browse files Browse the repository at this point in the history
  • Loading branch information
fxamacker committed Sep 21, 2023
1 parent f8e0992 commit 3584952
Showing 1 changed file with 30 additions and 26 deletions.
56 changes: 30 additions & 26 deletions array.go
Original file line number Diff line number Diff line change
Expand Up @@ -3020,19 +3020,18 @@ func (a *Array) Storable(_ SlabStorage, _ Address, maxInlineSize uint64) (Storab
inlined := a.root.Inlined()
inlinable := a.root.Inlinable(maxInlineSize)

if inlinable && inlined {
switch {
case inlinable && inlined:
// Root slab is inlinable and was inlined.
// Return root slab as storable, no size adjustment and change to storage.
return a.root, nil
}

if !inlinable && !inlined {
case !inlinable && !inlined:
// Root slab is not inlinable and was not inlined.
// Return root slab ID as storable, no size adjustment and change to storage.
return SlabIDStorable(a.SlabID()), nil
}

if inlinable && !inlined {
case inlinable && !inlined:
// Root slab is inlinable and was NOT inlined.

// Inline root data slab.
Expand Down Expand Up @@ -3061,34 +3060,39 @@ func (a *Array) Storable(_ SlabStorage, _ Address, maxInlineSize uint64) (Storab
rootDataSlab.inlined = true

return rootDataSlab, nil
}

// here, root slab is NOT inlinable and was previously inlined.
case !inlinable && inlined:

// Un-inline root slab.
// Root slab is NOT inlinable and was previously inlined.

// Inlined root slab must be data slab.
rootDataSlab, ok := a.root.(*ArrayDataSlab)
if !ok {
return nil, NewFatalError(fmt.Errorf("unexpected inlined array slab type %T", a.root))
}
// Un-inline root slab.

// Update root data slab size
rootDataSlab.header.size = rootDataSlab.header.size -
inlinedArrayDataSlabPrefixSize +
arrayRootDataSlabPrefixSize
// Inlined root slab must be data slab.
rootDataSlab, ok := a.root.(*ArrayDataSlab)
if !ok {
return nil, NewFatalError(fmt.Errorf("unexpected inlined array slab type %T", a.root))
}

// Update root data slab inlined status.
rootDataSlab.inlined = false
// Update root data slab size
rootDataSlab.header.size = rootDataSlab.header.size -
inlinedArrayDataSlabPrefixSize +
arrayRootDataSlabPrefixSize

// Store root slab in storage
err := a.Storage.Store(rootDataSlab.header.slabID, a.root)
if err != nil {
// Wrap err as external error (if needed) because err is returned by SlabStorage interface.
return nil, wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to store slab %s", a.SlabID()))
}
// Update root data slab inlined status.
rootDataSlab.inlined = false

// Store root slab in storage
err := a.Storage.Store(rootDataSlab.header.slabID, a.root)
if err != nil {
// Wrap err as external error (if needed) because err is returned by SlabStorage interface.
return nil, wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to store slab %s", a.SlabID()))
}

return SlabIDStorable(a.SlabID()), nil

return SlabIDStorable(a.SlabID()), nil
default:
panic("not reachable")
}
}

var emptyArrayIterator = &ArrayIterator{}
Expand Down

0 comments on commit 3584952

Please sign in to comment.