Skip to content

Commit a015364

Browse files
committed
Remove redundant int casting in mapping; remove redundant positive value check
1 parent ec23498 commit a015364

5 files changed

Lines changed: 299 additions & 13 deletions

File tree

QuantileFlow/ddsketch/mapping/cubic_interpolation.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,6 @@ def _cubic_interpolation(self, s: float) -> float:
5959
return s * (self.C + s * (self.B + s * self.A))
6060

6161
def compute_bucket_index(self, value: float) -> int:
62-
if value <= 0:
63-
raise ValueError("Value must be positive")
64-
6562
# Get binary exponent and normalized significand
6663
exponent, significand = self._extract_exponent_and_significand(value)
6764

@@ -73,7 +70,7 @@ def compute_bucket_index(self, value: float) -> int:
7370
# where m is the optimal multiplier, e is the exponent,
7471
# P(s) is the cubic interpolation, and γ is (1+α)/(1-α)
7572
index = self.m * (exponent + interpolated) / self.log2_gamma
76-
return int(math.ceil(index))
73+
return math.ceil(index)
7774

7875
def compute_value_from_index(self, index: float) -> float:
7976
"""
@@ -84,7 +81,7 @@ def compute_value_from_index(self, index: float) -> float:
8481
target = (index * self.log2_gamma) / self.m
8582

8683
# Extract integer and fractional parts
87-
e = int(math.floor(target))
84+
e = math.floor(target)
8885
f = target - e
8986

9087
# If f is close to 0 or 1, return power of 2 directly

QuantileFlow/ddsketch/mapping/linear_interpolation.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,6 @@ def _extract_exponent(self, value: float) -> tuple[int, float]:
3030
return exponent, normalized_fraction
3131

3232
def compute_bucket_index(self, value: float) -> int:
33-
if value <= 0:
34-
raise ValueError("Value must be positive")
35-
3633
# Get binary exponent and normalized fraction
3734
exponent, normalized_fraction = self._extract_exponent(value)
3835

@@ -42,7 +39,7 @@ def compute_bucket_index(self, value: float) -> int:
4239

4340
# Compute final index
4441
log2_value = exponent + log2_fraction
45-
return int(math.ceil(log2_value / self.log_gamma))
42+
return math.ceil(log2_value / self.log_gamma)
4643

4744
def compute_value_from_index(self, index: int) -> float:
4845
"""
@@ -58,7 +55,7 @@ def compute_value_from_index(self, index: int) -> float:
5855
log2_value = index * self.log_gamma
5956

6057
# Extract the integer and fractional parts of log2_value
61-
exponent = int(math.floor(log2_value) + 1)
58+
exponent = math.floor(log2_value) + 1
6259
mantissa = (log2_value - exponent + 2) / 2.0
6360

6461
# Use ldexp to efficiently compute 2^exponent * mantissa

QuantileFlow/ddsketch/mapping/logarithmic.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,8 @@ def __init__(self, relative_accuracy: float):
1010
self.multiplier = 1 / math.log(self.gamma)
1111

1212
def compute_bucket_index(self, value: float) -> int:
13-
if value <= 0:
14-
raise ValueError(f"Value must be positive, got {value}")
1513
# ceil(log_gamma(value) = ceil(log(value) / log(gamma))
16-
return int(math.ceil(math.log(value) * self.multiplier))
14+
return math.ceil(math.log(value) * self.multiplier)
1715

1816
def compute_value_from_index(self, index: int) -> float:
1917
# Return geometric mean of bucket boundaries
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
{
2+
"timestamp": "2026-01-10T16:17:23.669749",
3+
"name": "compute_bucket_index_redundancy",
4+
"metadata": {
5+
"num_values": 10000000,
6+
"num_trials": 5,
7+
"timestamp": "2026-01-10T16:17:23.669737"
8+
},
9+
"stats": [
10+
{
11+
"function": "profile_ddsketch.py:17(run_sketch_operations)",
12+
"ncalls": 1,
13+
"tottime": 1.3887002275999998,
14+
"cumtime": 13.0266837678,
15+
"percall_tot": 1.3887002275999998,
16+
"percall_cum": 13.0266837678
17+
},
18+
{
19+
"function": "core.py:77(insert)",
20+
"ncalls": 10000000,
21+
"tottime": 3.2283954910000006,
22+
"cumtime": 11.6364473224,
23+
"percall_tot": 3.228395491e-07,
24+
"percall_cum": 1.1636447322399998e-06
25+
},
26+
{
27+
"function": "contiguous.py:37(add)",
28+
"ncalls": 10000000,
29+
"tottime": 4.568145479,
30+
"cumtime": 5.2062685638,
31+
"percall_tot": 4.568145479000001e-07,
32+
"percall_cum": 5.206268563800001e-07
33+
},
34+
{
35+
"function": "logarithmic.py:12(compute_bucket_index)",
36+
"ncalls": 10000000,
37+
"tottime": 1.903147694,
38+
"cumtime": 3.2017832675999998,
39+
"percall_tot": 1.9031476940000004e-07,
40+
"percall_cum": 3.2017832676e-07
41+
},
42+
{
43+
"function": "~:0(<built-in method math.log>)",
44+
"ncalls": 10000001,
45+
"tottime": 0.7143321826000001,
46+
"cumtime": 0.7143321826000001,
47+
"percall_tot": 7.14332111166789e-08,
48+
"percall_cum": 7.14332111166789e-08
49+
},
50+
{
51+
"function": "~:0(<built-in method builtins.len>)",
52+
"ncalls": 10003244,
53+
"tottime": 0.6382800820000002,
54+
"cumtime": 0.6382800820000002,
55+
"percall_tot": 6.380730510039856e-08,
56+
"percall_cum": 6.380730510039856e-08
57+
},
58+
{
59+
"function": "~:0(<built-in method math.ceil>)",
60+
"ncalls": 10000000,
61+
"tottime": 0.584304876,
62+
"cumtime": 0.584304876,
63+
"percall_tot": 5.8430487600000004e-08,
64+
"percall_cum": 5.8430487600000004e-08
65+
},
66+
{
67+
"function": "core.py:128(quantile)",
68+
"ncalls": 4,
69+
"tottime": 0.0004936750000000001,
70+
"cumtime": 0.0014924204000000003,
71+
"percall_tot": 0.00012341875000000001,
72+
"percall_cum": 0.0003731051000000001
73+
},
74+
{
75+
"function": "contiguous.py:167(get_count)",
76+
"ncalls": 3223,
77+
"tottime": 0.0008286914000000001,
78+
"cumtime": 0.0009856886,
79+
"percall_tot": 2.571817063873498e-07,
80+
"percall_cum": 3.0584562750259306e-07
81+
},
82+
{
83+
"function": "core.py:24(__init__)",
84+
"ncalls": 1,
85+
"tottime": 1.12378e-05,
86+
"cumtime": 4.37974e-05,
87+
"percall_tot": 1.12378e-05,
88+
"percall_cum": 4.37974e-05
89+
},
90+
{
91+
"function": "~:0(<method 'disable' of '_lsprof.Profiler' objects>)",
92+
"ncalls": 1,
93+
"tottime": 3.56454e-05,
94+
"cumtime": 3.56454e-05,
95+
"percall_tot": 3.56454e-05,
96+
"percall_cum": 3.56454e-05
97+
},
98+
{
99+
"function": "contiguous.py:19(__init__)",
100+
"ncalls": 2,
101+
"tottime": 1.10118e-05,
102+
"cumtime": 2.7115199999999997e-05,
103+
"percall_tot": 5.5059e-06,
104+
"percall_cum": 1.3557599999999999e-05
105+
},
106+
{
107+
"function": "~:0(<built-in method numpy.zeros>)",
108+
"ncalls": 2,
109+
"tottime": 1.4002600000000002e-05,
110+
"cumtime": 1.4002600000000002e-05,
111+
"percall_tot": 7.001300000000001e-06,
112+
"percall_cum": 7.001300000000001e-06
113+
},
114+
{
115+
"function": "logarithmic.py:16(compute_value_from_index)",
116+
"ncalls": 4,
117+
"tottime": 1.12992e-05,
118+
"cumtime": 1.3056800000000002e-05,
119+
"percall_tot": 2.8248e-06,
120+
"percall_cum": 3.2642000000000004e-06
121+
},
122+
{
123+
"function": "logarithmic.py:7(__init__)",
124+
"ncalls": 1,
125+
"tottime": 3.959400000000001e-06,
126+
"cumtime": 5.4444e-06,
127+
"percall_tot": 3.959400000000001e-06,
128+
"percall_cum": 5.4444e-06
129+
},
130+
{
131+
"function": "base.py:17(__init__)",
132+
"ncalls": 2,
133+
"tottime": 2.1008e-06,
134+
"cumtime": 2.1008e-06,
135+
"percall_tot": 1.0504e-06,
136+
"percall_cum": 1.0504e-06
137+
},
138+
{
139+
"function": "~:0(<built-in method math.pow>)",
140+
"ncalls": 4,
141+
"tottime": 1.7576000000000003e-06,
142+
"cumtime": 1.7576000000000003e-06,
143+
"percall_tot": 4.394000000000001e-07,
144+
"percall_cum": 4.394000000000001e-07
145+
}
146+
]
147+
}

benchmarks/numba_mapping.json

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
{
2+
"timestamp": "2026-01-10T16:02:37.248118",
3+
"name": "numba_mapping",
4+
"metadata": {
5+
"num_values": 10000000,
6+
"num_trials": 5,
7+
"timestamp": "2026-01-10T16:02:37.248088"
8+
},
9+
"stats": [
10+
{
11+
"function": "profile_ddsketch.py:17(run_sketch_operations)",
12+
"ncalls": 1,
13+
"tottime": 1.5803080648,
14+
"cumtime": 15.3215635052,
15+
"percall_tot": 1.5803080648,
16+
"percall_cum": 15.3215635052
17+
},
18+
{
19+
"function": "core.py:80(insert)",
20+
"ncalls": 10000000,
21+
"tottime": 3.561579956,
22+
"cumtime": 13.739444904199999,
23+
"percall_tot": 3.561579956e-07,
24+
"percall_cum": 1.3739444904200001e-06
25+
},
26+
{
27+
"function": "contiguous.py:37(add)",
28+
"ncalls": 10000000,
29+
"tottime": 4.967856963600001,
30+
"cumtime": 5.6552094486,
31+
"percall_tot": 4.9678569636e-07,
32+
"percall_cum": 5.6552094486e-07
33+
},
34+
{
35+
"function": "logarithmic.py:35(compute_bucket_index)",
36+
"ncalls": 10000000,
37+
"tottime": 3.040597505,
38+
"cumtime": 4.522655499600001,
39+
"percall_tot": 3.0405975050000003e-07,
40+
"percall_cum": 4.5226554996000005e-07
41+
},
42+
{
43+
"function": "~:0(<built-in method math.log>)",
44+
"ncalls": 10000001,
45+
"tottime": 0.775530691,
46+
"cumtime": 0.775530691,
47+
"percall_tot": 7.755306134469388e-08,
48+
"percall_cum": 7.755306134469388e-08
49+
},
50+
{
51+
"function": "~:0(<built-in method math.ceil>)",
52+
"ncalls": 10000000,
53+
"tottime": 0.7065290746,
54+
"cumtime": 0.7065290746,
55+
"percall_tot": 7.065290746000001e-08,
56+
"percall_cum": 7.065290746000001e-08
57+
},
58+
{
59+
"function": "~:0(<built-in method builtins.len>)",
60+
"ncalls": 10003501,
61+
"tottime": 0.6875303102,
62+
"cumtime": 0.6875303102,
63+
"percall_tot": 6.87289631095811e-08,
64+
"percall_cum": 6.87289631095811e-08
65+
},
66+
{
67+
"function": "core.py:131(quantile)",
68+
"ncalls": 4,
69+
"tottime": 0.0005834166000000001,
70+
"cumtime": 0.0017633896,
71+
"percall_tot": 0.00014585415000000003,
72+
"percall_cum": 0.0004408474
73+
},
74+
{
75+
"function": "contiguous.py:167(get_count)",
76+
"ncalls": 3482,
77+
"tottime": 0.0009676952,
78+
"cumtime": 0.0011455204,
79+
"percall_tot": 2.784679111662184e-07,
80+
"percall_cum": 3.2965915502525064e-07
81+
},
82+
{
83+
"function": "~:0(<method 'disable' of '_lsprof.Profiler' objects>)",
84+
"ncalls": 1,
85+
"tottime": 4.857280000000001e-05,
86+
"cumtime": 4.857280000000001e-05,
87+
"percall_tot": 4.857280000000001e-05,
88+
"percall_cum": 4.857280000000001e-05
89+
},
90+
{
91+
"function": "core.py:24(__init__)",
92+
"ncalls": 1,
93+
"tottime": 1.14384e-05,
94+
"cumtime": 4.71466e-05,
95+
"percall_tot": 1.14384e-05,
96+
"percall_cum": 4.71466e-05
97+
},
98+
{
99+
"function": "logarithmic.py:47(compute_value_from_index)",
100+
"ncalls": 4,
101+
"tottime": 3.120040000000001e-05,
102+
"cumtime": 3.44526e-05,
103+
"percall_tot": 7.800100000000002e-06,
104+
"percall_cum": 8.61315e-06
105+
},
106+
{
107+
"function": "contiguous.py:19(__init__)",
108+
"ncalls": 2,
109+
"tottime": 1.0658400000000001e-05,
110+
"cumtime": 2.9616400000000003e-05,
111+
"percall_tot": 5.329200000000001e-06,
112+
"percall_cum": 1.4808200000000002e-05
113+
},
114+
{
115+
"function": "~:0(<built-in method numpy.zeros>)",
116+
"ncalls": 2,
117+
"tottime": 1.6537600000000002e-05,
118+
"cumtime": 1.6537600000000002e-05,
119+
"percall_tot": 8.268800000000001e-06,
120+
"percall_cum": 8.268800000000001e-06
121+
},
122+
{
123+
"function": "logarithmic.py:29(__init__)",
124+
"ncalls": 1,
125+
"tottime": 4.3208e-06,
126+
"cumtime": 6.0918e-06,
127+
"percall_tot": 4.3208e-06,
128+
"percall_cum": 6.0918e-06
129+
},
130+
{
131+
"function": "~:0(<built-in method math.pow>)",
132+
"ncalls": 4,
133+
"tottime": 3.2522000000000004e-06,
134+
"cumtime": 3.2522000000000004e-06,
135+
"percall_tot": 8.130500000000001e-07,
136+
"percall_cum": 8.130500000000001e-07
137+
},
138+
{
139+
"function": "base.py:17(__init__)",
140+
"ncalls": 2,
141+
"tottime": 2.4204e-06,
142+
"cumtime": 2.4204e-06,
143+
"percall_tot": 1.2102e-06,
144+
"percall_cum": 1.2102e-06
145+
}
146+
]
147+
}

0 commit comments

Comments
 (0)