Coverage Report

Created: 2026-03-31 07:54

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/duckdb/third_party/brotli/enc/literal_cost.cpp
Line
Count
Source
1
/* Copyright 2013 Google Inc. All Rights Reserved.
2
3
   Distributed under MIT license.
4
   See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
5
*/
6
7
/* Literal cost model to allow backward reference replacement to be efficient.
8
*/
9
10
#include "literal_cost.h"
11
12
#include <string.h>  /* memset */
13
14
#include <brotli/types.h>
15
16
#include "../common/brotli_platform.h"
17
#include "fast_log.h"
18
#include "utf8_util.h"
19
20
using namespace duckdb_brotli;
21
22
0
static size_t UTF8Position(size_t last, size_t c, size_t clamp) {
23
0
  if (c < 128) {
24
0
    return 0;  /* Next one is the 'Byte 1' again. */
25
0
  } else if (c >= 192) {  /* Next one is the 'Byte 2' of utf-8 encoding. */
26
0
    return BROTLI_MIN(size_t, 1, clamp);
27
0
  } else {
28
    /* Let's decide over the last byte if this ends the sequence. */
29
0
    if (last < 0xE0) {
30
0
      return 0;  /* Completed two or three byte coding. */
31
0
    } else {  /* Next one is the 'Byte 3' of utf-8 encoding. */
32
0
      return BROTLI_MIN(size_t, 2, clamp);
33
0
    }
34
0
  }
35
0
}
36
37
static size_t DecideMultiByteStatsLevel(size_t pos, size_t len, size_t mask,
38
0
                                        const uint8_t* data) {
39
0
  size_t counts[3] = { 0 };
40
0
  size_t max_utf8 = 1;  /* should be 2, but 1 compresses better. */
41
0
  size_t last_c = 0;
42
0
  size_t i;
43
0
  for (i = 0; i < len; ++i) {
44
0
    size_t c = data[(pos + i) & mask];
45
0
    ++counts[UTF8Position(last_c, c, 2)];
46
0
    last_c = c;
47
0
  }
48
0
  if (counts[2] < 500) {
49
0
    max_utf8 = 1;
50
0
  }
51
0
  if (counts[1] + counts[2] < 25) {
52
0
    max_utf8 = 0;
53
0
  }
54
0
  return max_utf8;
55
0
}
56
57
static void EstimateBitCostsForLiteralsUTF8(size_t pos, size_t len, size_t mask,
58
                                            const uint8_t* data,
59
0
                                            size_t* histogram, float* cost) {
60
  /* max_utf8 is 0 (normal ASCII single byte modeling),
61
     1 (for 2-byte UTF-8 modeling), or 2 (for 3-byte UTF-8 modeling). */
62
0
  const size_t max_utf8 = DecideMultiByteStatsLevel(pos, len, mask, data);
63
0
  size_t window_half = 495;
64
0
  size_t in_window = BROTLI_MIN(size_t, window_half, len);
65
0
  size_t in_window_utf8[3] = { 0 };
66
0
  size_t i;
67
0
  memset(histogram, 0, 3 * 256 * sizeof(histogram[0]));
68
69
0
  {  /* Bootstrap histograms. */
70
0
    size_t last_c = 0;
71
0
    size_t utf8_pos = 0;
72
0
    for (i = 0; i < in_window; ++i) {
73
0
      size_t c = data[(pos + i) & mask];
74
0
      ++histogram[256 * utf8_pos + c];
75
0
      ++in_window_utf8[utf8_pos];
76
0
      utf8_pos = UTF8Position(last_c, c, max_utf8);
77
0
      last_c = c;
78
0
    }
79
0
  }
80
81
  /* Compute bit costs with sliding window. */
82
0
  for (i = 0; i < len; ++i) {
83
0
    if (i >= window_half) {
84
      /* Remove a byte in the past. */
85
0
      size_t c =
86
0
          i < window_half + 1 ? 0 : data[(pos + i - window_half - 1) & mask];
87
0
      size_t last_c =
88
0
          i < window_half + 2 ? 0 : data[(pos + i - window_half - 2) & mask];
89
0
      size_t utf8_pos2 = UTF8Position(last_c, c, max_utf8);
90
0
      --histogram[256 * utf8_pos2 + data[(pos + i - window_half) & mask]];
91
0
      --in_window_utf8[utf8_pos2];
92
0
    }
93
0
    if (i + window_half < len) {
94
      /* Add a byte in the future. */
95
0
      size_t c = data[(pos + i + window_half - 1) & mask];
96
0
      size_t last_c = data[(pos + i + window_half - 2) & mask];
97
0
      size_t utf8_pos2 = UTF8Position(last_c, c, max_utf8);
98
0
      ++histogram[256 * utf8_pos2 + data[(pos + i + window_half) & mask]];
99
0
      ++in_window_utf8[utf8_pos2];
100
0
    }
101
0
    {
102
0
      size_t c = i < 1 ? 0 : data[(pos + i - 1) & mask];
103
0
      size_t last_c = i < 2 ? 0 : data[(pos + i - 2) & mask];
104
0
      size_t utf8_pos = UTF8Position(last_c, c, max_utf8);
105
0
      size_t masked_pos = (pos + i) & mask;
106
0
      size_t histo = histogram[256 * utf8_pos + data[masked_pos]];
107
0
      double lit_cost;
108
0
      if (histo == 0) {
109
0
        histo = 1;
110
0
      }
111
0
      lit_cost = FastLog2(in_window_utf8[utf8_pos]) - FastLog2(histo);
112
0
      lit_cost += 0.02905;
113
0
      if (lit_cost < 1.0) {
114
0
        lit_cost *= 0.5;
115
0
        lit_cost += 0.5;
116
0
      }
117
      /* Make the first bytes more expensive -- seems to help, not sure why.
118
         Perhaps because the entropy source is changing its properties
119
         rapidly in the beginning of the file, perhaps because the beginning
120
         of the data is a statistical "anomaly". */
121
0
      if (i < 2000) {
122
0
        lit_cost += 0.7 - ((double)(2000 - i) / 2000.0 * 0.35);
123
0
      }
124
0
      cost[i] = (float)lit_cost;
125
0
    }
126
0
  }
127
0
}
128
129
void duckdb_brotli::BrotliEstimateBitCostsForLiterals(size_t pos, size_t len, size_t mask,
130
                                       const uint8_t* data,
131
0
                                       size_t* histogram, float* cost) {
132
0
  if (BrotliIsMostlyUTF8(data, pos, mask, len, kMinUTF8Ratio)) {
133
0
    EstimateBitCostsForLiteralsUTF8(pos, len, mask, data, histogram, cost);
134
0
    return;
135
0
  } else {
136
0
    size_t window_half = 2000;
137
0
    size_t in_window = BROTLI_MIN(size_t, window_half, len);
138
0
    size_t i;
139
0
    memset(histogram, 0, 256 * sizeof(histogram[0]));
140
141
    /* Bootstrap histogram. */
142
0
    for (i = 0; i < in_window; ++i) {
143
0
      ++histogram[data[(pos + i) & mask]];
144
0
    }
145
146
    /* Compute bit costs with sliding window. */
147
0
    for (i = 0; i < len; ++i) {
148
0
      size_t histo;
149
0
      if (i >= window_half) {
150
        /* Remove a byte in the past. */
151
0
        --histogram[data[(pos + i - window_half) & mask]];
152
0
        --in_window;
153
0
      }
154
0
      if (i + window_half < len) {
155
        /* Add a byte in the future. */
156
0
        ++histogram[data[(pos + i + window_half) & mask]];
157
0
        ++in_window;
158
0
      }
159
0
      histo = histogram[data[(pos + i) & mask]];
160
0
      if (histo == 0) {
161
0
        histo = 1;
162
0
      }
163
0
      {
164
0
        double lit_cost = FastLog2(in_window) - FastLog2(histo);
165
0
        lit_cost += 0.029;
166
0
        if (lit_cost < 1.0) {
167
0
          lit_cost *= 0.5;
168
0
          lit_cost += 0.5;
169
0
        }
170
0
        cost[i] = (float)lit_cost;
171
0
      }
172
0
    }
173
0
  }
174
0
}
175
176