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

no global var #19282

Closed
wants to merge 4 commits into from
Closed

no global var #19282

wants to merge 4 commits into from

Conversation

6543
Copy link
Member

@6543 6543 commented Mar 31, 2022

last missing nit from #17919

@6543 6543 added type/refactoring Existing code has been cleaned up. There should be no new functionality. skip-changelog This PR is irrelevant for the (next) changelog, for example bug fixes for unreleased features. labels Mar 31, 2022
@6543 6543 added this to the 1.17.0 milestone Mar 31, 2022
@6543 6543 requested a review from wxiaoguang March 31, 2022 16:23
@GiteaBot GiteaBot added the lgtm/need 1 This PR needs approval from one additional maintainer to be merged. label Mar 31, 2022
@lunny
Copy link
Member

lunny commented Mar 31, 2022

What's wrong with the global variable?

@6543
Copy link
Member Author

6543 commented Mar 31, 2022

It's only used inside the function - it should never be global!!!

@wxiaoguang
Copy link
Contributor

wxiaoguang commented Mar 31, 2022

You see, it's not a simple global variable. It's a cache. It's usually nil and doesn't consume memory.

It helps the http.HandlerFunc in install.go to save time (save CPU) from making/copying the slice again and again.

I do not see the necessary to change it. If you want to change it, the logic inside getDbTypeNames should also be changed (remove the if nil check) ............

@6543
Copy link
Member Author

6543 commented Mar 31, 2022

I dont think we need to cache this as install page will only show up on installation

@wxiaoguang
Copy link
Contributor

Then you should also remove the if supportedDbTypeNames == nil {

@6543
Copy link
Member Author

6543 commented Mar 31, 2022

in general we should avoid global vars if possible

they produce races, consume resources not needed after exec of code parts mostly ...

@wxiaoguang
Copy link
Contributor

in general we should avoid global vars if possible

they produce races, consume resources not needed after exec of code parts mostly ...

As comments before, it's just a install page, no race there. And it only consumes 8 bytes (a pointer) memory. Just one request consume much much more than that .... Personally I won't put too many loops in the common Context construction. Indeed there is no problem to remove the cache if you like to.

@GiteaBot GiteaBot added lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore. and removed lgtm/need 1 This PR needs approval from one additional maintainer to be merged. labels Mar 31, 2022
@wxiaoguang
Copy link
Contributor

After grep-ing ^var in *.go, I found some more global variables, like this (ps: there is a bug that the ctx is not used ....)

Maybe it's worth to do some clean up in the project.

image

@6543 6543 added the pr/wip This PR is not ready for review label Mar 31, 2022
@silverwind
Copy link
Member

silverwind commented Mar 31, 2022

There still may be a performance benefit of keeping these top-level variables in case their instantiation is expensive. For example, I like to put regexes in top level because pre-compiling them will always be better performance than to compile them on every function call.

@wxiaoguang
Copy link
Contributor

regex vars can be kept, while supportedDbTypeNames and delRepoArchiver etc can be removed safely.

@wxiaoguang
Copy link
Contributor

For the supportedDbTypeNames, I made another fix, move it from global to local and still cache the slice. And there is another fix for the installation page, need 👀

@6543 6543 closed this Apr 1, 2022
@6543 6543 deleted the next-17919 branch April 1, 2022 16:00
@go-gitea go-gitea locked and limited conversation to collaborators Apr 28, 2022
@wxiaoguang wxiaoguang removed this from the 1.17.0 milestone Jun 12, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore. pr/wip This PR is not ready for review skip-changelog This PR is irrelevant for the (next) changelog, for example bug fixes for unreleased features. type/refactoring Existing code has been cleaned up. There should be no new functionality.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants