Skip to content

Instantly share code, notes, and snippets.

View chihkaiyu's full-sized avatar

Kai Yu chihkaiyu

View GitHub Profile
@chihkaiyu
chihkaiyu / erc721.abi
Last active January 13, 2022 15:43
erc721.abi
[
{
"inputs": [],
"stateMutability": "nonpayable",
"type": "constructor"
},
{
"anonymous": false,
"inputs": [
{
@chihkaiyu
chihkaiyu / main.py
Created January 26, 2020 08:54
Scratch-off Lottery Ticket Checker
import sys
import itertools
LOTTERY = {
'HUN':100,
'TWO':200,
'THR':300,
'FOR':400,
'FIV':500,
'SIX':600,
@chihkaiyu
chihkaiyu / largesize.go
Created July 10, 2019 04:36
large size
// malloc.go
func mallocgc(size uintptr, typ *_type, needzero bool) unsafe.Pointer {
// 處理 GC 相關事情
if size <= maxSmallSize {
if noscan && size < maxTinySize {
// tiny size 分配流程
} else {
// small size 分配流程
}
} else {
@chihkaiyu
chihkaiyu / tinysize.go
Created July 10, 2019 04:35
tiny size
// malloc.go
func mallocgc(size uintptr, typ *_type, needzero bool) unsafe.Pointer {
// 處理 GC 相關事情
if size <= maxSmallSize {
if noscan && size < maxTinySize {
off := c.tinyoffset
// 根據要分配的 size 對齊到 2、4、8
if size&7 == 0 {
off = round(off, 8)
} else if size&3 == 0 {
@chihkaiyu
chihkaiyu / smallsize.go
Last active July 10, 2019 04:35
small size
// malloc.go
func mallocgc(size uintptr, typ *_type, needzero bool) unsafe.Pointer {
// 處理 GC 相關事情
if size <= maxSmallSize {
if noscan && size < maxTinySize {
// tiny size 分配流程
} else {
// 根據要分配的 size 找出合適的 size class
var sizeclass uint8
// sizeclasses.go
// class bytes/obj bytes/span objects tail waste max waste
// 1 8 8192 1024 0 87.50%
// 2 16 8192 512 0 43.75%
// 3 32 8192 256 0 46.88%
// 4 48 8192 170 32 31.52%
// 5 64 8192 128 0 23.44%
// 6 80 8192 102 32 19.07%
// 7 96 8192 85 32 15.95%
// 8 112 8192 73 16 13.56%
// mcache.go
type mcache struct {
// tiny 指向目前的 tiny block,若沒有則為 nil
tiny uintptr
// tinyoffset 為此 tiny block 分配到哪裡了
tinyoffset uintptr
// alloc 為 mspan array,其 index 為 span class,需要存取時,便取出相對應的 mspan
alloc [numSpanClasses]*mspan // spans to allocate from, indexed by spanClass
// mcentral.go
type mcentral struct {
// 每一種 span class 只會有一個 mcentral,當有多個 mcache 向 mcentral 申請新的 mspan 時,便需要 lock
lock mutex
// 該 mcentral 屬於的 span class
spanclass spanClass
// 還有可分配 object 的 mspan list
nonempty mSpanList // list of spans with a free object, ie a nonempty free list
// mheap.go
type mspan struct {
// 指向下一個 mspan 的 pointer (在 mcentral 裡,mspan 是以 double-linked list 存放)
next *mspan // next span in list, or nil if none
// 指向前一個 mspan 的 pointer
prev *mspan // previous span in list, or nil if none
list *mSpanList // For debugging. TODO: Remove.
// 該 mspan 第一個 byte 的起始位址,即是在 arena 的起始位址
// mheap.go
type mheap struct {
// mheap 只會有一個,當同時有多個角色來向 mheap 要求記憶體時,便需要 lock
lock mutex
// 一個 mTreap node 只會存放一個 span
// free 表示是乾淨的 span,而 scav 表示被汙染的 span
// 被汙染的意思是其中有 page 歸還給 OS 了
free mTreap // free and non-scavenged spans
scav mTreap // free and scavenged spans