Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
G
gostnops
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
wei.xuan
gostnops
Commits
8732a146
Commit
8732a146
authored
Jan 06, 2020
by
Ming Deng
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'master' of
https://github.com/dubbogo/gost
into dev
parents
cce873e9
a57121fa
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
168 additions
and
105 deletions
+168
-105
README.md
README.md
+3
-3
bytes_pool.go
bytes/bytes_pool.go
+92
-0
bytes_pool_test.go
bytes/bytes_pool_test.go
+57
-0
slice_pool.go
bytes/slice_pool.go
+16
-74
slice_pool_test.go
bytes/slice_pool_test.go
+0
-28
No files found.
README.md
View file @
8732a146
...
@@ -15,10 +15,10 @@ A go sdk for [Apache Dubbo-go](github.com/apache/dubbo-go).
...
@@ -15,10 +15,10 @@ A go sdk for [Apache Dubbo-go](github.com/apache/dubbo-go).
> slice pool
> slice pool
## container
## container
*
gx
queue
*
queue
> Queue
> Queue
*
gx
set
*
set
> HashSet
> HashSet
## math
## math
...
@@ -31,7 +31,7 @@ A go sdk for [Apache Dubbo-go](github.com/apache/dubbo-go).
...
@@ -31,7 +31,7 @@ A go sdk for [Apache Dubbo-go](github.com/apache/dubbo-go).
## strings
## strings
*
IsNil
*
IsNil
> check a var is nil or not.
> check a var is nil or not.
## time
## time
...
...
bytes/bytes_pool.go
0 → 100644
View file @
8732a146
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package
gxbytes
import
(
"sync"
)
// BytesPool hold specific size []byte
type
BytesPool
struct
{
sizes
[]
int
// sizes declare the cap of each slot
slots
[]
sync
.
Pool
length
int
}
var
defaultBytesPool
=
NewBytesPool
([]
int
{
512
,
1
<<
10
,
4
<<
10
,
16
<<
10
,
64
<<
10
})
// NewBytesPool ...
func
NewBytesPool
(
slotSize
[]
int
)
*
BytesPool
{
bp
:=
&
BytesPool
{}
bp
.
sizes
=
slotSize
bp
.
length
=
len
(
bp
.
sizes
)
bp
.
slots
=
make
([]
sync
.
Pool
,
bp
.
length
)
for
i
,
size
:=
range
bp
.
sizes
{
size
:=
size
bp
.
slots
[
i
]
=
sync
.
Pool
{
New
:
func
()
interface
{}
{
buf
:=
make
([]
byte
,
0
,
size
)
return
&
buf
}}
}
return
bp
}
// SetDefaultBytesPool change default pool options
func
SetDefaultBytesPool
(
bp
*
BytesPool
)
{
defaultBytesPool
=
bp
}
func
(
bp
*
BytesPool
)
findIndex
(
size
int
)
int
{
for
i
:=
0
;
i
<
bp
.
length
;
i
++
{
if
bp
.
sizes
[
i
]
>=
size
{
return
i
}
}
return
bp
.
length
}
// AcquireBytes get specific make([]byte, 0, size)
func
(
bp
*
BytesPool
)
AcquireBytes
(
size
int
)
*
[]
byte
{
idx
:=
bp
.
findIndex
(
size
)
if
idx
>=
bp
.
length
{
buf
:=
make
([]
byte
,
0
,
size
)
return
&
buf
}
bufp
:=
bp
.
slots
[
idx
]
.
Get
()
.
(
*
[]
byte
)
buf
:=
(
*
bufp
)[
:
size
]
return
&
buf
}
// ReleaseBytes ...
func
(
bp
*
BytesPool
)
ReleaseBytes
(
bufp
*
[]
byte
)
{
bufCap
:=
cap
(
*
bufp
)
idx
:=
bp
.
findIndex
(
bufCap
)
if
idx
>=
bp
.
length
||
bp
.
sizes
[
idx
]
!=
bufCap
{
return
}
bp
.
slots
[
idx
]
.
Put
(
bufp
)
}
// AcquireBytes called by defaultBytesPool
func
AcquireBytes
(
size
int
)
*
[]
byte
{
return
defaultBytesPool
.
AcquireBytes
(
size
)
}
// ReleaseBytes called by defaultBytesPool
func
ReleaseBytes
(
bufp
*
[]
byte
)
{
defaultBytesPool
.
ReleaseBytes
(
bufp
)
}
bytes/bytes_pool_test.go
0 → 100644
View file @
8732a146
package
gxbytes
import
(
"fmt"
"testing"
)
func
Test_findIndex
(
t
*
testing
.
T
)
{
bp
:=
NewBytesPool
([]
int
{
16
,
4
<<
10
,
16
<<
10
,
32
<<
10
,
64
<<
10
})
type
args
struct
{
size
int
}
tests
:=
[]
struct
{
args
args
want
int
}{
{
args
{
1
},
0
},
{
args
{
15
},
0
},
{
args
{
16
},
0
},
{
args
{
17
},
1
},
{
args
{
4095
},
1
},
{
args
{
4096
},
1
},
{
args
{
4097
},
2
},
{
args
{
16
<<
10
},
2
},
{
args
{
64
<<
10
},
4
},
{
args
{
64
<<
11
},
5
},
}
for
_
,
tt
:=
range
tests
{
t
.
Run
(
fmt
.
Sprint
(
tt
.
args
.
size
),
func
(
t
*
testing
.
T
)
{
if
got
:=
bp
.
findIndex
(
tt
.
args
.
size
);
got
!=
tt
.
want
{
t
.
Errorf
(
"[%v] findIndex() = %v, want %v"
,
tt
.
args
.
size
,
got
,
tt
.
want
)
}
})
}
}
func
BenchmarkAcquireBytesSize32
(
b
*
testing
.
B
)
{
benchmarkAcquireBytes
(
b
,
32
)
}
func
BenchmarkAcquireBytesSize10k
(
b
*
testing
.
B
)
{
benchmarkAcquireBytes
(
b
,
10000
)
}
func
BenchmarkAcquireBytesSize60k
(
b
*
testing
.
B
)
{
benchmarkAcquireBytes
(
b
,
60000
)
}
func
BenchmarkAcquireBytesSize70k
(
b
*
testing
.
B
)
{
benchmarkAcquireBytes
(
b
,
70000
)
}
func
benchmarkAcquireBytes
(
b
*
testing
.
B
,
size
int
)
{
for
i
:=
0
;
i
<
b
.
N
;
i
++
{
bytes
:=
AcquireBytes
(
size
)
ReleaseBytes
(
bytes
)
}
}
func
BenchmarkFindIndexSize8
(
b
*
testing
.
B
)
{
benchmarkfindIndex
(
b
,
8
)
}
func
BenchmarkFindIndexSize60k
(
b
*
testing
.
B
)
{
benchmarkfindIndex
(
b
,
60000
)
}
func
benchmarkfindIndex
(
b
*
testing
.
B
,
size
int
)
{
for
i
:=
0
;
i
<
b
.
N
;
i
++
{
defaultBytesPool
.
findIndex
(
size
)
}
}
bytes/slice_pool.go
View file @
8732a146
...
@@ -17,14 +17,9 @@
...
@@ -17,14 +17,9 @@
package
gxbytes
package
gxbytes
import
(
"sync"
)
const
(
const
(
minShift
=
6
minShift
=
6
maxShift
=
18
maxShift
=
18
errSlot
=
-
1
)
)
var
(
var
(
...
@@ -36,98 +31,45 @@ func init() {
...
@@ -36,98 +31,45 @@ func init() {
}
}
// SlicePool is []byte pools
// SlicePool is []byte pools
// Deprecated
type
SlicePool
struct
{
type
SlicePool
struct
{
minShift
int
BytesPool
minSize
int
maxSize
int
pool
[]
*
sliceSlot
}
type
sliceSlot
struct
{
defaultSize
int
pool
sync
.
Pool
}
}
// newSlicePool returns SlicePool
// newSlicePool returns SlicePool
// Deprecated
// instead by NewBytesPool
func
NewSlicePool
()
*
SlicePool
{
func
NewSlicePool
()
*
SlicePool
{
p
:=
&
SlicePool
{
sizes
:=
make
([]
int
,
0
,
maxShift
-
minShift
+
1
)
minShift
:
minShift
,
for
i
:=
minShift
;
i
<=
maxShift
;
i
++
{
minSize
:
1
<<
minShift
,
sizes
=
append
(
sizes
,
1
<<
uint
(
i
))
maxSize
:
1
<<
maxShift
,
}
}
for
i
:=
0
;
i
<=
maxShift
-
minShift
;
i
++
{
p
:=
&
SlicePool
{
slab
:=
&
sliceSlot
{
*
NewBytesPool
(
sizes
),
defaultSize
:
1
<<
(
uint
)(
i
+
minShift
),
}
p
.
pool
=
append
(
p
.
pool
,
slab
)
}
}
return
p
return
p
}
}
func
(
p
*
SlicePool
)
slot
(
size
int
)
int
{
if
size
>
p
.
maxSize
{
return
errSlot
}
slot
:=
0
shift
:=
0
if
size
>
p
.
minSize
{
size
--
for
size
>
0
{
size
=
size
>>
1
shift
++
}
slot
=
shift
-
p
.
minShift
}
return
slot
}
func
newSlice
(
size
int
)
[]
byte
{
return
make
([]
byte
,
size
)
}
// Get returns *[]byte from SlicePool
// Get returns *[]byte from SlicePool
func
(
p
*
SlicePool
)
Get
(
size
int
)
*
[]
byte
{
func
(
p
*
SlicePool
)
Get
(
size
int
)
*
[]
byte
{
slot
:=
p
.
slot
(
size
)
return
p
.
AcquireBytes
(
size
)
if
slot
==
errSlot
{
b
:=
newSlice
(
size
)
return
&
b
}
v
:=
p
.
pool
[
slot
]
.
pool
.
Get
()
if
v
==
nil
{
b
:=
newSlice
(
p
.
pool
[
slot
]
.
defaultSize
)
b
=
b
[
0
:
size
]
return
&
b
}
b
:=
v
.
(
*
[]
byte
)
*
b
=
(
*
b
)[
0
:
size
]
return
b
}
}
// Put returns *[]byte to SlicePool
// Put returns *[]byte to SlicePool
func
(
p
*
SlicePool
)
Put
(
buf
*
[]
byte
)
{
func
(
p
*
SlicePool
)
Put
(
bufp
*
[]
byte
)
{
if
buf
==
nil
{
p
.
ReleaseBytes
(
bufp
)
return
}
size
:=
cap
(
*
buf
)
slot
:=
p
.
slot
(
size
)
if
slot
==
errSlot
{
return
}
if
size
!=
int
(
p
.
pool
[
slot
]
.
defaultSize
)
{
return
}
p
.
pool
[
slot
]
.
pool
.
Put
(
buf
)
}
}
// GetBytes returns *[]byte from SlicePool
// GetBytes returns *[]byte from SlicePool
// Deprecated
// instead by AcquireBytes
func
GetBytes
(
size
int
)
*
[]
byte
{
func
GetBytes
(
size
int
)
*
[]
byte
{
return
defaultSlicePool
.
Get
(
size
)
return
defaultSlicePool
.
Get
(
size
)
}
}
// PutBytes Put *[]byte to SlicePool
// PutBytes Put *[]byte to SlicePool
// Deprecated
// instead by ReleaseBytes
func
PutBytes
(
buf
*
[]
byte
)
{
func
PutBytes
(
buf
*
[]
byte
)
{
defaultSlicePool
.
Put
(
buf
)
defaultSlicePool
.
Put
(
buf
)
}
}
bytes/slice_pool_test.go
View file @
8732a146
...
@@ -77,31 +77,3 @@ func TestSlicePoolLargeBytes(t *testing.T) {
...
@@ -77,31 +77,3 @@ func TestSlicePoolLargeBytes(t *testing.T) {
pool
.
Put
(
bp
)
pool
.
Put
(
bp
)
}
}
}
}
func
TestBytesSlot
(
t
*
testing
.
T
)
{
pool
:=
NewSlicePool
()
if
pool
.
slot
(
pool
.
minSize
-
1
)
!=
0
{
t
.
Errorf
(
"Expect get the 0 slot"
)
}
if
pool
.
slot
(
pool
.
minSize
)
!=
0
{
t
.
Errorf
(
"Expect get the 0 slot"
)
}
if
pool
.
slot
(
pool
.
minSize
+
1
)
!=
1
{
t
.
Errorf
(
"Expect get the 1 slot"
)
}
if
pool
.
slot
(
pool
.
maxSize
-
1
)
!=
maxShift
-
minShift
{
t
.
Errorf
(
"Expect get the %d slot"
,
maxShift
-
minShift
)
}
if
pool
.
slot
(
pool
.
maxSize
)
!=
maxShift
-
minShift
{
t
.
Errorf
(
"Expect get the %d slot"
,
maxShift
-
minShift
)
}
if
pool
.
slot
(
pool
.
maxSize
+
1
)
!=
errSlot
{
t
.
Errorf
(
"Expect get errSlot"
)
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment