Repository navigation
Expand file tree
/
Copy pathbytespool.go
More file actions
4364 lines (3727 loc) · 130 KB
/
Copy pathbytespool.go
File metadata and controls
4364 lines (3727 loc) · 130 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Code generated by bytespool-gen. DO NOT EDIT.
// Package bytespool implements a series of sync.Pools tailored to safely recycle
// byte slices and bytes.Buffers. It works by defining a number of buckets, each for
// slices or Buffers of a certain range of capacities, and directing the Put and Get
// requests to the appropriate bucket.
// This package is autogenerated to allow to customize the granularity of the buckets
// and the range of supported capacities. This version was generated with the following
// buckets: 256B 512B 1KB 2KB 4KB 8KB 16KB 32KB 64KB 128KB 256KB 512KB 1MB 2MB 4MB 8MB 16MB.
// It is advisable not to grow/extend the byte slices or bytes.Buffer returned by
// bytespool, as this incurs allocations. To avoid this, use bytespool to allocate a new
// object of the desired size, copy the data to the new object and then recycle the
// original object.
package bytespool
import (
"bufio"
"bytes"
"github.com/CAFxX/bytespool/internal"
"io"
"net/http/httputil"
"sync"
)
var buckets = [...]int{256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536, 131072, 262144, 524288, 1048576, 2097152, 4194304, 8388608, 16777216}
var pool256 sync.Pool // *[]byte, 256 <= cap < 512
var poolb256 sync.Pool // *bytes.Buffer, 256 <= Cap < 512
var poolr256 sync.Pool // *bufio.Reader, 256 <= Size < 512
var poolw256 sync.Pool // *bufio.Writer, 256 <= Size < 512
func get256() *[]byte {
p, _ := pool256.Get().(*[]byte)
return p
}
func getb256() *bytes.Buffer {
b, _ := poolb256.Get().(*bytes.Buffer)
return b
}
func getr256() *bufio.Reader {
r, _ := poolr256.Get().(*bufio.Reader)
return r
}
func getw256() *bufio.Writer {
w, _ := poolw256.Get().(*bufio.Writer)
return w
}
func put256(b *[]byte) {
pool256.Put(b)
}
func putb256(b *bytes.Buffer) {
poolb256.Put(b)
}
func putr256(r *bufio.Reader) {
poolr256.Put(r)
}
func putw256(w *bufio.Writer) {
poolw256.Put(w)
}
// GetBytesBuffer256 gets a bytes.Buffer with a capacity of at least 256 bytes.
func GetBytesBuffer256() *bytes.Buffer {
if b := getb256(); b != nil {
return b
}
if p := get256(); p != nil {
return bytes.NewBuffer(internal.Puts(p))
}
return bytes.NewBuffer(make([]byte, 256))
}
// GetBytesSlice256 gets a byte slice with a capacity of at least 256 bytes and length of 256 bytes.
// GetBytesSlice256 is somewhat slower than GetBytesSlicePtr256, but it is significantly easier to use.
func GetBytesSlice256() []byte {
if p := get256(); p != nil {
return internal.Puts(p)
}
if b := getb256(); b != nil {
return internal.Bb2bs(b)
}
p := make([]byte, 256)
return p
}
// GetBytesSlicePtr256 is like GetBytesSlice256 but returns a pointer to the slice instead.
// GetBytesSlicePtr256 is somewhat faster than GetBytesSlice256, but it is significantly more complicated to use, as you need to
// provide to PutBytesSlicePtr256 the original pointer returned by GetBytesSlicePtr256 to recycle the slice.
func GetBytesSlicePtr256() *[]byte {
if p := get256(); p != nil {
return p
}
if b := getb256(); b != nil {
p := internal.Bb2bs(b)
return internal.Gets(p)
}
p := make([]byte, 256)
return &p
}
// GetBufioReader256 returns a bufio.Reader with an internal buffer size of at least 256.
func GetBufioReader256(pr io.Reader) *bufio.Reader {
if r := getr256(); r != nil {
r.Reset(pr)
return r
}
return bufio.NewReaderSize(pr, 256)
}
// GetBufioWriter256 returns a bufio.Writer with an internal buffer size of at least 256.
func GetBufioWriter256(pw io.Writer) *bufio.Writer {
if w := getw256(); w != nil {
w.Reset(pw)
return w
}
return bufio.NewWriterSize(pw, 256)
}
// PutBytesBuffer256 recycles the passed bytes.Buffer. If the bytes.Buffer can not be recycled (e.g. because its capacity
// is too small or too big) false is returned and the bytes.Buffer is unmodified, otherwise the bytes.Buffer is
// recycled and true is returned. In the latter case, the caller should never use again the passed bytes.Buffer.
// PutBytesBuffer256 is optimized for bytes.Buffer of capacity [256, 512) but will accepts other
// sizes as well.
func PutBytesBuffer256(b *bytes.Buffer) bool {
if b == nil {
return false
}
if l := b.Cap(); l < 256 || l >= 512 {
return PutBytesBuffer(b)
}
b.Reset()
putb256(b)
return true
}
// PutBytesSlice256 recycles the passed byte slice. If the byte slice can not be recycled (e.g. because its capacity
// is too small or too big) false is returned and the bytes.Buffer is returned unmodified, otherwise the byte slice is
// recycled and true is returned. In the latter case, the caller should never use again the passed byte slice.
// PutBytesSlice256 is optimized for byte slice of capacity [256, 512) but will accepts other
// sizes as well.
// PutBytesSlice256, contrary to PutBytesSlicePtr256, will perform a pointer-sized allocation for each call.
func PutBytesSlice256(p []byte) bool {
if l := cap(p); l < 256 || l >= 512 {
return PutBytesSlice(p)
}
p = p[0:256]
put256(internal.Gets(p))
return true
}
// PutBytesSlicePtr256 is like PutBytesSlice256, but it accepts a pointer to the byte slice and does not perform
// a pointer-sized allocation for each call.
func PutBytesSlicePtr256(p *[]byte) bool {
if p == nil {
return false
}
if l := cap(*p); l < 256 || l >= 512 {
return PutBytesSlicePtr(p)
}
*p = (*p)[0:256]
put256(p)
return true
}
// PutBufioReader256 recycles the passed bufio.Reader. It returns true if it's recycled, and false otherwise. If it's
// recycled, bufio.Reader.Reset() is called internally, so calling Reset before PutBufioReader256 is not required. If
// it is not recycled, the bufio.Reader is left untouched. After a succesfull call to PutBufioReader256 callers should
// not use the bufio.Reader anymore and drop all references to it.
// PutBufioReader256 is optimized for bufio.Reader with Size() in the range (256, 512], but it accepts
// also other sizes.
func PutBufioReader256(r *bufio.Reader) bool {
if r == nil {
return false
}
if l := r.Size(); l < 256 || l >= 512 {
return PutBufioReader(r)
}
r.Reset(nil) // to not keep the parent reader alive
putr256(r)
return true
}
// PutBufioWriter256 recycles the passed bufio.Writer. It returns true if it's recycled, and false otherwise. If it's
// recycled, bufio.Writer.Reset() is called internally, so calling Reset before PutBufioWriter256 is not required. If
// it is not recycled, the bufio.Writer is left untouched. After a succesfull call to PutBufioWriter256 callers should
// not use the bufio.Writer anymore and drop all references to it.
// PutBufioWriter256 is optimized for bufio.Writer with Size() in the range (256, 512], but it accepts
// also other sizes.
func PutBufioWriter256(w *bufio.Writer) bool {
if w == nil {
return false
}
if l := w.Size(); l < 256 || l >= 512 {
return PutBufioWriter(w)
}
w.Reset(nil) // to not keep the parent writer alive
putw256(w)
return true
}
// BufferPool256 is a httputil.BufferPool that provides byte slices of 256 bytes.
type BufferPool256 struct {
httputil.BufferPool
}
// Get implements httputil.BufferPool.Get
func (_ BufferPool256) Get() []byte {
return GetBytesSlice256()
}
// Put implements httputil.BufferPool.Put
func (_ BufferPool256) Put(b []byte) {
PutBytesSlice256(b)
}
// BufferPtrPool256 is like BufferPool256, but using pointer to byte slices instead (to avoid allocations during Put).
// For this reason it is not compatible with httputil.BufferPool.
type BufferPtrPool256 struct{}
// Get gets a byte slice from the pool. See GetBytesSlicePtr256 for details.
func (_ BufferPtrPool256) Get() *[]byte {
return GetBytesSlicePtr256()
}
// Put inserts a byte slice in the pool. See PutBytesSlicePtr256 for details.
func (_ BufferPtrPool256) Put(b *[]byte) {
PutBytesSlicePtr256(b)
}
var pool512 sync.Pool // *[]byte, 512 <= cap < 1024
var poolb512 sync.Pool // *bytes.Buffer, 512 <= Cap < 1024
var poolr512 sync.Pool // *bufio.Reader, 512 <= Size < 1024
var poolw512 sync.Pool // *bufio.Writer, 512 <= Size < 1024
func get512() *[]byte {
p, _ := pool512.Get().(*[]byte)
return p
}
func getb512() *bytes.Buffer {
b, _ := poolb512.Get().(*bytes.Buffer)
return b
}
func getr512() *bufio.Reader {
r, _ := poolr512.Get().(*bufio.Reader)
return r
}
func getw512() *bufio.Writer {
w, _ := poolw512.Get().(*bufio.Writer)
return w
}
func put512(b *[]byte) {
pool512.Put(b)
}
func putb512(b *bytes.Buffer) {
poolb512.Put(b)
}
func putr512(r *bufio.Reader) {
poolr512.Put(r)
}
func putw512(w *bufio.Writer) {
poolw512.Put(w)
}
// GetBytesBuffer512 gets a bytes.Buffer with a capacity of at least 512 bytes.
func GetBytesBuffer512() *bytes.Buffer {
if b := getb512(); b != nil {
return b
}
if p := get512(); p != nil {
return bytes.NewBuffer(internal.Puts(p))
}
return bytes.NewBuffer(make([]byte, 512))
}
// GetBytesSlice512 gets a byte slice with a capacity of at least 512 bytes and length of 512 bytes.
// GetBytesSlice512 is somewhat slower than GetBytesSlicePtr512, but it is significantly easier to use.
func GetBytesSlice512() []byte {
if p := get512(); p != nil {
return internal.Puts(p)
}
if b := getb512(); b != nil {
return internal.Bb2bs(b)
}
p := make([]byte, 512)
return p
}
// GetBytesSlicePtr512 is like GetBytesSlice512 but returns a pointer to the slice instead.
// GetBytesSlicePtr512 is somewhat faster than GetBytesSlice512, but it is significantly more complicated to use, as you need to
// provide to PutBytesSlicePtr512 the original pointer returned by GetBytesSlicePtr512 to recycle the slice.
func GetBytesSlicePtr512() *[]byte {
if p := get512(); p != nil {
return p
}
if b := getb512(); b != nil {
p := internal.Bb2bs(b)
return internal.Gets(p)
}
p := make([]byte, 512)
return &p
}
// GetBufioReader512 returns a bufio.Reader with an internal buffer size of at least 512.
func GetBufioReader512(pr io.Reader) *bufio.Reader {
if r := getr512(); r != nil {
r.Reset(pr)
return r
}
return bufio.NewReaderSize(pr, 512)
}
// GetBufioWriter512 returns a bufio.Writer with an internal buffer size of at least 512.
func GetBufioWriter512(pw io.Writer) *bufio.Writer {
if w := getw512(); w != nil {
w.Reset(pw)
return w
}
return bufio.NewWriterSize(pw, 512)
}
// PutBytesBuffer512 recycles the passed bytes.Buffer. If the bytes.Buffer can not be recycled (e.g. because its capacity
// is too small or too big) false is returned and the bytes.Buffer is unmodified, otherwise the bytes.Buffer is
// recycled and true is returned. In the latter case, the caller should never use again the passed bytes.Buffer.
// PutBytesBuffer512 is optimized for bytes.Buffer of capacity [512, 1024) but will accepts other
// sizes as well.
func PutBytesBuffer512(b *bytes.Buffer) bool {
if b == nil {
return false
}
if l := b.Cap(); l < 512 || l >= 1024 {
return PutBytesBuffer(b)
}
b.Reset()
putb512(b)
return true
}
// PutBytesSlice512 recycles the passed byte slice. If the byte slice can not be recycled (e.g. because its capacity
// is too small or too big) false is returned and the bytes.Buffer is returned unmodified, otherwise the byte slice is
// recycled and true is returned. In the latter case, the caller should never use again the passed byte slice.
// PutBytesSlice512 is optimized for byte slice of capacity [512, 1024) but will accepts other
// sizes as well.
// PutBytesSlice512, contrary to PutBytesSlicePtr512, will perform a pointer-sized allocation for each call.
func PutBytesSlice512(p []byte) bool {
if l := cap(p); l < 512 || l >= 1024 {
return PutBytesSlice(p)
}
p = p[0:512]
put512(internal.Gets(p))
return true
}
// PutBytesSlicePtr512 is like PutBytesSlice512, but it accepts a pointer to the byte slice and does not perform
// a pointer-sized allocation for each call.
func PutBytesSlicePtr512(p *[]byte) bool {
if p == nil {
return false
}
if l := cap(*p); l < 512 || l >= 1024 {
return PutBytesSlicePtr(p)
}
*p = (*p)[0:512]
put512(p)
return true
}
// PutBufioReader512 recycles the passed bufio.Reader. It returns true if it's recycled, and false otherwise. If it's
// recycled, bufio.Reader.Reset() is called internally, so calling Reset before PutBufioReader512 is not required. If
// it is not recycled, the bufio.Reader is left untouched. After a succesfull call to PutBufioReader512 callers should
// not use the bufio.Reader anymore and drop all references to it.
// PutBufioReader512 is optimized for bufio.Reader with Size() in the range (512, 1024], but it accepts
// also other sizes.
func PutBufioReader512(r *bufio.Reader) bool {
if r == nil {
return false
}
if l := r.Size(); l < 512 || l >= 1024 {
return PutBufioReader(r)
}
r.Reset(nil) // to not keep the parent reader alive
putr512(r)
return true
}
// PutBufioWriter512 recycles the passed bufio.Writer. It returns true if it's recycled, and false otherwise. If it's
// recycled, bufio.Writer.Reset() is called internally, so calling Reset before PutBufioWriter512 is not required. If
// it is not recycled, the bufio.Writer is left untouched. After a succesfull call to PutBufioWriter512 callers should
// not use the bufio.Writer anymore and drop all references to it.
// PutBufioWriter512 is optimized for bufio.Writer with Size() in the range (512, 1024], but it accepts
// also other sizes.
func PutBufioWriter512(w *bufio.Writer) bool {
if w == nil {
return false
}
if l := w.Size(); l < 512 || l >= 1024 {
return PutBufioWriter(w)
}
w.Reset(nil) // to not keep the parent writer alive
putw512(w)
return true
}
// BufferPool512 is a httputil.BufferPool that provides byte slices of 512 bytes.
type BufferPool512 struct {
httputil.BufferPool
}
// Get implements httputil.BufferPool.Get
func (_ BufferPool512) Get() []byte {
return GetBytesSlice512()
}
// Put implements httputil.BufferPool.Put
func (_ BufferPool512) Put(b []byte) {
PutBytesSlice512(b)
}
// BufferPtrPool512 is like BufferPool512, but using pointer to byte slices instead (to avoid allocations during Put).
// For this reason it is not compatible with httputil.BufferPool.
type BufferPtrPool512 struct{}
// Get gets a byte slice from the pool. See GetBytesSlicePtr512 for details.
func (_ BufferPtrPool512) Get() *[]byte {
return GetBytesSlicePtr512()
}
// Put inserts a byte slice in the pool. See PutBytesSlicePtr512 for details.
func (_ BufferPtrPool512) Put(b *[]byte) {
PutBytesSlicePtr512(b)
}
var pool1K sync.Pool // *[]byte, 1024 <= cap < 2048
var poolb1K sync.Pool // *bytes.Buffer, 1024 <= Cap < 2048
var poolr1K sync.Pool // *bufio.Reader, 1024 <= Size < 2048
var poolw1K sync.Pool // *bufio.Writer, 1024 <= Size < 2048
func get1K() *[]byte {
p, _ := pool1K.Get().(*[]byte)
return p
}
func getb1K() *bytes.Buffer {
b, _ := poolb1K.Get().(*bytes.Buffer)
return b
}
func getr1K() *bufio.Reader {
r, _ := poolr1K.Get().(*bufio.Reader)
return r
}
func getw1K() *bufio.Writer {
w, _ := poolw1K.Get().(*bufio.Writer)
return w
}
func put1K(b *[]byte) {
pool1K.Put(b)
}
func putb1K(b *bytes.Buffer) {
poolb1K.Put(b)
}
func putr1K(r *bufio.Reader) {
poolr1K.Put(r)
}
func putw1K(w *bufio.Writer) {
poolw1K.Put(w)
}
// GetBytesBuffer1K gets a bytes.Buffer with a capacity of at least 1K bytes.
func GetBytesBuffer1K() *bytes.Buffer {
if b := getb1K(); b != nil {
return b
}
if p := get1K(); p != nil {
return bytes.NewBuffer(internal.Puts(p))
}
return bytes.NewBuffer(make([]byte, 1024))
}
// GetBytesSlice1K gets a byte slice with a capacity of at least 1K bytes and length of 1K bytes.
// GetBytesSlice1K is somewhat slower than GetBytesSlicePtr1K, but it is significantly easier to use.
func GetBytesSlice1K() []byte {
if p := get1K(); p != nil {
return internal.Puts(p)
}
if b := getb1K(); b != nil {
return internal.Bb2bs(b)
}
p := make([]byte, 1024)
return p
}
// GetBytesSlicePtr1K is like GetBytesSlice1K but returns a pointer to the slice instead.
// GetBytesSlicePtr1K is somewhat faster than GetBytesSlice1K, but it is significantly more complicated to use, as you need to
// provide to PutBytesSlicePtr1K the original pointer returned by GetBytesSlicePtr1K to recycle the slice.
func GetBytesSlicePtr1K() *[]byte {
if p := get1K(); p != nil {
return p
}
if b := getb1K(); b != nil {
p := internal.Bb2bs(b)
return internal.Gets(p)
}
p := make([]byte, 1024)
return &p
}
// GetBufioReader1K returns a bufio.Reader with an internal buffer size of at least 1024.
func GetBufioReader1K(pr io.Reader) *bufio.Reader {
if r := getr1K(); r != nil {
r.Reset(pr)
return r
}
return bufio.NewReaderSize(pr, 1024)
}
// GetBufioWriter1K returns a bufio.Writer with an internal buffer size of at least 1024.
func GetBufioWriter1K(pw io.Writer) *bufio.Writer {
if w := getw1K(); w != nil {
w.Reset(pw)
return w
}
return bufio.NewWriterSize(pw, 1024)
}
// PutBytesBuffer1K recycles the passed bytes.Buffer. If the bytes.Buffer can not be recycled (e.g. because its capacity
// is too small or too big) false is returned and the bytes.Buffer is unmodified, otherwise the bytes.Buffer is
// recycled and true is returned. In the latter case, the caller should never use again the passed bytes.Buffer.
// PutBytesBuffer1K is optimized for bytes.Buffer of capacity [1024, 2048) but will accepts other
// sizes as well.
func PutBytesBuffer1K(b *bytes.Buffer) bool {
if b == nil {
return false
}
if l := b.Cap(); l < 1024 || l >= 2048 {
return PutBytesBuffer(b)
}
b.Reset()
putb1K(b)
return true
}
// PutBytesSlice1K recycles the passed byte slice. If the byte slice can not be recycled (e.g. because its capacity
// is too small or too big) false is returned and the bytes.Buffer is returned unmodified, otherwise the byte slice is
// recycled and true is returned. In the latter case, the caller should never use again the passed byte slice.
// PutBytesSlice1K is optimized for byte slice of capacity [1024, 2048) but will accepts other
// sizes as well.
// PutBytesSlice1K, contrary to PutBytesSlicePtr1K, will perform a pointer-sized allocation for each call.
func PutBytesSlice1K(p []byte) bool {
if l := cap(p); l < 1024 || l >= 2048 {
return PutBytesSlice(p)
}
p = p[0:1024]
put1K(internal.Gets(p))
return true
}
// PutBytesSlicePtr1K is like PutBytesSlice1K, but it accepts a pointer to the byte slice and does not perform
// a pointer-sized allocation for each call.
func PutBytesSlicePtr1K(p *[]byte) bool {
if p == nil {
return false
}
if l := cap(*p); l < 1024 || l >= 2048 {
return PutBytesSlicePtr(p)
}
*p = (*p)[0:1024]
put1K(p)
return true
}
// PutBufioReader1K recycles the passed bufio.Reader. It returns true if it's recycled, and false otherwise. If it's
// recycled, bufio.Reader.Reset() is called internally, so calling Reset before PutBufioReader1K is not required. If
// it is not recycled, the bufio.Reader is left untouched. After a succesfull call to PutBufioReader1K callers should
// not use the bufio.Reader anymore and drop all references to it.
// PutBufioReader1K is optimized for bufio.Reader with Size() in the range (1024, 2048], but it accepts
// also other sizes.
func PutBufioReader1K(r *bufio.Reader) bool {
if r == nil {
return false
}
if l := r.Size(); l < 1024 || l >= 2048 {
return PutBufioReader(r)
}
r.Reset(nil) // to not keep the parent reader alive
putr1K(r)
return true
}
// PutBufioWriter1K recycles the passed bufio.Writer. It returns true if it's recycled, and false otherwise. If it's
// recycled, bufio.Writer.Reset() is called internally, so calling Reset before PutBufioWriter1K is not required. If
// it is not recycled, the bufio.Writer is left untouched. After a succesfull call to PutBufioWriter1K callers should
// not use the bufio.Writer anymore and drop all references to it.
// PutBufioWriter1K is optimized for bufio.Writer with Size() in the range (1024, 2048], but it accepts
// also other sizes.
func PutBufioWriter1K(w *bufio.Writer) bool {
if w == nil {
return false
}
if l := w.Size(); l < 1024 || l >= 2048 {
return PutBufioWriter(w)
}
w.Reset(nil) // to not keep the parent writer alive
putw1K(w)
return true
}
// BufferPool1K is a httputil.BufferPool that provides byte slices of 1K bytes.
type BufferPool1K struct {
httputil.BufferPool
}
// Get implements httputil.BufferPool.Get
func (_ BufferPool1K) Get() []byte {
return GetBytesSlice1K()
}
// Put implements httputil.BufferPool.Put
func (_ BufferPool1K) Put(b []byte) {
PutBytesSlice1K(b)
}
// BufferPtrPool1K is like BufferPool1K, but using pointer to byte slices instead (to avoid allocations during Put).
// For this reason it is not compatible with httputil.BufferPool.
type BufferPtrPool1K struct{}
// Get gets a byte slice from the pool. See GetBytesSlicePtr1K for details.
func (_ BufferPtrPool1K) Get() *[]byte {
return GetBytesSlicePtr1K()
}
// Put inserts a byte slice in the pool. See PutBytesSlicePtr1K for details.
func (_ BufferPtrPool1K) Put(b *[]byte) {
PutBytesSlicePtr1K(b)
}
var pool2K sync.Pool // *[]byte, 2048 <= cap < 4096
var poolb2K sync.Pool // *bytes.Buffer, 2048 <= Cap < 4096
var poolr2K sync.Pool // *bufio.Reader, 2048 <= Size < 4096
var poolw2K sync.Pool // *bufio.Writer, 2048 <= Size < 4096
func get2K() *[]byte {
p, _ := pool2K.Get().(*[]byte)
return p
}
func getb2K() *bytes.Buffer {
b, _ := poolb2K.Get().(*bytes.Buffer)
return b
}
func getr2K() *bufio.Reader {
r, _ := poolr2K.Get().(*bufio.Reader)
return r
}
func getw2K() *bufio.Writer {
w, _ := poolw2K.Get().(*bufio.Writer)
return w
}
func put2K(b *[]byte) {
pool2K.Put(b)
}
func putb2K(b *bytes.Buffer) {
poolb2K.Put(b)
}
func putr2K(r *bufio.Reader) {
poolr2K.Put(r)
}
func putw2K(w *bufio.Writer) {
poolw2K.Put(w)
}
// GetBytesBuffer2K gets a bytes.Buffer with a capacity of at least 2K bytes.
func GetBytesBuffer2K() *bytes.Buffer {
if b := getb2K(); b != nil {
return b
}
if p := get2K(); p != nil {
return bytes.NewBuffer(internal.Puts(p))
}
return bytes.NewBuffer(make([]byte, 2048))
}
// GetBytesSlice2K gets a byte slice with a capacity of at least 2K bytes and length of 2K bytes.
// GetBytesSlice2K is somewhat slower than GetBytesSlicePtr2K, but it is significantly easier to use.
func GetBytesSlice2K() []byte {
if p := get2K(); p != nil {
return internal.Puts(p)
}
if b := getb2K(); b != nil {
return internal.Bb2bs(b)
}
p := make([]byte, 2048)
return p
}
// GetBytesSlicePtr2K is like GetBytesSlice2K but returns a pointer to the slice instead.
// GetBytesSlicePtr2K is somewhat faster than GetBytesSlice2K, but it is significantly more complicated to use, as you need to
// provide to PutBytesSlicePtr2K the original pointer returned by GetBytesSlicePtr2K to recycle the slice.
func GetBytesSlicePtr2K() *[]byte {
if p := get2K(); p != nil {
return p
}
if b := getb2K(); b != nil {
p := internal.Bb2bs(b)
return internal.Gets(p)
}
p := make([]byte, 2048)
return &p
}
// GetBufioReader2K returns a bufio.Reader with an internal buffer size of at least 2048.
func GetBufioReader2K(pr io.Reader) *bufio.Reader {
if r := getr2K(); r != nil {
r.Reset(pr)
return r
}
return bufio.NewReaderSize(pr, 2048)
}
// GetBufioWriter2K returns a bufio.Writer with an internal buffer size of at least 2048.
func GetBufioWriter2K(pw io.Writer) *bufio.Writer {
if w := getw2K(); w != nil {
w.Reset(pw)
return w
}
return bufio.NewWriterSize(pw, 2048)
}
// PutBytesBuffer2K recycles the passed bytes.Buffer. If the bytes.Buffer can not be recycled (e.g. because its capacity
// is too small or too big) false is returned and the bytes.Buffer is unmodified, otherwise the bytes.Buffer is
// recycled and true is returned. In the latter case, the caller should never use again the passed bytes.Buffer.
// PutBytesBuffer2K is optimized for bytes.Buffer of capacity [2048, 4096) but will accepts other
// sizes as well.
func PutBytesBuffer2K(b *bytes.Buffer) bool {
if b == nil {
return false
}
if l := b.Cap(); l < 2048 || l >= 4096 {
return PutBytesBuffer(b)
}
b.Reset()
putb2K(b)
return true
}
// PutBytesSlice2K recycles the passed byte slice. If the byte slice can not be recycled (e.g. because its capacity
// is too small or too big) false is returned and the bytes.Buffer is returned unmodified, otherwise the byte slice is
// recycled and true is returned. In the latter case, the caller should never use again the passed byte slice.
// PutBytesSlice2K is optimized for byte slice of capacity [2048, 4096) but will accepts other
// sizes as well.
// PutBytesSlice2K, contrary to PutBytesSlicePtr2K, will perform a pointer-sized allocation for each call.
func PutBytesSlice2K(p []byte) bool {
if l := cap(p); l < 2048 || l >= 4096 {
return PutBytesSlice(p)
}
p = p[0:2048]
put2K(internal.Gets(p))
return true
}
// PutBytesSlicePtr2K is like PutBytesSlice2K, but it accepts a pointer to the byte slice and does not perform
// a pointer-sized allocation for each call.
func PutBytesSlicePtr2K(p *[]byte) bool {
if p == nil {
return false
}
if l := cap(*p); l < 2048 || l >= 4096 {
return PutBytesSlicePtr(p)
}
*p = (*p)[0:2048]
put2K(p)
return true
}
// PutBufioReader2K recycles the passed bufio.Reader. It returns true if it's recycled, and false otherwise. If it's
// recycled, bufio.Reader.Reset() is called internally, so calling Reset before PutBufioReader2K is not required. If
// it is not recycled, the bufio.Reader is left untouched. After a succesfull call to PutBufioReader2K callers should
// not use the bufio.Reader anymore and drop all references to it.
// PutBufioReader2K is optimized for bufio.Reader with Size() in the range (2048, 4096], but it accepts
// also other sizes.
func PutBufioReader2K(r *bufio.Reader) bool {
if r == nil {
return false
}
if l := r.Size(); l < 2048 || l >= 4096 {
return PutBufioReader(r)
}
r.Reset(nil) // to not keep the parent reader alive
putr2K(r)
return true
}
// PutBufioWriter2K recycles the passed bufio.Writer. It returns true if it's recycled, and false otherwise. If it's
// recycled, bufio.Writer.Reset() is called internally, so calling Reset before PutBufioWriter2K is not required. If
// it is not recycled, the bufio.Writer is left untouched. After a succesfull call to PutBufioWriter2K callers should
// not use the bufio.Writer anymore and drop all references to it.
// PutBufioWriter2K is optimized for bufio.Writer with Size() in the range (2048, 4096], but it accepts
// also other sizes.
func PutBufioWriter2K(w *bufio.Writer) bool {
if w == nil {
return false
}
if l := w.Size(); l < 2048 || l >= 4096 {
return PutBufioWriter(w)
}
w.Reset(nil) // to not keep the parent writer alive
putw2K(w)
return true
}
// BufferPool2K is a httputil.BufferPool that provides byte slices of 2K bytes.
type BufferPool2K struct {
httputil.BufferPool
}
// Get implements httputil.BufferPool.Get
func (_ BufferPool2K) Get() []byte {
return GetBytesSlice2K()
}
// Put implements httputil.BufferPool.Put
func (_ BufferPool2K) Put(b []byte) {
PutBytesSlice2K(b)
}
// BufferPtrPool2K is like BufferPool2K, but using pointer to byte slices instead (to avoid allocations during Put).
// For this reason it is not compatible with httputil.BufferPool.
type BufferPtrPool2K struct{}
// Get gets a byte slice from the pool. See GetBytesSlicePtr2K for details.
func (_ BufferPtrPool2K) Get() *[]byte {
return GetBytesSlicePtr2K()
}
// Put inserts a byte slice in the pool. See PutBytesSlicePtr2K for details.
func (_ BufferPtrPool2K) Put(b *[]byte) {
PutBytesSlicePtr2K(b)
}
var pool4K sync.Pool // *[]byte, 4096 <= cap < 8192
var poolb4K sync.Pool // *bytes.Buffer, 4096 <= Cap < 8192
var poolr4K sync.Pool // *bufio.Reader, 4096 <= Size < 8192
var poolw4K sync.Pool // *bufio.Writer, 4096 <= Size < 8192
func get4K() *[]byte {
p, _ := pool4K.Get().(*[]byte)
return p
}
func getb4K() *bytes.Buffer {
b, _ := poolb4K.Get().(*bytes.Buffer)
return b
}
func getr4K() *bufio.Reader {
r, _ := poolr4K.Get().(*bufio.Reader)
return r
}
func getw4K() *bufio.Writer {
w, _ := poolw4K.Get().(*bufio.Writer)
return w
}
func put4K(b *[]byte) {
pool4K.Put(b)
}
func putb4K(b *bytes.Buffer) {
poolb4K.Put(b)
}
func putr4K(r *bufio.Reader) {
poolr4K.Put(r)
}
func putw4K(w *bufio.Writer) {
poolw4K.Put(w)
}
// GetBytesBuffer4K gets a bytes.Buffer with a capacity of at least 4K bytes.
func GetBytesBuffer4K() *bytes.Buffer {
if b := getb4K(); b != nil {
return b
}
if p := get4K(); p != nil {
return bytes.NewBuffer(internal.Puts(p))
}
return bytes.NewBuffer(make([]byte, 4096))
}
// GetBytesSlice4K gets a byte slice with a capacity of at least 4K bytes and length of 4K bytes.
// GetBytesSlice4K is somewhat slower than GetBytesSlicePtr4K, but it is significantly easier to use.
func GetBytesSlice4K() []byte {
if p := get4K(); p != nil {
return internal.Puts(p)
}
if b := getb4K(); b != nil {
return internal.Bb2bs(b)
}
p := make([]byte, 4096)
return p
}
// GetBytesSlicePtr4K is like GetBytesSlice4K but returns a pointer to the slice instead.
// GetBytesSlicePtr4K is somewhat faster than GetBytesSlice4K, but it is significantly more complicated to use, as you need to
// provide to PutBytesSlicePtr4K the original pointer returned by GetBytesSlicePtr4K to recycle the slice.
func GetBytesSlicePtr4K() *[]byte {
if p := get4K(); p != nil {
return p
}
if b := getb4K(); b != nil {
p := internal.Bb2bs(b)
return internal.Gets(p)
}
p := make([]byte, 4096)
return &p
}
// GetBufioReader4K returns a bufio.Reader with an internal buffer size of at least 4096.
func GetBufioReader4K(pr io.Reader) *bufio.Reader {
if r := getr4K(); r != nil {
r.Reset(pr)
return r
}
return bufio.NewReaderSize(pr, 4096)
}
// GetBufioWriter4K returns a bufio.Writer with an internal buffer size of at least 4096.
func GetBufioWriter4K(pw io.Writer) *bufio.Writer {
if w := getw4K(); w != nil {
w.Reset(pw)
return w
}
return bufio.NewWriterSize(pw, 4096)
}
// PutBytesBuffer4K recycles the passed bytes.Buffer. If the bytes.Buffer can not be recycled (e.g. because its capacity
// is too small or too big) false is returned and the bytes.Buffer is unmodified, otherwise the bytes.Buffer is
// recycled and true is returned. In the latter case, the caller should never use again the passed bytes.Buffer.
// PutBytesBuffer4K is optimized for bytes.Buffer of capacity [4096, 8192) but will accepts other
// sizes as well.
func PutBytesBuffer4K(b *bytes.Buffer) bool {
if b == nil {
return false
}
if l := b.Cap(); l < 4096 || l >= 8192 {
return PutBytesBuffer(b)
}
b.Reset()
putb4K(b)
return true
}
// PutBytesSlice4K recycles the passed byte slice. If the byte slice can not be recycled (e.g. because its capacity
// is too small or too big) false is returned and the bytes.Buffer is returned unmodified, otherwise the byte slice is
// recycled and true is returned. In the latter case, the caller should never use again the passed byte slice.
// PutBytesSlice4K is optimized for byte slice of capacity [4096, 8192) but will accepts other
// sizes as well.
// PutBytesSlice4K, contrary to PutBytesSlicePtr4K, will perform a pointer-sized allocation for each call.
func PutBytesSlice4K(p []byte) bool {
if l := cap(p); l < 4096 || l >= 8192 {
return PutBytesSlice(p)
}
p = p[0:4096]
put4K(internal.Gets(p))
return true
}
// PutBytesSlicePtr4K is like PutBytesSlice4K, but it accepts a pointer to the byte slice and does not perform
// a pointer-sized allocation for each call.
func PutBytesSlicePtr4K(p *[]byte) bool {
if p == nil {
return false
}
if l := cap(*p); l < 4096 || l >= 8192 {
return PutBytesSlicePtr(p)