Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

proposal: reflect/type.go: func (t *structType) Field(i int) (f StructField) Why not delete the memory allocation? #68380

Closed
Fgaoxing opened this issue Jul 11, 2024 · 3 comments
Labels
Milestone

Comments

@Fgaoxing
Copy link

Proposal Details

I noticed the problem with #2230, but this allocation is so performance-impairing that it consumes hundreds of nanoseconds more, it's almost pointless, and the "index" is the same as the incoming "i", which can be deleted entirely. If you are worried about compatibility, you can also provide a new method without deleting the old one (this may cause new history issues, but compatibility will be better). The problem with this method is that packages seeking high performance have to use the "unsafe" and "namelink" methods instead of directly using "toType" and "toRtype", which may also lead to an additional risk of memory leaks, which are dangerous and destabilizing (this may be similar to the "reflect.SliceHeader" problem).

@gopherbot gopherbot added this to the Proposal milestone Jul 11, 2024
@gabyhelp
Copy link

Related Issues and Documentation

(Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.)

@ianlancetaylor
Copy link
Member

Note: the issue reference in the top comment should be to #2320.

We can't change the definition of StructField; that would break existing programs and the Go compatibility guarantee. We aren't going to introduce a new variant of StructField that duplicates the existing definition.

Closing as infeasible.

@ianlancetaylor ianlancetaylor closed this as not planned Won't fix, can't repro, duplicate, stale Jul 11, 2024
@gopherbot
Copy link
Contributor

Change https://go.dev/cl/597855 mentions this issue: reflect: avoid allocation in structType.Field common cases

gopherbot pushed a commit that referenced this issue Sep 10, 2024
Use assembler to make runtime.staticuint64s into a readonly array
so that the reflect package can safely create a slice without requiring
any allocation.

Fixes #2320
Fixes #68380

Change-Id: If2c97238eca782d0632db265c840581d4ecb9d18
Reviewed-on: https://go-review.googlesource.com/c/go/+/597855
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
Reviewed-by: Cherry Mui <cherryyz@google.com>
Reviewed-by: Cuong Manh Le <cuong.manhle.vn@gmail.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Auto-Submit: Ian Lance Taylor <iant@golang.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants