Allow some buffer overwrite on literal emitting

Calls to memcpy seem to be quite expensive

```
BM_ZFlat/0                  [html (22.24 %)   ]     114µs ± 6%   110µs ± 6%  -3.97%  (p=0.000 n=118+115)
BM_ZFlat/1                  [urls (47.84 %)   ]    1.63ms ± 5%  1.58ms ± 5%  -3.39%  (p=0.000 n=117+115)
BM_ZFlat/2                  [jpg (99.95 %)    ]    7.84µs ± 6%  7.70µs ± 6%  -1.66%  (p=0.000 n=119+117)
BM_ZFlat/3                  [jpg_200 (73.00 %)]     265ns ± 6%   255ns ± 6%  -3.48%   (p=0.000 n=101+98)
BM_ZFlat/4                  [pdf (83.31 %)    ]    11.8µs ± 6%  11.6µs ± 6%  -2.14%  (p=0.000 n=118+116)
BM_ZFlat/5                  [html4 (22.52 %)  ]     525µs ± 6%   513µs ± 6%  -2.36%  (p=0.000 n=117+116)
BM_ZFlat/6                  [txt1 (57.87 %)   ]     494µs ± 5%   480µs ± 6%  -2.84%  (p=0.000 n=118+116)
BM_ZFlat/7                  [txt2 (62.02 %)   ]     444µs ± 4%   428µs ± 7%  -3.51%  (p=0.000 n=119+117)
BM_ZFlat/8                  [txt3 (55.17 %)   ]    1.34ms ± 5%  1.30ms ± 5%  -2.40%  (p=0.000 n=120+116)
BM_ZFlat/9                  [txt4 (66.41 %)   ]    1.84ms ± 5%  1.78ms ± 5%  -3.55%  (p=0.000 n=110+111)
BM_ZFlat/10                 [pb (19.61 %)     ]     101µs ± 5%    97µs ± 5%  -4.67%  (p=0.000 n=118+118)
BM_ZFlat/11                 [gaviota (37.73 %)]     368µs ± 5%   360µs ± 6%  -2.13%    (p=0.000 n=91+90)
BM_ZFlat/12                 [cp (48.25 %)     ]    38.9µs ± 6%  36.8µs ± 6%  -5.36%    (p=0.000 n=88+87)
BM_ZFlat/13                 [c (42.52 %)      ]    13.4µs ± 6%  13.1µs ± 8%  -2.38%  (p=0.000 n=115+116)
BM_ZFlat/14                 [lsp (48.94 %)    ]    4.05µs ± 4%  3.94µs ± 4%  -2.58%    (p=0.000 n=91+85)
BM_ZFlat/15                 [xls (41.10 %)    ]    1.42ms ± 5%  1.39ms ± 7%  -2.49%  (p=0.000 n=116+117)
BM_ZFlat/16                 [xls_200 (78.00 %)]     313ns ± 6%   307ns ± 5%  -1.89%    (p=0.000 n=89+84)
BM_ZFlat/17                 [bin (18.12 %)    ]     518µs ± 5%   506µs ± 5%  -2.42%  (p=0.000 n=118+116)
BM_ZFlat/18                 [bin_200 (7.50 %) ]    86.8ns ± 6%  85.3ns ± 6%  -1.76%  (p=0.000 n=118+114)
BM_ZFlat/19                 [sum (48.99 %)    ]    67.9µs ± 4%  61.1µs ± 6%  -9.96%  (p=0.000 n=114+117)
BM_ZFlat/20                 [man (59.45 %)    ]    5.64µs ± 6%  5.47µs ± 7%  -3.06%  (p=0.000 n=117+115)
BM_ZFlatAll                 [21 kTestDataFiles]    9.23ms ± 4%  9.01ms ± 5%  -2.44%    (p=0.000 n=80+83)
BM_ZFlatIncreasingTableSize [7 tables         ]    30.4µs ± 5%  29.3µs ± 7%  -3.45%    (p=0.000 n=96+96)
```

PiperOrigin-RevId: 490184133
This commit is contained in:
Snappy Team 2022-11-22 09:59:27 +00:00 committed by Victor Costan
parent 37f375ddeb
commit 74960e8bd6
1 changed files with 13 additions and 1 deletions

View File

@ -636,7 +636,19 @@ static inline char* EmitLiteral(char* op, const char* literal, int len) {
LittleEndian::Store32(op, n);
op += count;
}
std::memcpy(op, literal, len);
// When allow_fast_path is true, we can overwrite up to 16 bytes.
if (allow_fast_path) {
char* destination = op;
const char* source = literal;
const char* end = destination + len;
do {
std::memcpy(destination, source, 16);
destination += 16;
source += 16;
} while (destination < end);
} else {
std::memcpy(op, literal, len);
}
return op + len;
}