Skip to content

reflect: remove calling mapiterkey and mapiterelem #69416

@xiaost

Description

@xiaost

Hi, Go team.

I'm working on optimizing iterating over map for an internal data encoding project.
And in our case, I found that one of the bottlenecks is reflect.MapIter .Next.
It can be improved by removing calling mapiterkey.

coz we have already copiedhiter struct from runtime for performance concern.
To make it further, we can use key and elem of hiter struct directly without calling mapiterkey and mapiterelem

This mainly impacts the Next method of reflect.MapIter.
I ran the BenchmarkMapIterNext benchmark in reflect/benchmark_test.go, and here is the output:

goos: darwin
goarch: arm64
pkg: reflect
cpu: Apple M2 Pro
               │  ./old.txt  │              ./new.txt              │
               │   sec/op    │   sec/op     vs base                │
MapIterNext-12   61.44n ± 1%   54.51n ± 1%  -11.29% (p=0.000 n=10)

I would like to raise CR if you're OK with it.

Metadata

Metadata

Assignees

No one assigned

    Labels

    NeedsFixThe path to resolution is known, but the work has not been done.compiler/runtimeIssues related to the Go compiler and/or runtime.

    Type

    No type

    Projects

    Status

    Done

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions