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

cmd/compile: dead code-elimination should work for sync.OnceValue #62202

Open
dsnet opened this issue Aug 22, 2023 · 2 comments
Open

cmd/compile: dead code-elimination should work for sync.OnceValue #62202

dsnet opened this issue Aug 22, 2023 · 2 comments
Labels
binary-size compiler/runtime Issues related to the Go compiler and/or runtime. FeatureRequest NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@dsnet
Copy link
Member

dsnet commented Aug 22, 2023

This is special case of #14840, but identified as a potential source of special-cases if the general case is hard to solve.

Compiling the following program:

package main

import "sync"

var initializeVariable = sync.OnceValue(func() string {
    return "THIS_IS_DEAD_CODE"
})

func main() {}

should not link in logic for initializeVariable since the variable is dead.
This also applies to OnceFunc and OnceValues.

At present, OnceValue is unfortunately not a drop-in replacement for for sync.Once for initializing global variables.

@gopherbot gopherbot added the compiler/runtime Issues related to the Go compiler and/or runtime. label Aug 22, 2023
@cagedmantis cagedmantis added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Aug 22, 2023
@cagedmantis cagedmantis added this to the Backlog milestone Aug 22, 2023
@mknyszek
Copy link
Contributor

CC @dr2chase who has been thinking about function purity analysis, which may be related.

@aclements
Copy link
Member

A related optimization we should consider is detecting that the package variable is never modified and then applying inlining at sites that call the function bound to it. We do this for closures bound to local variables, but not at package scope. (This might be a different issue, or could be thought of together with this one.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
binary-size compiler/runtime Issues related to the Go compiler and/or runtime. FeatureRequest NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
Development

No branches or pull requests

6 participants