-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[patch] add Real Map memory size function (#138)
Signed-off-by: kpango <kpango@vdaas.org>
- Loading branch information
Showing
17 changed files
with
300 additions
and
17 deletions.
There are no files selected for viewing
Empty file.
Empty file.
Empty file.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
module github.com/kpango/gache/v2 | ||
|
||
go 1.22.3 | ||
go 1.23.1 | ||
|
||
require ( | ||
github.com/kpango/fastime v1.1.9 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
// Copyright (c) 2009 The Go Authors. All rights resered. | ||
// Modified <Yusuke Kato (kpango)> | ||
|
||
// Redistribution and use in source and binary forms, with or without | ||
// modification, are permitted provided that the following conditions are | ||
// met: | ||
|
||
// * Redistributions of source code must retain the above copyright | ||
// notice, this list of conditions and the following disclaimer. | ||
// * Redistributions in binary form must reproduce the above | ||
// copyright notice, this list of conditions and the following disclaimer | ||
// in the documentation and/or other materials provided with the | ||
// distribution. | ||
// * Neither the name of Google Inc. nor the names of its | ||
// contributors may be used to endorse or promote products derived from | ||
// this software without specific prior written permission. | ||
|
||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
|
||
package gache | ||
|
||
import "unsafe" | ||
|
||
// A header for a Go map. | ||
type hmap struct { | ||
// Note: the format of the hmap is also encoded in cmd/compile/internal/reflectdata/reflect.go. | ||
// Make sure this stays in sync with the compiler's definition. | ||
count int // # live cells == size of map. Must be first (used by len() builtin) | ||
flags uint8 | ||
B uint8 // log_2 of # of buckets (can hold up to loadFactor * 2^B items) | ||
noverflow uint16 // approximate number of overflow buckets; see incrnoverflow for details | ||
hash0 uint32 // hash seed | ||
|
||
buckets unsafe.Pointer // array of 2^B Buckets. may be nil if count==0. | ||
oldbuckets unsafe.Pointer // previous bucket array of half the size, non-nil only when growing | ||
nevacuate uintptr // progress counter for evacuation (buckets less than this have been evacuated) | ||
} | ||
|
||
const bucketCnt = 8 | ||
|
||
// A bucket for a Go map. | ||
type bmap struct { | ||
// tophash generally contains the top byte of the hash value | ||
// for each key in this bucket. If tophash[0] < minTopHash, | ||
// tophash[0] is a bucket evacuation state instead. | ||
tophash [bucketCnt]uint8 | ||
// Followed by bucketCnt keys and then bucketCnt elems. | ||
// NOTE: packing all the keys together and then all the elems together makes the | ||
// code a bit more complicated than alternating key/elem/key/elem/... but it allows | ||
// us to eliminate padding which would be needed for, e.g., map[int64]int8. | ||
// Followed by an overflow pointer. | ||
} | ||
|
||
var singleBucketSize = unsafe.Sizeof(bmap{}) | ||
|
||
func mapSize[K comparable, V any](m map[K]V) (size uintptr) { | ||
h := (*hmap)(*(*unsafe.Pointer)(unsafe.Pointer(&m))) | ||
if h == nil { | ||
return 0 | ||
} | ||
var ( | ||
zeroK K | ||
zeroV V | ||
) | ||
return h.Size(unsafe.Sizeof(zeroK), unsafe.Sizeof(zeroV)) | ||
} | ||
|
||
func (b *bmap) Size() (size uintptr) { | ||
return unsafe.Sizeof(b.tophash) | ||
} | ||
|
||
func (h *hmap) Size(kSize, vSize uintptr) (size uintptr) { | ||
size += unsafe.Sizeof(h.count) | ||
size += unsafe.Sizeof(h.flags) | ||
size += unsafe.Sizeof(h.B) | ||
size += unsafe.Sizeof(h.noverflow) | ||
size += unsafe.Sizeof(h.hash0) | ||
size += unsafe.Sizeof(h.buckets) | ||
size += unsafe.Sizeof(h.oldbuckets) | ||
size += unsafe.Sizeof(h.nevacuate) | ||
|
||
if h.B == 0 { | ||
return size | ||
} | ||
bucketSize := singleBucketSize + (bucketCnt * (kSize + vSize)) | ||
if h.buckets != nil { | ||
size += uintptr(1<<h.B) * bucketSize | ||
} | ||
if h.oldbuckets != nil && h.B > 1 { | ||
size += uintptr(1<<(h.B-1)) * bucketSize | ||
} | ||
return size | ||
} |
Oops, something went wrong.