Skip to content

Commit bf3db20

Browse files
nyurikdanielrh
authored andcommitted
Minor hq.rs cleanup
* Convert `fn StoreLookaheadH10()` to `const STORE_LOOKAHEAD_H_10` (used locally) * Inline `fn InitStartPosQueue()` * use methods for `struct BackwardMatch` * use methods for `struct StartPosQueue`
1 parent 10083bf commit bf3db20

File tree

1 file changed

+59
-66
lines changed
  • src/enc/backward_references

1 file changed

+59
-66
lines changed

src/enc/backward_references/hq.rs

+59-66
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ pub const BROTLI_SIMPLE_DISTANCE_ALPHABET_SIZE: usize = encode::BROTLI_NUM_DISTA
5656
as usize
5757
+ (2 * encode::BROTLI_LARGE_MAX_DISTANCE_BITS as usize);
5858

59+
const STORE_LOOKAHEAD_H_10: usize = 128;
60+
5961
#[inline(always)]
6062
pub fn BrotliInitZopfliNodes(array: &mut [ZopfliNode], length: usize) {
6163
let stub = ZopfliNode::default();
@@ -84,9 +86,7 @@ impl ZopfliNode {
8486
.wrapping_add(9)
8587
.wrapping_sub(self.length >> 25)
8688
}
87-
}
8889

89-
impl ZopfliNode {
9090
#[inline(always)]
9191
fn distance_code(&self) -> u32 {
9292
let short_code: u32 = self.dcode_insert_length >> 27;
@@ -205,11 +205,6 @@ impl Default for StartPosQueue {
205205
}
206206
}
207207

208-
#[inline(always)]
209-
fn StoreLookaheadH10() -> usize {
210-
128usize
211-
}
212-
213208
impl<AllocF: Allocator<floatX>> ZopfliCostModel<AllocF> {
214209
fn init(m: &mut AllocF, dist: &BrotliDistanceParams, num_bytes: usize) -> Self {
215210
Self {
@@ -266,11 +261,6 @@ impl<AllocF: Allocator<floatX>> ZopfliCostModel<AllocF> {
266261
}
267262
}
268263

269-
#[inline(always)]
270-
fn InitStartPosQueue() -> StartPosQueue {
271-
StartPosQueue::default()
272-
}
273-
274264
#[inline(always)]
275265
fn HashBytesH10(data: &[u8]) -> u32 {
276266
let h: u32 = BROTLI_UNALIGNED_LOAD32(data).wrapping_mul(kHashMul32);
@@ -438,9 +428,11 @@ where
438428
matches_offset
439429
}
440430

441-
#[inline(always)]
442-
fn BackwardMatchLength(xself: &BackwardMatch) -> usize {
443-
(xself.length_and_code() >> 5) as usize
431+
impl BackwardMatch {
432+
#[inline(always)]
433+
fn length(&self) -> usize {
434+
(self.length_and_code() >> 5) as usize
435+
}
444436
}
445437

446438
#[inline(always)]
@@ -518,22 +510,24 @@ fn ComputeDistanceCache(
518510
}
519511
}
520512

521-
#[inline(always)]
522-
fn StartPosQueueSize(xself: &StartPosQueue) -> usize {
523-
min(xself.idx_, 8)
524-
}
513+
impl StartPosQueue {
514+
#[inline(always)]
515+
fn size(&self) -> usize {
516+
min(self.idx_, 8)
517+
}
525518

526-
fn StartPosQueuePush(xself: &mut StartPosQueue, posdata: &PosData) {
527-
let mut offset: usize = !xself.idx_ & 7usize;
528-
xself.idx_ = xself.idx_.wrapping_add(1);
529-
let len: usize = StartPosQueueSize(xself);
530-
let q: &mut [PosData; 8] = &mut xself.q_;
531-
q[offset] = *posdata;
532-
for _i in 1..len {
533-
if q[offset & 7].costdiff > q[(offset + 1) & 7].costdiff {
534-
q.swap(offset & 7, (offset + 1) & 7);
519+
fn push(&mut self, posdata: &PosData) {
520+
let mut offset: usize = !self.idx_ & 7usize;
521+
self.idx_ = self.idx_.wrapping_add(1);
522+
let len: usize = self.size();
523+
let q: &mut [PosData; 8] = &mut self.q_;
524+
q[offset] = *posdata;
525+
for _i in 1..len {
526+
if q[offset & 7].costdiff > q[(offset + 1) & 7].costdiff {
527+
q.swap(offset & 7, (offset + 1) & 7);
528+
}
529+
offset = offset.wrapping_add(1);
535530
}
536-
offset = offset.wrapping_add(1);
537531
}
538532
}
539533

@@ -571,13 +565,15 @@ fn EvaluateNode<AllocF: Allocator<floatX>>(
571565
nodes,
572566
&mut posdata.distance_cache[..],
573567
);
574-
StartPosQueuePush(queue, &mut posdata);
568+
queue.push(&mut posdata);
575569
}
576570
}
577571

578-
#[inline(always)]
579-
fn StartPosQueueAt(xself: &StartPosQueue, k: usize) -> &PosData {
580-
&xself.q_[(k.wrapping_sub(xself.idx_) & 7usize)]
572+
impl StartPosQueue {
573+
#[inline(always)]
574+
fn at(&self, k: usize) -> &PosData {
575+
&self.q_[k.wrapping_sub(self.idx_) & 7usize]
576+
}
581577
}
582578

583579
impl<AllocF: Allocator<floatX>> ZopfliCostModel<AllocF> {
@@ -655,13 +651,15 @@ fn UpdateZopfliNode(
655651
next.u = Union1::cost(cost);
656652
}
657653

658-
#[inline(always)]
659-
fn BackwardMatchLengthCode(xself: &BackwardMatch) -> usize {
660-
let code: usize = (xself.length_and_code() & 31u32) as usize;
661-
if code != 0 {
662-
code
663-
} else {
664-
BackwardMatchLength(xself)
654+
impl BackwardMatch {
655+
#[inline(always)]
656+
fn length_code(&self) -> usize {
657+
let code = (self.length_and_code() & 31u32) as usize;
658+
if code != 0 {
659+
code
660+
} else {
661+
self.length()
662+
}
665663
}
666664
}
667665

@@ -701,16 +699,16 @@ fn UpdateNodes<AllocF: Allocator<floatX>>(
701699
nodes,
702700
);
703701
{
704-
let posdata = StartPosQueueAt(queue, 0usize);
702+
let posdata = queue.at(0);
705703
let min_cost =
706704
posdata.cost + model.get_min_cost_cmd() + model.get_literal_costs(posdata.pos, pos);
707705
min_len = ComputeMinimumCopyLength(min_cost, nodes, num_bytes, pos);
708706
}
709707
k = 0usize;
710-
while k < max_iters && (k < StartPosQueueSize(queue)) {
708+
while k < max_iters && k < queue.size() {
711709
'continue28: loop {
712710
{
713-
let posdata = StartPosQueueAt(queue, k);
711+
let posdata = queue.at(k);
714712
let start: usize = posdata.pos;
715713
let inscode: u16 = GetInsertLengthCode(pos.wrapping_sub(start));
716714
let start_costdiff: floatX = posdata.costdiff;
@@ -803,7 +801,7 @@ fn UpdateNodes<AllocF: Allocator<floatX>>(
803801
{
804802
let mut len: usize = min_len;
805803
for j in 0usize..num_matches {
806-
let mut match_: BackwardMatch = BackwardMatch(matches[j]);
804+
let match_ = BackwardMatch(matches[j]);
807805
let dist: usize = match_.distance() as usize;
808806
let is_dictionary_match = dist > max_distance.wrapping_add(gap);
809807
let dist_code: usize = dist.wrapping_add(16).wrapping_sub(1);
@@ -821,7 +819,7 @@ fn UpdateNodes<AllocF: Allocator<floatX>>(
821819
let dist_cost = base_cost
822820
+ (distnumextra as floatX)
823821
+ model.get_distance_cost((dist_symbol as i32 & 0x03ff) as usize);
824-
let max_match_len: usize = BackwardMatchLength(&mut match_);
822+
let max_match_len = match_.length();
825823
if len < max_match_len
826824
&& (is_dictionary_match || max_match_len > max_zopfli_len)
827825
{
@@ -830,7 +828,7 @@ fn UpdateNodes<AllocF: Allocator<floatX>>(
830828
while len <= max_match_len {
831829
{
832830
let len_code: usize = if is_dictionary_match {
833-
BackwardMatchLengthCode(&mut match_)
831+
match_.length_code()
834832
} else {
835833
len
836834
};
@@ -919,10 +917,10 @@ where
919917
let mut model: ZopfliCostModel<AllocF>;
920918
let mut queue: StartPosQueue;
921919
let mut matches = [0; MAX_NUM_MATCHES_H10];
922-
let store_end: usize = if num_bytes >= StoreLookaheadH10() {
920+
let store_end: usize = if num_bytes >= STORE_LOOKAHEAD_H_10 {
923921
position
924922
.wrapping_add(num_bytes)
925-
.wrapping_sub(StoreLookaheadH10())
923+
.wrapping_sub(STORE_LOOKAHEAD_H_10)
926924
.wrapping_add(1)
927925
} else {
928926
position
@@ -937,7 +935,7 @@ where
937935
return 0usize;
938936
}
939937
model.set_from_literal_costs(position, ringbuffer, ringbuffer_mask);
940-
queue = InitStartPosQueue();
938+
queue = StartPosQueue::default();
941939
i = 0usize;
942940
while i.wrapping_add(handle.HashTypeLength()).wrapping_sub(1) < num_bytes {
943941
{
@@ -956,9 +954,8 @@ where
956954
params,
957955
&mut matches[lz_matches_offset..],
958956
);
959-
if num_matches > 0usize
960-
&& (BackwardMatchLength(&BackwardMatch(matches[num_matches.wrapping_sub(1)]))
961-
> max_zopfli_len)
957+
if num_matches > 0
958+
&& BackwardMatch(matches[num_matches.wrapping_sub(1)]).length() > max_zopfli_len
962959
{
963960
matches[0] = matches[num_matches.wrapping_sub(1)];
964961
num_matches = 1usize;
@@ -981,10 +978,8 @@ where
981978
if skip < 16384usize {
982979
skip = 0usize;
983980
}
984-
if num_matches == 1usize
985-
&& (BackwardMatchLength(&BackwardMatch(matches[0])) > max_zopfli_len)
986-
{
987-
skip = max(BackwardMatchLength(&BackwardMatch(matches[0])), skip);
981+
if num_matches == 1 && BackwardMatch(matches[0]).length() > max_zopfli_len {
982+
skip = max(BackwardMatch(matches[0]).length(), skip);
988983
}
989984
if skip > 1usize {
990985
handle.StoreRange(
@@ -1228,7 +1223,7 @@ fn ZopfliIterate<AllocF: Allocator<floatX>>(
12281223
let mut i: usize;
12291224
(nodes[0]).length = 0u32;
12301225
(nodes[0]).u = Union1::cost(0.0);
1231-
queue = InitStartPosQueue();
1226+
queue = StartPosQueue::default();
12321227
i = 0usize;
12331228
while i.wrapping_add(3) < num_bytes {
12341229
{
@@ -1251,12 +1246,11 @@ fn ZopfliIterate<AllocF: Allocator<floatX>>(
12511246
skip = 0usize;
12521247
}
12531248
cur_match_pos = cur_match_pos.wrapping_add(num_matches[i] as usize);
1254-
if num_matches[i] == 1u32
1255-
&& (BackwardMatchLength(&BackwardMatch(matches[cur_match_pos.wrapping_sub(1)]))
1256-
> max_zopfli_len)
1249+
if num_matches[i] == 1
1250+
&& BackwardMatch(matches[cur_match_pos.wrapping_sub(1)]).length() > max_zopfli_len
12571251
{
12581252
skip = max(
1259-
BackwardMatchLength(&BackwardMatch(matches[cur_match_pos.wrapping_sub(1)])),
1253+
BackwardMatch(matches[cur_match_pos.wrapping_sub(1)]).length(),
12601254
skip,
12611255
);
12621256
}
@@ -1315,10 +1309,10 @@ pub fn BrotliCreateHqZopfliBackwardReferences<
13151309
<Alloc as Allocator<u32>>::AllocatedMemory::default()
13161310
};
13171311
let mut matches_size: usize = (4usize).wrapping_mul(num_bytes);
1318-
let store_end: usize = if num_bytes >= StoreLookaheadH10() {
1312+
let store_end: usize = if num_bytes >= STORE_LOOKAHEAD_H_10 {
13191313
position
13201314
.wrapping_add(num_bytes)
1321-
.wrapping_sub(StoreLookaheadH10())
1315+
.wrapping_sub(STORE_LOOKAHEAD_H_10)
13221316
.wrapping_add(1)
13231317
} else {
13241318
position
@@ -1402,9 +1396,8 @@ pub fn BrotliCreateHqZopfliBackwardReferences<
14021396
}
14031397
num_matches.slice_mut()[i] = num_found_matches as u32;
14041398
if num_found_matches > 0usize {
1405-
let match_len: usize = BackwardMatchLength(&BackwardMatch(
1406-
matches.slice()[(cur_match_end.wrapping_sub(1) as usize)],
1407-
));
1399+
let match_len =
1400+
BackwardMatch(matches.slice()[cur_match_end.wrapping_sub(1)]).length();
14081401
if match_len > 325usize {
14091402
let skip: usize = match_len.wrapping_sub(1);
14101403
let tmp = matches.slice()[(cur_match_end.wrapping_sub(1) as usize)];

0 commit comments

Comments
 (0)