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

@@ -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 =