Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
go
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
go
Commits
0f931181
Commit
0f931181
authored
Dec 18, 2013
by
Brad Fitzpatrick
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fmt: use sync.Pool
Update #4720 R=golang-dev, iant CC=golang-dev
https://golang.org/cl/43990043
parent
8c6ef061
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
10 additions
and
39 deletions
+10
-39
src/pkg/fmt/print.go
src/pkg/fmt/print.go
+4
-36
src/pkg/fmt/scan.go
src/pkg/fmt/scan.go
+6
-3
No files found.
src/pkg/fmt/print.go
View file @
0f931181
...
@@ -124,45 +124,13 @@ type pp struct {
...
@@ -124,45 +124,13 @@ type pp struct {
fmt
fmt
fmt
fmt
}
}
// A cache holds a set of reusable objects.
var
ppFree
=
sync
.
Pool
{
// The slice is a stack (LIFO).
New
:
func
()
interface
{}
{
return
new
(
pp
)
},
// If more are needed, the cache creates them by calling new.
type
cache
struct
{
mu
sync
.
Mutex
saved
[]
interface
{}
new
func
()
interface
{}
}
}
func
(
c
*
cache
)
put
(
x
interface
{})
{
c
.
mu
.
Lock
()
if
len
(
c
.
saved
)
<
cap
(
c
.
saved
)
{
c
.
saved
=
append
(
c
.
saved
,
x
)
}
c
.
mu
.
Unlock
()
}
func
(
c
*
cache
)
get
()
interface
{}
{
c
.
mu
.
Lock
()
n
:=
len
(
c
.
saved
)
if
n
==
0
{
c
.
mu
.
Unlock
()
return
c
.
new
()
}
x
:=
c
.
saved
[
n
-
1
]
c
.
saved
=
c
.
saved
[
0
:
n
-
1
]
c
.
mu
.
Unlock
()
return
x
}
func
newCache
(
f
func
()
interface
{})
*
cache
{
return
&
cache
{
saved
:
make
([]
interface
{},
0
,
100
),
new
:
f
}
}
var
ppFree
=
newCache
(
func
()
interface
{}
{
return
new
(
pp
)
})
// newPrinter allocates a new pp struct or grab a cached one.
// newPrinter allocates a new pp struct or grab a cached one.
func
newPrinter
()
*
pp
{
func
newPrinter
()
*
pp
{
p
:=
ppFree
.
g
et
()
.
(
*
pp
)
p
:=
ppFree
.
G
et
()
.
(
*
pp
)
p
.
panicking
=
false
p
.
panicking
=
false
p
.
erroring
=
false
p
.
erroring
=
false
p
.
fmt
.
init
(
&
p
.
buf
)
p
.
fmt
.
init
(
&
p
.
buf
)
...
@@ -178,7 +146,7 @@ func (p *pp) free() {
...
@@ -178,7 +146,7 @@ func (p *pp) free() {
p
.
buf
=
p
.
buf
[
:
0
]
p
.
buf
=
p
.
buf
[
:
0
]
p
.
arg
=
nil
p
.
arg
=
nil
p
.
value
=
reflect
.
Value
{}
p
.
value
=
reflect
.
Value
{}
ppFree
.
p
ut
(
p
)
ppFree
.
P
ut
(
p
)
}
}
func
(
p
*
pp
)
Width
()
(
wid
int
,
ok
bool
)
{
return
p
.
fmt
.
wid
,
p
.
fmt
.
widPresent
}
func
(
p
*
pp
)
Width
()
(
wid
int
,
ok
bool
)
{
return
p
.
fmt
.
wid
,
p
.
fmt
.
widPresent
}
...
...
src/pkg/fmt/scan.go
View file @
0f931181
...
@@ -11,6 +11,7 @@ import (
...
@@ -11,6 +11,7 @@ import (
"os"
"os"
"reflect"
"reflect"
"strconv"
"strconv"
"sync"
"unicode/utf8"
"unicode/utf8"
)
)
...
@@ -380,7 +381,9 @@ func (r *readRune) ReadRune() (rr rune, size int, err error) {
...
@@ -380,7 +381,9 @@ func (r *readRune) ReadRune() (rr rune, size int, err error) {
return
return
}
}
var
ssFree
=
newCache
(
func
()
interface
{}
{
return
new
(
ss
)
})
var
ssFree
=
sync
.
Pool
{
New
:
func
()
interface
{}
{
return
new
(
ss
)
},
}
// newScanState allocates a new ss struct or grab a cached one.
// newScanState allocates a new ss struct or grab a cached one.
func
newScanState
(
r
io
.
Reader
,
nlIsSpace
,
nlIsEnd
bool
)
(
s
*
ss
,
old
ssave
)
{
func
newScanState
(
r
io
.
Reader
,
nlIsSpace
,
nlIsEnd
bool
)
(
s
*
ss
,
old
ssave
)
{
...
@@ -395,7 +398,7 @@ func newScanState(r io.Reader, nlIsSpace, nlIsEnd bool) (s *ss, old ssave) {
...
@@ -395,7 +398,7 @@ func newScanState(r io.Reader, nlIsSpace, nlIsEnd bool) (s *ss, old ssave) {
return
return
}
}
s
=
ssFree
.
g
et
()
.
(
*
ss
)
s
=
ssFree
.
G
et
()
.
(
*
ss
)
if
rr
,
ok
:=
r
.
(
io
.
RuneReader
);
ok
{
if
rr
,
ok
:=
r
.
(
io
.
RuneReader
);
ok
{
s
.
rr
=
rr
s
.
rr
=
rr
}
else
{
}
else
{
...
@@ -427,7 +430,7 @@ func (s *ss) free(old ssave) {
...
@@ -427,7 +430,7 @@ func (s *ss) free(old ssave) {
}
}
s
.
buf
=
s
.
buf
[
:
0
]
s
.
buf
=
s
.
buf
[
:
0
]
s
.
rr
=
nil
s
.
rr
=
nil
ssFree
.
p
ut
(
s
)
ssFree
.
P
ut
(
s
)
}
}
// skipSpace skips spaces and maybe newlines.
// skipSpace skips spaces and maybe newlines.
...
...
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