Change how to multiply 1.5 to ints to reduce overflow (#24257)

This commit is contained in:
Tomohiro
2024-10-08 06:18:11 +09:00
committed by GitHub
parent 4515b2dae2
commit d6633ae1da
9 changed files with 11 additions and 10 deletions

View File

@@ -73,7 +73,7 @@ const
const
invalidChar = 255
template encodeSize(size: int): int = (size * 4 div 3) + 6
template encodeSize(size: int): int = (size div 3 + size) + 6
template encodeInternal(s, alphabet: typed): untyped =
## encodes `s` into base64 representation.

View File

@@ -32,11 +32,11 @@ proc nextTry(h, maxHash: Hash): Hash {.inline.} =
proc mustRehash[T](t: T): bool {.inline.} =
# If this is changed, make sure to synchronize it with `slotsNeeded` below
assert(t.dataLen > t.counter)
result = (t.dataLen * 2 < t.counter * 3) or (t.dataLen - t.counter < 4)
result = (t.dataLen < (t.counter + t.counter div 2)) or (t.dataLen - t.counter < 4)
proc slotsNeeded(count: Natural): int {.inline.} =
# Make sure to synchronize with `mustRehash` above
result = nextPowerOfTwo(count * 3 div 2 + 4)
result = nextPowerOfTwo(count div 2 + count + 4)
template rawGetKnownHCImpl() {.dirty.} =
if t.dataLen == 0:

View File

@@ -54,7 +54,7 @@ proc `==`(a, b: StackTrace): bool =
# However a chain length of over 3000 is suspicious...
var
profileData: ProfileData
emptySlots = profileData.len * 3 div 2
emptySlots = profileData.len div 2 + profileData.len
maxChainLen = 0
totalCalls = 0

View File

@@ -22,7 +22,7 @@ proc contains(s: CellSeq, c: PCell): bool {.inline.} =
return false
proc resize(s: var CellSeq) =
s.cap = s.cap * 3 div 2
s.cap = s.cap div 2 + s.cap
let d = cast[PCellArray](alloc(s.cap * sizeof(PCell)))
copyMem(d, s.d, s.len * sizeof(PCell))
dealloc(s.d)

View File

@@ -17,7 +17,7 @@ type
d: CellArray[T]
proc resize[T](s: var CellSeq[T]) =
s.cap = s.cap * 3 div 2
s.cap = s.cap div 2 + s.cap
var newSize = s.cap * sizeof(CellTuple[T])
when compileOption("threads"):
s.d = cast[CellArray[T]](reallocShared(s.d, newSize))

View File

@@ -144,7 +144,7 @@ proc cellSetPut(t: var CellSet, key: uint): PPageDesc =
if x.key == key: return x
h = nextTry(h, t.max)
if ((t.max+1)*2 < t.counter*3) or ((t.max+1)-t.counter < 4):
if ((t.max+1) < t.counter div 2 + t.counter) or ((t.max+1)-t.counter < 4):
cellSetEnlarge(t)
inc(t.counter)
h = cast[int](key) and t.max

View File

@@ -424,7 +424,8 @@ proc collectCycles() =
rootsThreshold = 0
#cfprintf(cstderr, "[collectCycles] freed %ld, touched %ld new threshold %ld\n", j.freed, j.touched, rootsThreshold)
elif rootsThreshold < high(int) div 4:
rootsThreshold = (if rootsThreshold <= 0: defaultThreshold else: rootsThreshold) * 3 div 2
rootsThreshold = (if rootsThreshold <= 0: defaultThreshold else: rootsThreshold)
rootsThreshold = rootsThreshold div 2 + rootsThreshold
when logOrc:
cfprintf(cstderr, "[collectCycles] end; freed %ld new threshold %ld touched: %ld mem: %ld rcSum: %ld edges: %ld\n", j.freed, rootsThreshold, j.touched,
getOccupiedMem(), j.rcSum, j.edges)

View File

@@ -61,7 +61,7 @@ template reallocPayload0(p: pointer; oldLen, newLen: int): ptr NimStrPayload =
proc resize(old: int): int {.inline.} =
if old <= 0: result = 4
elif old <= high(int16): result = old * 2
else: result = old * 3 div 2 # for large arrays * 3/2 is better
else: result = old div 2 + old # for large arrays * 3/2 is better
proc prepareAdd(s: var NimStringV2; addLen: int) {.compilerRtl.} =
let newLen = s.len + addLen

View File

@@ -25,7 +25,7 @@ proc dataPointer(a: PGenericSeq, elemAlign, elemSize, index: int): pointer =
proc resize(old: int): int {.inline.} =
if old <= 0: result = 4
elif old < 65536: result = old * 2
else: result = old * 3 div 2 # for large arrays * 3/2 is better
else: result = old div 2 + old # for large arrays * 3/2 is better
when declared(allocAtomic):
template allocStr(size: untyped): untyped =