Skip to content

Commit 191c3c1

Browse files
qianlongzttwpayne
authored andcommitted
chore: Use sync.OnceValues reduce duplicate compute
1 parent 23ff368 commit 191c3c1

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

internal/chezmoi/sourcestate.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -1883,7 +1883,7 @@ func (s *SourceState) newFileTargetStateEntryFunc(
18831883
}, nil
18841884
}
18851885
}
1886-
executedContentsFunc := func() ([]byte, error) {
1886+
executedContentsFunc := sync.OnceValues(func() ([]byte, error) {
18871887
contents, err := contentsFunc()
18881888
if err != nil {
18891889
return nil, err
@@ -1899,7 +1899,7 @@ func (s *SourceState) newFileTargetStateEntryFunc(
18991899
}
19001900
}
19011901
return contents, nil
1902-
}
1902+
})
19031903
return &TargetStateFile{
19041904
contentsFunc: executedContentsFunc,
19051905
contentsSHA256Func: lazySHA256(executedContentsFunc),
@@ -1922,7 +1922,7 @@ func (s *SourceState) newModifyTargetStateEntryFunc(
19221922
interpreter *Interpreter,
19231923
) targetStateEntryFunc {
19241924
return func(destSystem System, destAbsPath AbsPath) (TargetStateEntry, error) {
1925-
contentsFunc := func() (contents []byte, err error) {
1925+
contentsFunc := sync.OnceValues(func() (contents []byte, err error) {
19261926
// FIXME this should share code with RealSystem.RunScript
19271927

19281928
// Read the current contents of the target.
@@ -2019,7 +2019,7 @@ func (s *SourceState) newModifyTargetStateEntryFunc(
20192019
cmd.Stderr = os.Stderr
20202020
contents, err = chezmoilog.LogCmdOutput(s.logger, cmd)
20212021
return
2022-
}
2022+
})
20232023
return &TargetStateFile{
20242024
contentsFunc: contentsFunc,
20252025
contentsSHA256Func: lazySHA256(contentsFunc),
@@ -2043,12 +2043,12 @@ func (s *SourceState) newScriptTargetStateEntryFunc(
20432043
sourceRelPath SourceRelPath,
20442044
fileAttr FileAttr,
20452045
targetRelPath RelPath,
2046-
contentsFunc func() ([]byte, error),
2046+
sourceContentsFunc func() ([]byte, error),
20472047
interpreter *Interpreter,
20482048
) targetStateEntryFunc {
20492049
return func(destSystem System, destAbsPath AbsPath) (TargetStateEntry, error) {
2050-
contentsFunc := func() ([]byte, error) {
2051-
contents, err := contentsFunc()
2050+
contentsFunc := sync.OnceValues(func() ([]byte, error) {
2051+
contents, err := sourceContentsFunc()
20522052
if err != nil {
20532053
return nil, err
20542054
}
@@ -2063,7 +2063,7 @@ func (s *SourceState) newScriptTargetStateEntryFunc(
20632063
}
20642064
}
20652065
return contents, nil
2066-
}
2066+
})
20672067
return &TargetStateScript{
20682068
name: targetRelPath,
20692069
contentsFunc: contentsFunc,

0 commit comments

Comments
 (0)