mirror of
https://github.com/nim-lang/Nim.git
synced 2025-12-28 17:04:41 +00:00
Improve and refactor cellseqs_v2 in Nim standard library (#21796)
* Refactor and optimize cellseqs_v2 in Nim standard library * Extract resizing logic into a separate 'resize' procedure for better readability and separation of concerns * Implement realloc for non-threaded cases to improve memory operations efficiency * Use ',' instead of ';' between parameters in 'add' procedure for consistency with other Nim code * Respond to Araq's feedback: Refactor resize function to use reallocShared This commit replaces the usage of allocShared and deallocShared with reallocShared to optimize memory allocation and deallocation while resizing the CellSeq.
This commit is contained in:
@@ -16,20 +16,17 @@ type
|
||||
len, cap: int
|
||||
d: CellArray[T]
|
||||
|
||||
proc add[T](s: var CellSeq[T], c: T; t: PNimTypeV2) {.inline.} =
|
||||
proc resize[T](s: var CellSeq[T]) =
|
||||
s.cap = s.cap * 3 div 2
|
||||
var newSize = s.cap * sizeof(CellTuple[T])
|
||||
when compileOption("threads"):
|
||||
s.d = cast[CellArray[T]](reallocShared(s.d, newSize))
|
||||
else:
|
||||
s.d = cast[CellArray[T]](realloc(s.d, newSize))
|
||||
|
||||
proc add[T](s: var CellSeq[T], c: T, t: PNimTypeV2) {.inline.} =
|
||||
if s.len >= s.cap:
|
||||
s.cap = s.cap * 3 div 2
|
||||
when compileOption("threads"):
|
||||
var d = cast[CellArray[T]](allocShared(uint(s.cap * sizeof(CellTuple[T]))))
|
||||
else:
|
||||
var d = cast[CellArray[T]](alloc(s.cap * sizeof(CellTuple[T])))
|
||||
copyMem(d, s.d, s.len * sizeof(CellTuple[T]))
|
||||
when compileOption("threads"):
|
||||
deallocShared(s.d)
|
||||
else:
|
||||
dealloc(s.d)
|
||||
s.d = d
|
||||
# XXX: realloc?
|
||||
s.resize()
|
||||
s.d[s.len] = (c, t)
|
||||
inc(s.len)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user