Skip to content

Commit 1426329

Browse files
committed
性能优化,尝试减少内存分配
1 parent cab8408 commit 1426329

File tree

8 files changed

+190
-175
lines changed

8 files changed

+190
-175
lines changed

FUTURE.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
* [x] 调整 hash 包使用姿势
1515
* [x] 大规模重构,优化代码使用
16-
* [ ] 性能优化,尝试减少内存分配
16+
* [x] 性能优化,尝试减少内存分配
1717
* [ ] 继续完善单元测试,提升覆盖率到 90%
1818

1919
### v0.3.x

HISTORY.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
## 📜 历史版本的特性介绍 (Features in old versions)
22

3+
### v0.4.2-alpha
4+
5+
> 此版本发布于 2024-05-27
6+
7+
* 性能优化,尝试减少内存分配
8+
39
### v0.4.1-alpha
410

511
> 此版本发布于 2024-05-22

README.en.md

Lines changed: 83 additions & 83 deletions
Large diffs are not rendered by default.

README.md

Lines changed: 83 additions & 83 deletions
Large diffs are not rendered by default.

aes/aes.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ func EncryptECB(key cryptox.Bytes, padding cryptox.Padding, bs cryptox.Bytes) (c
2828
return nil, err
2929
}
3030

31+
bs = bs.Clone()
3132
src := padding.Padding(bs, blockSize)
3233
dst := src.Clone()
3334

@@ -80,6 +81,7 @@ func EncryptCBC(key cryptox.Bytes, iv cryptox.Bytes, padding cryptox.Padding, bs
8081
return nil, err
8182
}
8283

84+
bs = bs.Clone()
8385
src := padding.Padding(bs, blockSize)
8486
dst := src.Clone()
8587

@@ -106,6 +108,7 @@ func EncryptCFB(key cryptox.Bytes, iv cryptox.Bytes, padding cryptox.Padding, bs
106108
return nil, err
107109
}
108110

111+
bs = bs.Clone()
109112
src := padding.Padding(bs, blockSize)
110113
dst := src.Clone()
111114

@@ -132,6 +135,7 @@ func EncryptOFB(key cryptox.Bytes, iv cryptox.Bytes, padding cryptox.Padding, bs
132135
return nil, err
133136
}
134137

138+
bs = bs.Clone()
135139
src := padding.Padding(bs, blockSize)
136140
dst := src.Clone()
137141

@@ -158,6 +162,7 @@ func EncryptCTR(key cryptox.Bytes, iv cryptox.Bytes, padding cryptox.Padding, bs
158162
return nil, err
159163
}
160164

165+
bs = bs.Clone()
161166
src := padding.Padding(bs, blockSize)
162167
dst := src.Clone()
163168

des/des.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ func EncryptECB(key cryptox.Bytes, padding cryptox.Padding, bs cryptox.Bytes) (c
2828
return nil, err
2929
}
3030

31+
bs = bs.Clone()
3132
src := padding.Padding(bs, blockSize)
3233
dst := src.Clone()
3334

@@ -80,6 +81,7 @@ func EncryptCBC(key cryptox.Bytes, iv cryptox.Bytes, padding cryptox.Padding, bs
8081
return nil, err
8182
}
8283

84+
bs = bs.Clone()
8385
src := padding.Padding(bs, blockSize)
8486
dst := src.Clone()
8587

@@ -106,6 +108,7 @@ func EncryptCFB(key cryptox.Bytes, iv cryptox.Bytes, padding cryptox.Padding, bs
106108
return nil, err
107109
}
108110

111+
bs = bs.Clone()
109112
src := padding.Padding(bs, blockSize)
110113
dst := src.Clone()
111114

@@ -132,6 +135,7 @@ func EncryptOFB(key cryptox.Bytes, iv cryptox.Bytes, padding cryptox.Padding, bs
132135
return nil, err
133136
}
134137

138+
bs = bs.Clone()
135139
src := padding.Padding(bs, blockSize)
136140
dst := src.Clone()
137141

@@ -158,6 +162,7 @@ func EncryptCTR(key cryptox.Bytes, iv cryptox.Bytes, padding cryptox.Padding, bs
158162
return nil, err
159163
}
160164

165+
bs = bs.Clone()
161166
src := padding.Padding(bs, blockSize)
162167
dst := src.Clone()
163168

des/triple_des.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ func EncryptECBTriple(key cryptox.Bytes, padding cryptox.Padding, bs cryptox.Byt
2828
return nil, err
2929
}
3030

31+
bs = bs.Clone()
3132
src := padding.Padding(bs, blockSize)
3233
dst := src.Clone()
3334

@@ -80,6 +81,7 @@ func EncryptCBCTriple(key cryptox.Bytes, iv cryptox.Bytes, padding cryptox.Paddi
8081
return nil, err
8182
}
8283

84+
bs = bs.Clone()
8385
src := padding.Padding(bs, blockSize)
8486
dst := src.Clone()
8587

@@ -106,6 +108,7 @@ func EncryptCFBTriple(key cryptox.Bytes, iv cryptox.Bytes, padding cryptox.Paddi
106108
return nil, err
107109
}
108110

111+
bs = bs.Clone()
109112
src := padding.Padding(bs, blockSize)
110113
dst := src.Clone()
111114

@@ -132,6 +135,7 @@ func EncryptOFBTriple(key cryptox.Bytes, iv cryptox.Bytes, padding cryptox.Paddi
132135
return nil, err
133136
}
134137

138+
bs = bs.Clone()
135139
src := padding.Padding(bs, blockSize)
136140
dst := src.Clone()
137141

@@ -158,6 +162,7 @@ func EncryptCTRTriple(key cryptox.Bytes, iv cryptox.Bytes, padding cryptox.Paddi
158162
return nil, err
159163
}
160164

165+
bs = bs.Clone()
161166
src := padding.Padding(bs, blockSize)
162167
dst := src.Clone()
163168

padding.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ var (
1414
)
1515

1616
// Padding paddings and undo paddings to a byte slice.
17-
// You should know the returned bytes is always cloned from the passed bytes,
18-
// so they are two different byte slices.
1917
type Padding interface {
2018
Padding(bs Bytes, blockSize int) Bytes
2119
UndoPadding(bs Bytes, blockSize int) (Bytes, error)
@@ -24,17 +22,16 @@ type Padding interface {
2422
type paddingNone struct{}
2523

2624
func (paddingNone) Padding(bs Bytes, blockSize int) Bytes {
27-
return bs.Clone()
25+
return bs
2826
}
2927

3028
func (paddingNone) UndoPadding(bs Bytes, blockSize int) (Bytes, error) {
31-
return bs.Clone(), nil
29+
return bs, nil
3230
}
3331

3432
type paddingZero struct{}
3533

3634
func (paddingZero) Padding(bs Bytes, blockSize int) Bytes {
37-
bs = bs.Clone()
3835
padding := blockSize - (len(bs) % blockSize)
3936

4037
for i := 0; i < padding; i++ {
@@ -45,7 +42,6 @@ func (paddingZero) Padding(bs Bytes, blockSize int) Bytes {
4542
}
4643

4744
func (paddingZero) UndoPadding(bs Bytes, blockSize int) (Bytes, error) {
48-
bs = bs.Clone()
4945
length := len(bs)
5046

5147
var i int
@@ -66,7 +62,6 @@ func (paddingZero) UndoPadding(bs Bytes, blockSize int) (Bytes, error) {
6662
type paddingPKCS7 struct{}
6763

6864
func (paddingPKCS7) Padding(bs Bytes, blockSize int) Bytes {
69-
bs = bs.Clone()
7065
padding := blockSize - (len(bs) % blockSize)
7166

7267
for i := 0; i < padding; i++ {
@@ -77,7 +72,6 @@ func (paddingPKCS7) Padding(bs Bytes, blockSize int) Bytes {
7772
}
7873

7974
func (paddingPKCS7) UndoPadding(bs Bytes, blockSize int) (Bytes, error) {
80-
bs = bs.Clone()
8175
length := len(bs)
8276
number := int(bs[length-1])
8377

0 commit comments

Comments
 (0)