Skip to content

Commit

Permalink
phasing out ioutil
Browse files Browse the repository at this point in the history
  • Loading branch information
Dynom committed Feb 23, 2023
1 parent 4b8ad34 commit fb30954
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 22 deletions.
3 changes: 1 addition & 2 deletions cmd/web/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package main

import (
"fmt"
"io/ioutil"
"net/http"
"os"

Expand Down Expand Up @@ -87,7 +86,7 @@ func main() {
func buildConfig(fileName string) (Config, error) {
c := Config{}

b, err := ioutil.ReadFile(fileName)
b, err := os.ReadFile(fileName)
if err != nil {
return c, fmt.Errorf("unable to open %q, reason: %s", fileName, err)
}
Expand Down
12 changes: 6 additions & 6 deletions finder/find_benchmarks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func BenchmarkFindWithBucket(b *testing.B) {
f, _ := New(refs,
WithAlgorithm(alg),
WithLengthTolerance(0),
WithPrefixBuckets(false),
WithPrefixBuckets(true),
)

b.ResetTimer()
Expand All @@ -120,7 +120,7 @@ func BenchmarkFindWithBucket(b *testing.B) {
f, _ := New(refs,
WithAlgorithm(alg),
WithLengthTolerance(0),
WithPrefixBuckets(true),
WithPrefixBuckets(false),
)

b.ResetTimer()
Expand Down Expand Up @@ -166,11 +166,11 @@ func BenchmarkCopyOrAppend(b *testing.B) {
}
})

// "dst smaller copy" can't work, since the result won't contain all items or requires logic which'll make the
// implementation slower than an append
// "dst smaller copy" can't work, since the result won't contain all items or requires logic which makes the
// implementation slower then append

b.Run("dst smaller append", func(b *testing.B) {
refsAppendDst = make([]string, int(numToAllocate/2))
refsAppendDst = make([]string, numToAllocate/2)
b.ResetTimer()
for i := 0; i < b.N; i++ {
refsAppendDst = append(refsAppendSrc[:0:0], refsAppendSrc...)
Expand Down Expand Up @@ -198,7 +198,7 @@ func BenchmarkCopyOrAppend(b *testing.B) {
})

b.Run("dst larger append", func(b *testing.B) {
refsAppendDst = make([]string, int(numToAllocate*2))
refsAppendDst = make([]string, numToAllocate*2)
b.ResetTimer()
for i := 0; i < b.N; i++ {
refsAppendDst = append(refsAppendSrc[:0:0], refsAppendSrc...)
Expand Down
15 changes: 3 additions & 12 deletions server/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"net/http/httptest"
"strconv"
"strings"
"sync"
"testing"

"github.com/sirupsen/logrus/hooks/test"
Expand Down Expand Up @@ -160,37 +159,32 @@ func BenchmarkRequestIDGeneration(b *testing.B) {

b.Run("buffer", func(b *testing.B) {
buf := strings.Builder{}
lock := sync.Mutex{}
buf.Grow(256)
var result string
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
lock.Lock()
buf.Reset()
buf.WriteString("1228958371")
buf.WriteString("-")
buf.WriteString(strconv.Itoa(i))
result = buf.String()
lock.Unlock()
}

b.Logf("Last request ID is: %s", result)
})

b.Run("buffer custom", func(b *testing.B) {
var buf []byte
lock := sync.Mutex{}
buf := make([]byte, 0, 3)
var result string
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
lock.Lock()
buf = []byte{}
buf = buf[0:0]
buf = append(buf, "1228958371"...)
buf = append(buf, "-"...)
buf = append(buf, strconv.Itoa(i)...)
result = string(buf)
lock.Unlock()
}

b.Logf("Last request ID is: %s", result)
Expand All @@ -199,15 +193,12 @@ func BenchmarkRequestIDGeneration(b *testing.B) {
// The fastest (for short strings) and simplest solution
b.Run("string", func(b *testing.B) {
foo := "1228958371"
lock := sync.Mutex{}
var result string

b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
lock.Lock()
result = foo + "-" + strconv.Itoa(i)
lock.Unlock()
}

b.Logf("Last request ID is: %s", result)
Expand Down
3 changes: 1 addition & 2 deletions server/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"encoding/json"
"errors"
"io"
"io/ioutil"
"net/http"
"net/http/pprof"
"time"
Expand Down Expand Up @@ -203,7 +202,7 @@ func getRequestFromHTTPRequest(r *http.Request) (tySugRequest, error) {
return req, ErrMissingBody
}

b, err := ioutil.ReadAll(io.LimitReader(r.Body, maxSizePlusOne))
b, err := io.ReadAll(io.LimitReader(r.Body, maxSizePlusOne))
if err != nil {
if err == io.EOF {
return req, ErrMissingBody
Expand Down

0 comments on commit fb30954

Please sign in to comment.