Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
G
golang
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
go
golang
Commits
f9810f1b
Commit
f9810f1b
authored
Dec 02, 2009
by
Rob Pike
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
make io.ReadFile use Stat.Size as a hint for preallocation
R=rsc CC=golang-dev
https://golang.org/cl/163069
parent
49ebcfbb
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
17 additions
and
1 deletion
+17
-1
utils.go
src/pkg/io/utils.go
+17
-1
No files found.
src/pkg/io/utils.go
View file @
f9810f1b
...
...
@@ -26,7 +26,23 @@ func ReadFile(filename string) ([]byte, os.Error) {
return
nil
,
err
}
defer
f
.
Close
();
return
ReadAll
(
f
);
// It's a good but not certain bet that Stat will tell us exactly how much to
// read, so let's try it but be prepared for the answer to be wrong.
dir
,
err
:=
f
.
Stat
();
var
n
uint64
;
if
err
!=
nil
&&
dir
.
Size
<
2e9
{
// Don't preallocate a huge buffer, just in case.
n
=
dir
.
Size
}
if
n
==
0
{
n
=
1024
// No idea what's right, but zero isn't.
}
// Pre-allocate the correct size of buffer, then set its size to zero. The
// Buffer will read into the allocated space cheaply. If the size was wrong,
// we'll either waste some space off the end or reallocate as needed, but
// in the overwhelmingly common case we'll get it just right.
buf
:=
bytes
.
NewBuffer
(
make
([]
byte
,
n
)[
0
:
0
]);
_
,
err
=
Copy
(
buf
,
f
);
return
buf
.
Bytes
(),
err
;
}
// WriteFile writes data to a file named by filename.
...
...
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