Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
ld
go-ld-prime
Commits
7bca6b48
Unverified
Commit
7bca6b48
authored
Apr 09, 2021
by
Eric Myhre
Committed by
GitHub
Apr 09, 2021
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #161 from mvdan/generate-buffer
schema/gen/go: batch file writes via a bytes.Buffer
parents
f3d42e04
88f72f58
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
14 additions
and
5 deletions
+14
-5
schema/gen/go/generate.go
schema/gen/go/generate.go
+14
-5
No files found.
schema/gen/go/generate.go
View file @
7bca6b48
package
gengo
import
(
"bytes"
"fmt"
"io"
"
os
"
"
io/ioutil
"
"path/filepath"
"sort"
...
...
@@ -138,12 +139,20 @@ func Generate(pth string, pkgName string, ts schema.TypeSystem, adjCfg *AdjunctC
}
func
withFile
(
filename
string
,
fn
func
(
io
.
Writer
))
{
f
,
err
:=
os
.
OpenFile
(
filename
,
os
.
O_CREATE
|
os
.
O_TRUNC
|
os
.
O_WRONLY
,
0644
)
if
err
!=
nil
{
// Don't write directly to the file, as that many write syscalls can be
// expensive. Moreover, they can have a knock-on effect on daemons
// watching for file changes. gopls can easily eat CPU for many seconds
// just handling tens of thousands of file writes, for example.
//
// To alleviate both of those problems, write to a buffer first, and
// then write the resulting bytes to disk in a single go.
// A buffer is slightly better than bufio.Writer, as it gets us a bit
// more atomicity via the single write.
buf
:=
new
(
bytes
.
Buffer
)
fn
(
buf
)
if
err
:=
ioutil
.
WriteFile
(
filename
,
buf
.
Bytes
(),
0666
);
err
!=
nil
{
panic
(
err
)
}
defer
f
.
Close
()
fn
(
f
)
}
type
sortableTypeNames
[]
schema
.
TypeName
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment