Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(458)

Delta Between Two Patch Sets: src/pkg/fmt/scan.go

Issue 43990043: code review 43990043: fmt: use sync.Pool (Closed)
Left Patch Set: Created 10 years, 3 months ago
Right Patch Set: diff -r 2d9fe00d6ce1 https://go.googlecode.com/hg/ Created 10 years, 3 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Right: Side by side diff | Download
« no previous file with change/comment | « src/pkg/fmt/print.go ('k') | no next file » | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
(no file at all)
1 // Copyright 2010 The Go Authors. All rights reserved. 1 // Copyright 2010 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style 2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file. 3 // license that can be found in the LICENSE file.
4 4
5 package fmt 5 package fmt
6 6
7 import ( 7 import (
8 "errors" 8 "errors"
9 "io" 9 "io"
10 "math" 10 "math"
11 "os" 11 "os"
12 "reflect" 12 "reflect"
13 "strconv" 13 "strconv"
14 "sync"
14 "unicode/utf8" 15 "unicode/utf8"
15 ) 16 )
16 17
17 // runeUnreader is the interface to something that can unread runes. 18 // runeUnreader is the interface to something that can unread runes.
18 // If the object provided to Scan does not satisfy this interface, 19 // If the object provided to Scan does not satisfy this interface,
19 // a local buffer will be used to back up the input, but its contents 20 // a local buffer will be used to back up the input, but its contents
20 // will be lost when Scan returns. 21 // will be lost when Scan returns.
21 type runeUnreader interface { 22 type runeUnreader interface {
22 UnreadRune() error 23 UnreadRune() error
23 } 24 }
(...skipping 349 matching lines...) Expand 10 before | Expand all | Expand 10 after
373 return 374 return
374 } 375 }
375 } 376 }
376 rr, size = utf8.DecodeRune(r.buf[0:n]) 377 rr, size = utf8.DecodeRune(r.buf[0:n])
377 if size < n { // an error 378 if size < n { // an error
378 r.unread(r.buf[size:n]) 379 r.unread(r.buf[size:n])
379 } 380 }
380 return 381 return
381 } 382 }
382 383
383 var ssFree = newCache(func() interface{} { return new(ss) }) 384 var ssFree = sync.Pool{
385 » New: func() interface{} { return new(ss) },
386 }
384 387
385 // newScanState allocates a new ss struct or grab a cached one. 388 // newScanState allocates a new ss struct or grab a cached one.
386 func newScanState(r io.Reader, nlIsSpace, nlIsEnd bool) (s *ss, old ssave) { 389 func newScanState(r io.Reader, nlIsSpace, nlIsEnd bool) (s *ss, old ssave) {
387 // If the reader is a *ss, then we've got a recursive 390 // If the reader is a *ss, then we've got a recursive
388 // call to Scan, so re-use the scan state. 391 // call to Scan, so re-use the scan state.
389 s, ok := r.(*ss) 392 s, ok := r.(*ss)
390 if ok { 393 if ok {
391 old = s.ssave 394 old = s.ssave
392 s.limit = s.argLimit 395 s.limit = s.argLimit
393 s.nlIsEnd = nlIsEnd || s.nlIsEnd 396 s.nlIsEnd = nlIsEnd || s.nlIsEnd
394 s.nlIsSpace = nlIsSpace 397 s.nlIsSpace = nlIsSpace
395 return 398 return
396 } 399 }
397 400
398 » s = ssFree.get().(*ss) 401 » s = ssFree.Get().(*ss)
399 if rr, ok := r.(io.RuneReader); ok { 402 if rr, ok := r.(io.RuneReader); ok {
400 s.rr = rr 403 s.rr = rr
401 } else { 404 } else {
402 s.rr = &readRune{reader: r} 405 s.rr = &readRune{reader: r}
403 } 406 }
404 s.nlIsSpace = nlIsSpace 407 s.nlIsSpace = nlIsSpace
405 s.nlIsEnd = nlIsEnd 408 s.nlIsEnd = nlIsEnd
406 s.prevRune = -1 409 s.prevRune = -1
407 s.peekRune = -1 410 s.peekRune = -1
408 s.atEOF = false 411 s.atEOF = false
(...skipping 11 matching lines...) Expand all
420 if old.validSave { 423 if old.validSave {
421 s.ssave = old 424 s.ssave = old
422 return 425 return
423 } 426 }
424 // Don't hold on to ss structs with large buffers. 427 // Don't hold on to ss structs with large buffers.
425 if cap(s.buf) > 1024 { 428 if cap(s.buf) > 1024 {
426 return 429 return
427 } 430 }
428 s.buf = s.buf[:0] 431 s.buf = s.buf[:0]
429 s.rr = nil 432 s.rr = nil
430 » ssFree.put(s) 433 » ssFree.Put(s)
431 } 434 }
432 435
433 // skipSpace skips spaces and maybe newlines. 436 // skipSpace skips spaces and maybe newlines.
434 func (s *ss) skipSpace(stopAtNewline bool) { 437 func (s *ss) skipSpace(stopAtNewline bool) {
435 for { 438 for {
436 r := s.getRune() 439 r := s.getRune()
437 if r == eof { 440 if r == eof {
438 return 441 return
439 } 442 }
440 if r == '\r' && s.peek("\n") { 443 if r == '\r' && s.peek("\n") {
(...skipping 716 matching lines...) Expand 10 before | Expand all | Expand 10 after
1157 1160
1158 s.scanOne(c, arg) 1161 s.scanOne(c, arg)
1159 numProcessed++ 1162 numProcessed++
1160 s.argLimit = s.limit 1163 s.argLimit = s.limit
1161 } 1164 }
1162 if numProcessed < len(a) { 1165 if numProcessed < len(a) {
1163 s.errorString("too many operands") 1166 s.errorString("too many operands")
1164 } 1167 }
1165 return 1168 return
1166 } 1169 }
LEFTRIGHT

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b