@@ -1973,17 +1973,27 @@ func Signalfd(fd int, sigmask *Sigset_t, flags int) (newfd int, err error) {
1973
1973
//sys preadv2(fd int, iovs []Iovec, offs_l uintptr, offs_h uintptr, flags int) (n int, err error) = SYS_PREADV2
1974
1974
//sys pwritev2(fd int, iovs []Iovec, offs_l uintptr, offs_h uintptr, flags int) (n int, err error) = SYS_PWRITEV2
1975
1975
1976
- func bytes2iovec (bs [][]byte ) []Iovec {
1977
- iovecs := make ([]Iovec , len (bs ))
1978
- for i , b := range bs {
1979
- iovecs [i ].SetLen (len (b ))
1976
+ // minIovec is the size of the small initial allocation used by
1977
+ // Readv, Writev, etc.
1978
+ //
1979
+ // This small allocation gets stack allocated, which lets the
1980
+ // common use case of len(iovs) <= minIovs avoid more expensive
1981
+ // heap allocations.
1982
+ const minIovec = 8
1983
+
1984
+ // appendBytes converts bs to Iovecs and appends them to vecs.
1985
+ func appendBytes (vecs []Iovec , bs [][]byte ) []Iovec {
1986
+ for _ , b := range bs {
1987
+ var v Iovec
1988
+ v .SetLen (len (b ))
1980
1989
if len (b ) > 0 {
1981
- iovecs [ i ] .Base = & b [0 ]
1990
+ v .Base = & b [0 ]
1982
1991
} else {
1983
- iovecs [ i ] .Base = (* byte )(unsafe .Pointer (& _zero ))
1992
+ v .Base = (* byte )(unsafe .Pointer (& _zero ))
1984
1993
}
1994
+ vecs = append (vecs , v )
1985
1995
}
1986
- return iovecs
1996
+ return vecs
1987
1997
}
1988
1998
1989
1999
// offs2lohi splits offs into its low and high order bits.
@@ -1993,22 +2003,25 @@ func offs2lohi(offs int64) (lo, hi uintptr) {
1993
2003
}
1994
2004
1995
2005
func Readv (fd int , iovs [][]byte ) (n int , err error ) {
1996
- iovecs := bytes2iovec (iovs )
2006
+ iovecs := make ([]Iovec , 0 , minIovec )
2007
+ iovecs = appendBytes (iovecs , iovs )
1997
2008
n , err = readv (fd , iovecs )
1998
2009
readvRacedetect (iovecs , n , err )
1999
2010
return n , err
2000
2011
}
2001
2012
2002
2013
func Preadv (fd int , iovs [][]byte , offset int64 ) (n int , err error ) {
2003
- iovecs := bytes2iovec (iovs )
2014
+ iovecs := make ([]Iovec , 0 , minIovec )
2015
+ iovecs = appendBytes (iovecs , iovs )
2004
2016
lo , hi := offs2lohi (offset )
2005
2017
n , err = preadv (fd , iovecs , lo , hi )
2006
2018
readvRacedetect (iovecs , n , err )
2007
2019
return n , err
2008
2020
}
2009
2021
2010
2022
func Preadv2 (fd int , iovs [][]byte , offset int64 , flags int ) (n int , err error ) {
2011
- iovecs := bytes2iovec (iovs )
2023
+ iovecs := make ([]Iovec , 0 , minIovec )
2024
+ iovecs = appendBytes (iovecs , iovs )
2012
2025
lo , hi := offs2lohi (offset )
2013
2026
n , err = preadv2 (fd , iovecs , lo , hi , flags )
2014
2027
readvRacedetect (iovecs , n , err )
@@ -2035,7 +2048,8 @@ func readvRacedetect(iovecs []Iovec, n int, err error) {
2035
2048
}
2036
2049
2037
2050
func Writev (fd int , iovs [][]byte ) (n int , err error ) {
2038
- iovecs := bytes2iovec (iovs )
2051
+ iovecs := make ([]Iovec , 0 , minIovec )
2052
+ iovecs = appendBytes (iovecs , iovs )
2039
2053
if raceenabled {
2040
2054
raceReleaseMerge (unsafe .Pointer (& ioSync ))
2041
2055
}
@@ -2045,7 +2059,8 @@ func Writev(fd int, iovs [][]byte) (n int, err error) {
2045
2059
}
2046
2060
2047
2061
func Pwritev (fd int , iovs [][]byte , offset int64 ) (n int , err error ) {
2048
- iovecs := bytes2iovec (iovs )
2062
+ iovecs := make ([]Iovec , 0 , minIovec )
2063
+ iovecs = appendBytes (iovecs , iovs )
2049
2064
if raceenabled {
2050
2065
raceReleaseMerge (unsafe .Pointer (& ioSync ))
2051
2066
}
@@ -2056,7 +2071,8 @@ func Pwritev(fd int, iovs [][]byte, offset int64) (n int, err error) {
2056
2071
}
2057
2072
2058
2073
func Pwritev2 (fd int , iovs [][]byte , offset int64 , flags int ) (n int , err error ) {
2059
- iovecs := bytes2iovec (iovs )
2074
+ iovecs := make ([]Iovec , 0 , minIovec )
2075
+ iovecs = appendBytes (iovecs , iovs )
2060
2076
if raceenabled {
2061
2077
raceReleaseMerge (unsafe .Pointer (& ioSync ))
2062
2078
}
0 commit comments