# 当 GPT-4 拥有了 Diff 视图，那真的是如虎添翼！- 每天5分钟玩转 GPT 编程系列(9)


## 1. 当你要求 GPT-4 帮你写点代码时

如下图所示，我希望 GPT-4 给我的项目 GoPool 添加一个小功能：

![](1.png)

这时候 GPT-4 给出的回答如下：

--- 从这里开始 ---

---

- 用户希望将任务队列的大小从固定值改为可配置的。
- 当前的任务队列大小在 `NewGoPool` 函数中被硬编码为 `1e6`。

为了实现这个需求，我们可以在 `goPool` 结构体中添加一个新的字段 `taskQueueSize`，并在 `NewGoPool` 函数中使用这个字段来初始化 `taskQueue`。同时，我们需要添加一个新的选项函数 `WithTaskQueueSize` 来允许用户在创建 `GoPool` 时设置任务队列的大小。

以下是修改后的代码：

```go path=./gopool.go
package gopool

import (
	"context"
	"sync"
	"time"
)

// GoPool represents a pool of workers.
type GoPool interface {
	// AddTask adds a task to the pool.
	AddTask(t task)
	// Wait waits for all tasks to be dispatched and completed.
	Wait()
	// Release releases the pool and all its workers.
	Release()
	// GetRunning returns the number of running workers.
	Running() int
	// GetWorkerCount returns the number of workers.
	GetWorkerCount() int
}

// task represents a function that will be executed by a worker.
// It returns a result and an error.
type task func() (interface{}, error)

// goPool represents a pool of workers.
type goPool struct {
	workers     []*worker
	workerStack []int
	maxWorkers  int
	// Set by WithMinWorkers(), used to adjust the number of workers. Default equals to maxWorkers.
	minWorkers int
	// tasks are added to this channel first, then dispatched to workers. Default buffer size is 1 million.
	taskQueue chan task
	// Set by WithTaskQueueSize(), used to set the size of the task queue. Default is 1e6.
	taskQueueSize int
	// Set by WithRetryCount(), used to retry a task when it fails. Default is 0.
	retryCount int
	lock       sync.Locker
	cond       *sync.Cond
	// Set by WithTimeout(), used to set a timeout for a task. Default is 0, which means no timeout.
	timeout time.Duration
	// Set by WithResultCallback(), used to handle the result of a task. Default is nil.
	resultCallback func(interface{})
	// Set by WithErrorCallback(), used to handle the error of a task. Default is nil.
	errorCallback func(error)
	// adjustInterval is the interval to adjust the number of workers. Default is 1 second.
	adjustInterval time.Duration
	ctx            context.Context
	// cancel is used to cancel the context. It is called when Release() is called.
	cancel context.CancelFunc
}

// NewGoPool creates a new pool of workers.
func NewGoPool(maxWorkers int, opts ...Option) GoPool {
	ctx, cancel := context.WithCancel(context.Background())
	pool := &goPool{
		maxWorkers: maxWorkers,
		// Set minWorkers to maxWorkers by default
		minWorkers: maxWorkers,
		// workers and workerStack should be initialized after WithMinWorkers() is called
		workers:        nil,
		workerStack:    nil,
		taskQueue:      nil,
		taskQueueSize:  1e6,
		retryCount:     0,
		lock:           new(sync.Mutex),
		timeout:        0,
		adjustInterval: 1 * time.Second,
		ctx:            ctx,
		cancel:         cancel,
	}
	// Apply options
	for _, opt := range opts {
		opt(pool)
	}

	pool.taskQueue = make(chan task, pool.taskQueueSize)

	pool.workers = make([]*worker, pool.minWorkers)
	pool.workerStack = make([]int, pool.minWorkers)

	if pool.cond == nil {
		pool.cond = sync.NewCond(pool.lock)
	}
	// Create workers with the minimum number. Don't use pushWorker() here.
	for i := 0; i < pool.minWorkers; i++ {
		worker := newWorker()
		pool.workers[i] = worker
		pool.workerStack[i] = i
		worker.start(pool, i)
	}
	go pool.adjustWorkers()
	go pool.dispatch()
	return pool
}
```

```go path=./option.go
package gopool

import (
	"sync"
	"time"
)

// Option represents an option for the pool.
type Option func(*goPool)

// WithTaskQueueSize sets the size of the task queue for the pool.
func WithTaskQueueSize(size int) Option {
	return func(p *goPool) {
		p.taskQueueSize = size
	}
}
```

这样，用户就可以在创建 `GoPool` 时通过 `WithTaskQueueSize` 函数来设置任务队列的大小了。例如：

```go
pool := gopool.NewGoPool(100, gopool.WithTaskQueueSize(5000))
```

---

--- 到这里结束 ---

GPT-4 给出的第一段代码有接近100行，这就让我犯难了，我能猜到这里面只有不到10行是更新的内容，但是我怎么才能直观地看到这10行修改，然后判断这10行修改是否符合我的预期呢？我需要在100行里找出10行然后将其贴到我原先200多行的源文件中合适的位置，这听起来一点也不酷。

行，今天的故事就从“让这件事变酷”开始。

## 2. 你需要的背景知识都在这里

容我先介绍下 GoPool 的工作原理以及我想要添加的新功能。

### 2.1 关于 GoPool 和 DevChat

1. [GoPool](https://github.com/devchat-ai/gopool) 是一个高性能，功能完善且简单易用的 Golang worker pool 库；
2. [DevChat](https://www.devchat.ai/?utm_medium=website&utm_source=hutao&utm_content=gpt-gopool-git-diff&utm_campaign=5-minutes) 是一个对接了 GPT-4 的 VS Code 辅助编程插件。

### 2.2 关于 GoPool 的工作原理

GoPool 大概是这样用的：

```go
package main

import (
    "sync"
    "time"

    "github.com/devchat-ai/gopool"
)

func main() {
    pool := gopool.NewGoPool(100)
    defer pool.Release()

    for i := 0; i < 1000; i++ {
        pool.AddTask(func() (interface{}, error){
            time.Sleep(10 * time.Millisecond)
            return nil, nil
        })
    }
    pool.Wait()
}
```

也就是说你可以通过 GoPool 来创建一个 worker pool，然后向这个 pool 中添加任务，GoPool 会自动将任务分配给 worker 来执行。

在 GoPool 的源码中有一个 `goPool` 结构体，其中有2个“队列”，分别是表示 “worker 池”的 `workers` 和表示“任务队列”的 `taskQueue`：

```go
type goPool struct {
	workers     []*worker
    // ......
	// tasks are added to this channel first, then dispatched to workers. Default buffer size is 1 million.
	taskQueue chan task
    // ......
}
```

当调用 `AddTask()` 方法的时候，task 会被先加入到 `taskQueue` 中，然后被 `dispatch()` 函数取出并分配给 worker 来执行。

### 2.3 我想要让 taskQueue 的大小可配置

多数情况下被 `AddTask()` 方法添加到 `taskQueue` 中的 task 会被很快取出并分配给 worker 来执行，顺利的话 `taskQueue` 会被快速“消费”。但是如果 task 的执行时间过长，workers 很繁忙，这时候调用方又一直疯狂地“add task”的话，那么 `taskQueue` 就会被填满，这时候新添加的 task 就会被阻塞。

当然，阻塞本身并没有大问题，疯狂“add task”除了阻塞也没有其他办法（难道丢弃？似乎阻塞相对更合理一些）。这里的问题是如果 `taskQueue` 过大，会不会占用过多的内存？举个例子，如果 task 比较复杂，那么当 `taskQueue` 中有100万个 task 时，那么这100万个 task 就可能会占用过多的内存，这或许不是用户期望的结果。

因此我希望用户可以通过类似 `WithTaskQueueSize()` 的方式来设置 `taskQueue` 的大小，这样用户就可以根据自己的需求来灵活排队的 task 的数量了。

## 3. 用 Diff 视图来看 GPT-4 写的新代码

当你看到这局面的时候，是不是感觉有点迷：

![](2.png)

**这也是我们通过网页版 ChatGPT 写代码的时候会遇到的困境：怎么知道具体改了哪几行？**

这时我才发现 DevChat 里的 view diff 功能太好用了：

![](3.png)

如上图，点一下“View Diff”按钮，这样就知道了 GPT-4 写的代码具体改了哪些地方了。

你还可以通过这个小箭头选择“不接受”某一处修改：

![](4.png)

啥意思呢，就是默认情况下点击 “apply diff” 按钮会应用全部的修改，但是如果你不想应用某一处修改，那么你可以点击这个小箭头。

比如图中的场景，GPT-4 给的代码其实只包含涉及更新的一大段代码，后面还有100行没有给出来，所以直接 “apply diff” 就会导致“后面的100行被删掉”。

所以我们先点击下面这个小箭头，然后再点击 “apply diff” 就可以了：

![](5.png)

接着你也可以通过 VS Code 的“Source Control”面板来检查下修改：

![](6.png)

## 4. 通过 Diff 内容让 GPT-4 总结 Commit Message

当代码写完后，就到了最考验人的“commit message”编写环节了。别再“update”一个单词走天下了，让 GPT-4 来帮你写吧：

![](7.png)

步骤如下：

1. 可以用 IDE 里的 Source Control 相关插件来“Stage Changes”，也可以通过 `git add` 命令来手动添加修改；
2. 然后在 DevChat 窗口里点击“+”，选择“git diff --cached”，这样就能将当前 diff 内容添加到你和 GPT-4 聊天的“上下文”里；
3. 在 DevChat 里发送 commit_message 命令，接着就能让 GPT-4 基于当前的 diff 内容来生成 commit message 了。如下图：

![](8.png)

最后你可以选择直接在 DevChat 窗口里完成 commit，也可以复制这段 commit message 到其他地方进一步编辑，应用：

![](9.png)

> BTW：这次我提交的代码是：[Add configurable task queue size to GoPool](https://github.com/devchat-ai/gopool/pull/15/files)

## 5. 总结

又到一天总结时，今天我们获得的新技能是：

1. 通过 DevChat 的“View Diff”功能来查看 GPT-4 写的代码具体改了哪些地方；
2. 将 “git diff --cached” 内容添加到 GPT-4 聊天上下文，可以让 GPT-4 基于当前的 diff 内容来生成 commit message。

打完收工。

