buffer Flashcards

(10 cards)

1
Q

Buffered I/O (bufio package)

A

Reader:

reader := bufio.NewReader(os.Stdin)
input, _ := reader.ReadString('\n')  // Reads until newline

Writer:

writer := bufio.NewWriter(os.Stdout)
writer.WriteString("Buffered output\n")
writer.Flush()  // Don't forget!

Key Benefits:
* Reduces system calls
* Batch operations for efficiency

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Buffered Channels

A

Declaration:

ch := make(chan int, 3)  // Capacity=3

Behavior:

ch <- 1  // Doesn't block
ch <- 2  // Doesn't block
ch <- 3  // Doesn't block
ch <- 4  // Blocks until space available

Use Cases:
* Temporary workload storage
* Producer-consumer patterns

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

bytes.Buffer (Memory Buffer)

A

Usage:

var buf bytes.Buffer
buf.WriteString("Hello")
buf.WriteByte('!')
fmt.Println(buf.String())  // "Hello!"

Key Features:
* Implements io.Writer and io.Reader
* Zero value ready to use
* No need for manual memory management

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Buffer Pool (sync.Pool)

A

Optimization Technique:

var bufferPool = sync.Pool{
    New: func() interface{} {
        return new(bytes.Buffer)
    },
}

// Usage
buf := bufferPool.Get().(*bytes.Buffer)
buf.Reset()  // Reuse
defer bufferPool.Put(buf)
buf.WriteString("reusable")

Benefit: Reduces GC pressure for frequent allocations.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

File Buffering

A

Efficient Copy:

input, _ := os.Open("input.txt")
output, _ := os.Create("output.txt")
defer input.Close()
defer output.Close()

buf := make([]byte, 32*1024)  // 32KB buffer
io.CopyBuffer(output, input, buf)

Optimal Buffer Size: Typically 32KB (varies by system)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Channel Buffer Pitfalls

A

Deadlock Risk:

ch := make(chan int, 1)
ch <- 1
ch <- 2  // Blocks forever (buffer full)

Solution: Always coordinate send/receive patterns.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Custom Buffer Types

A

Ring Buffer:

type RingBuffer struct {
    data  []interface{}
    head  int
    tail  int
    count int
}

func (r *RingBuffer) Push(v interface{}) {
    if r.count == len(r.data) {
        r.resize()
    }
    r.data[r.tail] = v
    r.tail = (r.tail + 1) % len(r.data)
    r.count++
}

Use Case: Fixed-size sliding window of data.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Buffering Best Practices

A
  1. Size Buffers Appropriately
    * Too small → frequent operations
    * Too large → memory waste
  2. Always Flush Writers
writer.Flush()  // bufio, bytes.Buffer, etc.
  1. Reset Buffers for Reuse
buf.Reset()  // bytes.Buffer, sync.Pool buffers
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Benchmark Example

A

Testing Buffer Sizes:

func BenchmarkBufferSizes(b *testing.B) {
    sizes := []int{1, 32, 1024, 32768}
    for _, size := range sizes {
        b.Run(fmt.Sprintf("%d", size), func(b *testing.B) {
            buf := make([]byte, size)
            // Test I/O operations...
        })
    }
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Common Buffer Types Cheat Sheet

A

Type Package Best For
bufio.Reader bufio Efficient text/line reading
bufio.Writer bufio Batched writes
bytes.Buffer bytes In-memory byte manipulation
[]byte slice - Low-level byte operations
Buffered channel chan Producer-consumer workflows

Golden Rule: “Buffer what’s frequent, flush what’s critical.”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly