Skip to content

proposal: runtime: expose the internal/runtime/sys.Prefetch intrinsic #68769

@rip-create-your-account

Description

@rip-create-your-account

Proposal Details

I'm working on a concurrent hash map and with me having experience in using them in highly concurrent contexts with high throughput requirements I knew to add batch Put and Get methods to it. Such API design exposes the very obvious opportunity for prefetching the data.

To experiment with the impact of internal/runtime/sys.Prefetch on the batch Get call I modified runtime/panic.go to expose the intrinsic. go tool objdump then confirmed that a PREFETCHT0 instruction is emitted into the binary as expected.

// in runtime/panic.go
func Prefetch(addr uintptr) {
	sys.Prefetch(addr)
}

// and using it like this
for _, bucket := range initial_buckets_for_keys {
	runtime.Prefetch(uintptr(unsafe.Pointer(bucket)))
}

Looking up 100k distinct entries from a hash map with 5000k entries became significantly faster.

old: BenchmarkLookupBatches/size=100000-8                      63.14 million_lookups/s
new: BenchmarkLookupBatches/size=100000-8                      96.64 million_lookups/s

Which looks like a worthwhile optimization for me.

I personally like how for me this runtime.Prefetch Just Works™ so I propose adding just it. I also propose to not tell of it's addition in the change log so people don't use it willy-nilly. Just imagine the damage that all the blog spam will cause in the overall ecosystem, possibly leaking into other language communities too.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    Status

    Incoming

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions