sizehelper

package
v0.3.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 7, 2024 License: ISC Imports: 1 Imported by: 0

Documentation

Overview

Copyright (c) 2023 The btcsuite developers Use of this source code is governed by an ISC license that can be found in the LICENSE file.

Index

Constants

View Source
const (
	// outpointSize is the size of an outpoint.
	//
	// This value is calculated by running the following:
	//	unsafe.Sizeof(wire.OutPoint{})
	OutpointSize = 36

	// uint64Size is the size of an uint64 allocated in memory.
	Uint64Size = 8

	// UtxoCacheBucketSize is the size of the bucket in the cache map.  Exact
	// calculation is (16 + keysize*8 + valuesize*8) where for the map of:
	// map[wire.OutPoint]*UtxoEntry would have a keysize=36 and valuesize=8.
	//
	// https://github.com/golang/go/issues/34561#issuecomment-536115805
	UtxoCacheBucketSize = 16 + Uint64Size*OutpointSize + Uint64Size*Uint64Size

	// BaseEntrySize is calculated by running the following on a 64-bit system:
	//   unsafe.Sizeof(blockchain.UtxoEntry{})
	BaseEntrySize = 40

	// PubKeyHashLen is the length of a P2PKH script.
	PubKeyHashLen = 25

	// AvgEntrySize is how much each entry we expect it to be.  Since most
	// txs are p2pkh, we can assume the entry to be more or less the size
	// of a p2pkh tx.  We add on 7 to make it 32 since 64 bit systems will
	// align by 8 bytes.
	AvgEntrySize = BaseEntrySize + (PubKeyHashLen + 7)
)

These constants are related to bitcoin.

View Source
const (
	MaxUintptr = ^uintptr(0)

	// PtrSize is the size of a pointer in bytes - unsafe.Sizeof(uintptr(0))
	// but as an ideal constant. It is also the size of the machine's native
	// word size (that is, 4 on 32-bit systems, 8 on 64-bit).
	PtrSize = 4 << (^uintptr(0) >> 63)
)

Variables

This section is empty.

Functions

func CalcNumEntries

func CalcNumEntries(bucketSize uintptr, maxMemoryUsage int64) ([]int, int)

CalcNumEntries returns a list of ints that represent how much entries a map should allocate for to stay under the maxMemoryUsage and an int that's a sum of the returned list of ints.

func CalculateMinEntries

func CalculateMinEntries(totalBytes int, bucketSize int) int

CalculateMinEntries returns the minimum number of entries that will make the map allocate the given total bytes. -1 on the returned entry count will make the map allocate half as much total bytes (for returned entry count that's greater than 0).

func CalculateRoughMapSize

func CalculateRoughMapSize(hint int, bucketSize uintptr) int

CalculateRoughMapSize returns a close enough estimate of the total memory allocated by a map. hint should be the same value as the number you give when making a map with the following syntax: make(map[k]v, hint)

bucketsize is (16 + keysize*8 + valuesize*8). For a map of: map[int64]int64, keysize=8 and valuesize=8. There are edge cases where the bucket size is different that I can't find the source code for. https://github.com/golang/go/issues/34561#issuecomment-536115805

I suspect it's because of alignment and how the compiler handles it but when compared with how much the compiler allocates, it's a couple hundred bytes off.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL