mirror of
https://github.com/nim-lang/Nim.git
synced 2025-12-31 10:22:15 +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.
(cherry picked from commit b169dad1e5)
This commit is contained in:
committed by
narimiran
parent
a9f1e2dfcd
commit
1cca8ccca0
@@ -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