github.com/onflow/atree@v0.6.0/settings.go (about)

     1  /*
     2   * Atree - Scalable Arrays and Ordered Maps
     3   *
     4   * Copyright 2021 Dapper Labs, Inc.
     5   *
     6   * Licensed under the Apache License, Version 2.0 (the "License");
     7   * you may not use this file except in compliance with the License.
     8   * You may obtain a copy of the License at
     9   *
    10   *   http://www.apache.org/licenses/LICENSE-2.0
    11   *
    12   * Unless required by applicable law or agreed to in writing, software
    13   * distributed under the License is distributed on an "AS IS" BASIS,
    14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    15   * See the License for the specific language governing permissions and
    16   * limitations under the License.
    17   */
    18  
    19  package atree
    20  
    21  import "fmt"
    22  
    23  // Slab invariants:
    24  // - each element can't take up more than half of slab size (including encoding overhead and digest)
    25  // - data slab must have at least 2 elements when slab size > maxThreshold
    26  
    27  const (
    28  	defaultSlabSize       = uint64(1024)
    29  	minSlabSize           = uint64(256)
    30  	minElementCountInSlab = 2
    31  )
    32  
    33  var (
    34  	targetThreshold            uint64
    35  	minThreshold               uint64
    36  	maxThreshold               uint64
    37  	MaxInlineArrayElementSize  uint64
    38  	maxInlineMapElementSize    uint64
    39  	MaxInlineMapKeyOrValueSize uint64
    40  )
    41  
    42  func init() {
    43  	SetThreshold(defaultSlabSize)
    44  }
    45  
    46  func SetThreshold(threshold uint64) (uint64, uint64, uint64, uint64) {
    47  	if threshold < minSlabSize {
    48  		panic(fmt.Sprintf("Slab size %d is smaller than minSlabSize %d", threshold, minSlabSize))
    49  	}
    50  
    51  	targetThreshold = threshold
    52  	minThreshold = targetThreshold / 2
    53  	maxThreshold = uint64(float64(targetThreshold) * 1.5)
    54  
    55  	// Total slab size available for array elements, excluding slab encoding overhead
    56  	availableArrayElementsSize := targetThreshold - arrayDataSlabPrefixSize
    57  	MaxInlineArrayElementSize = availableArrayElementsSize / minElementCountInSlab
    58  
    59  	// Total slab size available for map elements, excluding slab encoding overhead
    60  	availableMapElementsSize := targetThreshold - mapDataSlabPrefixSize - hkeyElementsPrefixSize
    61  
    62  	// Total encoding overhead for one map element (key+value)
    63  	mapElementOverheadSize := uint64(digestSize)
    64  
    65  	// Max inline size for a map's element
    66  	maxInlineMapElementSize = availableMapElementsSize/minElementCountInSlab - mapElementOverheadSize
    67  
    68  	// Max inline size for a map's key or value, excluding element encoding overhead
    69  	MaxInlineMapKeyOrValueSize = (maxInlineMapElementSize - singleElementPrefixSize) / 2
    70  
    71  	return minThreshold, maxThreshold, MaxInlineArrayElementSize, MaxInlineMapKeyOrValueSize
    72  }