Pull request 1906: 5896-safe-browsing-cpu-ram
Updates #5896. Squashed commit of the following: commit 81ac59e2f95ef3ad6ac5c4668c07c35d69570454 Author: Ainar Garipov <A.Garipov@AdGuard.COM> Date: Tue Jul 4 16:07:33 2023 +0300 hashprefix: fix cache coding
This commit is contained in:
parent
97af062f7b
commit
c46516475d
|
@ -23,6 +23,13 @@ See also the [v0.107.34 GitHub milestone][ms-v0.107.34].
|
||||||
NOTE: Add new changes BELOW THIS COMMENT.
|
NOTE: Add new changes BELOW THIS COMMENT.
|
||||||
-->
|
-->
|
||||||
|
|
||||||
|
### Fixed
|
||||||
|
|
||||||
|
- Excessive RAM and CPU consumption by Safe Browsing and Parental Control
|
||||||
|
filters ([#5896]).
|
||||||
|
|
||||||
|
[#5896]: https://github.com/AdguardTeam/AdGuardHome/issues/5896
|
||||||
|
|
||||||
<!--
|
<!--
|
||||||
NOTE: Add new changes ABOVE THIS COMMENT.
|
NOTE: Add new changes ABOVE THIS COMMENT.
|
||||||
-->
|
-->
|
||||||
|
|
|
@ -25,7 +25,7 @@ func toCacheItem(data []byte) *cacheItem {
|
||||||
t := time.Unix(int64(binary.BigEndian.Uint64(data)), 0)
|
t := time.Unix(int64(binary.BigEndian.Uint64(data)), 0)
|
||||||
|
|
||||||
data = data[expirySize:]
|
data = data[expirySize:]
|
||||||
hashes := make([]hostnameHash, len(data)/hashSize)
|
hashes := make([]hostnameHash, 0, len(data)/hashSize)
|
||||||
|
|
||||||
for i := 0; i < len(data); i += hashSize {
|
for i := 0; i < len(data); i += hashSize {
|
||||||
var hash hostnameHash
|
var hash hostnameHash
|
||||||
|
@ -41,9 +41,10 @@ func toCacheItem(data []byte) *cacheItem {
|
||||||
|
|
||||||
// fromCacheItem encodes cacheItem into data.
|
// fromCacheItem encodes cacheItem into data.
|
||||||
func fromCacheItem(item *cacheItem) (data []byte) {
|
func fromCacheItem(item *cacheItem) (data []byte) {
|
||||||
data = make([]byte, len(item.hashes)*hashSize+expirySize)
|
data = make([]byte, 0, len(item.hashes)*hashSize+expirySize)
|
||||||
|
|
||||||
expiry := item.expiry.Unix()
|
expiry := item.expiry.Unix()
|
||||||
binary.BigEndian.PutUint64(data[:expirySize], uint64(expiry))
|
data = binary.BigEndian.AppendUint64(data, uint64(expiry))
|
||||||
|
|
||||||
for _, v := range item.hashes {
|
for _, v := range item.hashes {
|
||||||
// nolint:looppointer // The subsilce is used for a copy.
|
// nolint:looppointer // The subsilce is used for a copy.
|
||||||
|
|
|
@ -0,0 +1,44 @@
|
||||||
|
package hashprefix
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestCacheItem(t *testing.T) {
|
||||||
|
item := &cacheItem{
|
||||||
|
expiry: time.Unix(0x01_23_45_67_89_AB_CD_EF, 0),
|
||||||
|
hashes: []hostnameHash{{
|
||||||
|
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
|
||||||
|
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
|
||||||
|
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
|
||||||
|
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
|
||||||
|
}, {
|
||||||
|
0x01, 0x03, 0x05, 0x07, 0x02, 0x04, 0x06, 0x08,
|
||||||
|
0x01, 0x03, 0x05, 0x07, 0x02, 0x04, 0x06, 0x08,
|
||||||
|
0x01, 0x03, 0x05, 0x07, 0x02, 0x04, 0x06, 0x08,
|
||||||
|
0x01, 0x03, 0x05, 0x07, 0x02, 0x04, 0x06, 0x08,
|
||||||
|
}},
|
||||||
|
}
|
||||||
|
|
||||||
|
wantData := []byte{
|
||||||
|
0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF,
|
||||||
|
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
|
||||||
|
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
|
||||||
|
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
|
||||||
|
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
|
||||||
|
0x01, 0x03, 0x05, 0x07, 0x02, 0x04, 0x06, 0x08,
|
||||||
|
0x01, 0x03, 0x05, 0x07, 0x02, 0x04, 0x06, 0x08,
|
||||||
|
0x01, 0x03, 0x05, 0x07, 0x02, 0x04, 0x06, 0x08,
|
||||||
|
0x01, 0x03, 0x05, 0x07, 0x02, 0x04, 0x06, 0x08,
|
||||||
|
}
|
||||||
|
|
||||||
|
gotData := fromCacheItem(item)
|
||||||
|
assert.Equal(t, wantData, gotData)
|
||||||
|
|
||||||
|
newItem := toCacheItem(gotData)
|
||||||
|
gotData = fromCacheItem(newItem)
|
||||||
|
assert.Equal(t, wantData, gotData)
|
||||||
|
}
|
Loading…
Reference in New Issue