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

feat: lazy initialization codec #1584

Closed
wants to merge 1 commit into from

Conversation

xieyuschen
Copy link

@xieyuschen xieyuschen commented Jul 12, 2023

What I have changed

I have applied the TODO: make this lazy initialization instead left by @sagikazarmark . The main changes are listed as following:

  • lazily initialize the codec
  • merge encoder and decoder to codec.
    This change is caused as I try to create a new codec by calling a method of a nil pointer with type. I want the decoder and encoder to share the same object constructed by the method.
  • create constructor.Codec interface to avoid cycle import.

Benchmark

I use this command to run the benchmark across commits:

git checkout 214f242 
go test -v -bench=. -count=10 > old.out
git checkout 60d2632
go test -v -bench=. -count=10  > new.out
benchstat old.out new.out

The output is:

goos: darwin
goarch: amd64
pkg: github.com/spf13/viper
cpu: Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
                    │    old.out    │               new.out               │
                    │    sec/op     │   sec/op     vs base                │
New-12                2972.5n ± 77%   431.8n ± 3%  -85.48% (p=0.000 n=10)
_unmarshalReader-12    148.4µ ±  7%   139.1µ ± 3%   -6.25% (p=0.019 n=10)
GetBool-12             186.6n ± 18%   180.6n ± 2%   -3.22% (p=0.017 n=10)
Get-12                 157.5n ±  7%   156.6n ± 1%        ~ (p=0.436 n=10)
GetBoolFromMap-12      3.944n ± 28%   3.878n ± 2%        ~ (p=0.052 n=10)
geomean                551.7n         366.2n       -33.62%

For me, I think this change doesn't reduce the efficiency of viper.

@CLAassistant
Copy link

CLAassistant commented Jul 12, 2023

CLA assistant check
All committers have signed the CLA.

@github-actions
Copy link

👋 Thanks for contributing to Viper! You are awesome! 🎉

A maintainer will take a look at your pull request shortly. 👀

In the meantime: We are working on Viper v2 and we would love to hear your thoughts about what you like or don't like about Viper, so we can improve or fix those issues.

⏰ If you have a couple minutes, please take some time and share your thoughts: https://forms.gle/R6faU74qPRPAzchZ9

📣 If you've already given us your feedback, you can still help by spreading the news,
either by sharing the above link or telling people about this on Twitter:

https://twitter.com/sagikazarmark/status/1306904078967074816

Thank you! ❤️

@xieyuschen xieyuschen force-pushed the lazy-initialization branch 3 times, most recently from e7e12b3 to 60d2632 Compare July 12, 2023 10:30
@sagikazarmark
Copy link
Collaborator

Wow, thanks @xieyuschen !

Give me some time to review this PR, but I really appreciate the contribution.

One note off the bat: performance generally is not a huge concern in case of Viper as it's mostly initialized/used at the beginning of an application run, so readability is generally preferred over rigorous performance optimization.

@sagikazarmark sagikazarmark added the kind/enhancement New feature or request label Jul 21, 2023
@xieyuschen
Copy link
Author

Wow, thanks @xieyuschen !

Give me some time to review this PR, but I really appreciate the contribution.

One note off the bat: performance generally is not a huge concern in case of Viper as it's mostly initialized/used at the beginning of an application run, so readability is generally preferred over rigorous performance optimization.

I agree with you, and I have changed the implementation in a go style. @sagikazarmark Could you kindly check it if you have time?

Regards

@xieyuschen xieyuschen closed this Dec 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants