Skip to content

Commit 9cc1cc4

Browse files
authored
Merge branch 'main' into pep750-expression-default
2 parents c8976f4 + c49dc3b commit 9cc1cc4

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

Lib/random.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -844,8 +844,8 @@ def binomialvariate(self, n=1, p=0.5):
844844
# BTRS: Transformed rejection with squeeze method by Wolfgang Hörmann
845845
# https://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.47.8407&rep=rep1&type=pdf
846846
assert n*p >= 10.0 and p <= 0.5
847-
setup_complete = False
848847

848+
setup_complete = False
849849
spq = _sqrt(n * p * (1.0 - p)) # Standard deviation of the distribution
850850
b = 1.15 + 2.53 * spq
851851
a = -0.0873 + 0.0248 * b + 0.01 * p
@@ -860,22 +860,23 @@ def binomialvariate(self, n=1, p=0.5):
860860
k = _floor((2.0 * a / us + b) * u + c)
861861
if k < 0 or k > n:
862862
continue
863+
v = random()
863864

864865
# The early-out "squeeze" test substantially reduces
865866
# the number of acceptance condition evaluations.
866-
v = random()
867867
if us >= 0.07 and v <= vr:
868868
return k
869869

870-
# Acceptance-rejection test.
871-
# Note, the original paper erroneously omits the call to log(v)
872-
# when comparing to the log of the rescaled binomial distribution.
873870
if not setup_complete:
874871
alpha = (2.83 + 5.1 / b) * spq
875872
lpq = _log(p / (1.0 - p))
876873
m = _floor((n + 1) * p) # Mode of the distribution
877874
h = _lgamma(m + 1) + _lgamma(n - m + 1)
878875
setup_complete = True # Only needs to be done once
876+
877+
# Acceptance-rejection test.
878+
# Note, the original paper erroneously omits the call to log(v)
879+
# when comparing to the log of the rescaled binomial distribution.
879880
v *= alpha / (a / (us * us) + b)
880881
if _log(v) <= h - _lgamma(k + 1) - _lgamma(n - k + 1) + (k - m) * lpq:
881882
return k

Tools/jit/_targets.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,15 @@ async def _compile(
137137
f"-I{CPYTHON / 'Include' / 'internal' / 'mimalloc'}",
138138
f"-I{CPYTHON / 'Python'}",
139139
f"-I{CPYTHON / 'Tools' / 'jit'}",
140-
"-O3",
140+
# -O2 and -O3 include some optimizations that make sense for
141+
# standalone functions, but not for snippets of code that are going
142+
# to be laid out end-to-end (like ours)... common examples include
143+
# passes like tail-duplication, or aligning jump targets with nops.
144+
# -Os is equivalent to -O2 with many of these problematic passes
145+
# disabled. Based on manual review, for *our* purposes it usually
146+
# generates better code than -O2 (and -O2 usually generates better
147+
# code than -O3). As a nice benefit, it uses less memory too:
148+
"-Os",
141149
"-S",
142150
# Shorten full absolute file paths in the generated code (like the
143151
# __FILE__ macro and assert failure messages) for reproducibility:

0 commit comments

Comments
 (0)