Merge pull request #2255 from def-/copies
Only copy strings to their size, not capacity
This commit is contained in:
commit
554d7c040a
4 changed files with 32 additions and 20 deletions
|
|
@ -34,9 +34,9 @@ proc genericDeepCopyAux(dest, src: pointer, n: ptr TNimNode) {.benign.} =
|
||||||
|
|
||||||
proc copyDeepString(src: NimString): NimString {.inline.} =
|
proc copyDeepString(src: NimString): NimString {.inline.} =
|
||||||
if src != nil:
|
if src != nil:
|
||||||
result = rawNewString(src.space)
|
result = rawNewStringNoInit(src.len)
|
||||||
result.len = src.len
|
result.len = src.len
|
||||||
c_memcpy(result.data, src.data, (src.len + 1) * sizeof(char))
|
c_memcpy(result.data, src.data, src.len + 1)
|
||||||
|
|
||||||
proc genericDeepCopyAux(dest, src: pointer, mt: PNimType) =
|
proc genericDeepCopyAux(dest, src: pointer, mt: PNimType) =
|
||||||
var
|
var
|
||||||
|
|
|
||||||
|
|
@ -117,7 +117,7 @@ proc usrToCell(usr: pointer): PCell {.inline.} =
|
||||||
# convert pointer to userdata to object (=pointer to refcount)
|
# convert pointer to userdata to object (=pointer to refcount)
|
||||||
result = cast[PCell](cast[ByteAddress](usr)-%ByteAddress(sizeof(TCell)))
|
result = cast[PCell](cast[ByteAddress](usr)-%ByteAddress(sizeof(TCell)))
|
||||||
|
|
||||||
proc canbeCycleRoot(c: PCell): bool {.inline.} =
|
proc canBeCycleRoot(c: PCell): bool {.inline.} =
|
||||||
result = ntfAcyclic notin c.typ.flags
|
result = ntfAcyclic notin c.typ.flags
|
||||||
|
|
||||||
proc extGetCellType(c: pointer): PNimType {.compilerproc.} =
|
proc extGetCellType(c: pointer): PNimType {.compilerproc.} =
|
||||||
|
|
|
||||||
|
|
@ -593,7 +593,7 @@ proc addNewObjToZCT(res: PCell, gch: var TGcHeap) {.inline.} =
|
||||||
return
|
return
|
||||||
add(gch.zct, res)
|
add(gch.zct, res)
|
||||||
|
|
||||||
proc rawNewObj(typ: PNimType, size: int, gch: var TGcHeap, rc1: bool): pointer =
|
proc rawNewObj(typ: PNimType, size: int, gch: var TGcHeap, rc1 = false): pointer =
|
||||||
# generates a new object and sets its reference counter to 0
|
# generates a new object and sets its reference counter to 0
|
||||||
acquire(gch)
|
acquire(gch)
|
||||||
sysAssert(allocInv(gch.region), "rawNewObj begin")
|
sysAssert(allocInv(gch.region), "rawNewObj begin")
|
||||||
|
|
|
||||||
|
|
@ -30,18 +30,30 @@ proc eqStrings(a, b: NimString): bool {.inline, compilerProc.} =
|
||||||
if a == b: return true
|
if a == b: return true
|
||||||
if a == nil or b == nil: return false
|
if a == nil or b == nil: return false
|
||||||
return a.len == b.len and
|
return a.len == b.len and
|
||||||
c_memcmp(a.data, b.data, a.len * sizeof(char)) == 0'i32
|
c_memcmp(a.data, b.data, a.len) == 0'i32
|
||||||
|
|
||||||
when declared(allocAtomic):
|
when declared(allocAtomic):
|
||||||
template allocStr(size: expr): expr =
|
template allocStr(size: expr): expr =
|
||||||
cast[NimString](allocAtomic(size))
|
cast[NimString](allocAtomic(size))
|
||||||
|
|
||||||
|
template allocStrNoInit(size: expr): expr =
|
||||||
|
cast[NimString](boehmAllocAtomic(size))
|
||||||
else:
|
else:
|
||||||
template allocStr(size: expr): expr =
|
template allocStr(size: expr): expr =
|
||||||
cast[NimString](newObj(addr(strDesc), size))
|
cast[NimString](newObj(addr(strDesc), size))
|
||||||
|
|
||||||
|
template allocStrNoInit(size: expr): expr =
|
||||||
|
cast[NimString](rawNewObj(addr(strDesc), size, gch))
|
||||||
|
|
||||||
|
proc rawNewStringNoInit(space: int): NimString {.compilerProc.} =
|
||||||
|
var s = space
|
||||||
|
if s < 7: s = 7
|
||||||
|
result = allocStrNoInit(sizeof(TGenericSeq) + s + 1)
|
||||||
|
result.reserved = s
|
||||||
|
|
||||||
proc rawNewString(space: int): NimString {.compilerProc.} =
|
proc rawNewString(space: int): NimString {.compilerProc.} =
|
||||||
var s = space
|
var s = space
|
||||||
if s < 8: s = 7
|
if s < 7: s = 7
|
||||||
result = allocStr(sizeof(TGenericSeq) + s + 1)
|
result = allocStr(sizeof(TGenericSeq) + s + 1)
|
||||||
result.reserved = s
|
result.reserved = s
|
||||||
|
|
||||||
|
|
@ -53,10 +65,10 @@ proc copyStrLast(s: NimString, start, last: int): NimString {.compilerProc.} =
|
||||||
var start = max(start, 0)
|
var start = max(start, 0)
|
||||||
var len = min(last, s.len-1) - start + 1
|
var len = min(last, s.len-1) - start + 1
|
||||||
if len > 0:
|
if len > 0:
|
||||||
result = rawNewString(len)
|
result = rawNewStringNoInit(len)
|
||||||
result.len = len
|
result.len = len
|
||||||
c_memcpy(result.data, addr(s.data[start]), len * sizeof(char))
|
c_memcpy(result.data, addr(s.data[start]), len)
|
||||||
#result.data[len] = '\0'
|
result.data[len] = '\0'
|
||||||
else:
|
else:
|
||||||
result = rawNewString(len)
|
result = rawNewString(len)
|
||||||
|
|
||||||
|
|
@ -64,10 +76,9 @@ proc copyStr(s: NimString, start: int): NimString {.compilerProc.} =
|
||||||
result = copyStrLast(s, start, s.len-1)
|
result = copyStrLast(s, start, s.len-1)
|
||||||
|
|
||||||
proc toNimStr(str: cstring, len: int): NimString {.compilerProc.} =
|
proc toNimStr(str: cstring, len: int): NimString {.compilerProc.} =
|
||||||
result = rawNewString(len)
|
result = rawNewStringNoInit(len)
|
||||||
result.len = len
|
result.len = len
|
||||||
c_memcpy(result.data, str, (len+1) * sizeof(char))
|
c_memcpy(result.data, str, len + 1)
|
||||||
#result.data[len] = '\0' # readline relies on this!
|
|
||||||
|
|
||||||
proc cstrToNimstr(str: cstring): NimString {.compilerRtl.} =
|
proc cstrToNimstr(str: cstring): NimString {.compilerRtl.} =
|
||||||
result = toNimStr(str, c_strlen(str))
|
result = toNimStr(str, c_strlen(str))
|
||||||
|
|
@ -77,23 +88,24 @@ proc copyString(src: NimString): NimString {.compilerRtl.} =
|
||||||
if (src.reserved and seqShallowFlag) != 0:
|
if (src.reserved and seqShallowFlag) != 0:
|
||||||
result = src
|
result = src
|
||||||
else:
|
else:
|
||||||
result = rawNewString(src.space)
|
result = rawNewStringNoInit(src.len)
|
||||||
result.len = src.len
|
result.len = src.len
|
||||||
c_memcpy(result.data, src.data, (src.len + 1) * sizeof(char))
|
c_memcpy(result.data, src.data, src.len + 1)
|
||||||
|
|
||||||
proc copyStringRC1(src: NimString): NimString {.compilerRtl.} =
|
proc copyStringRC1(src: NimString): NimString {.compilerRtl.} =
|
||||||
if src != nil:
|
if src != nil:
|
||||||
var s = src.space
|
|
||||||
if s < 8: s = 7
|
|
||||||
when declared(newObjRC1):
|
when declared(newObjRC1):
|
||||||
|
var s = src.len
|
||||||
|
if s < 7: s = 7
|
||||||
result = cast[NimString](newObjRC1(addr(strDesc), sizeof(TGenericSeq) +
|
result = cast[NimString](newObjRC1(addr(strDesc), sizeof(TGenericSeq) +
|
||||||
s+1))
|
s+1))
|
||||||
|
result.reserved = s
|
||||||
else:
|
else:
|
||||||
result = allocStr(sizeof(TGenericSeq) + s + 1)
|
result = rawNewStringNoInit(src.len)
|
||||||
result.reserved = s
|
|
||||||
result.len = src.len
|
result.len = src.len
|
||||||
c_memcpy(result.data, src.data, src.len + 1)
|
c_memcpy(result.data, src.data, src.len + 1)
|
||||||
|
|
||||||
|
|
||||||
proc hashString(s: string): int {.compilerproc.} =
|
proc hashString(s: string): int {.compilerproc.} =
|
||||||
# the compiler needs exactly the same hash function!
|
# the compiler needs exactly the same hash function!
|
||||||
# this used to be used for efficient generation of string case statements
|
# this used to be used for efficient generation of string case statements
|
||||||
|
|
@ -113,7 +125,7 @@ proc addChar(s: NimString, c: char): NimString =
|
||||||
if result.len >= result.space:
|
if result.len >= result.space:
|
||||||
result.reserved = resize(result.space)
|
result.reserved = resize(result.space)
|
||||||
result = cast[NimString](growObj(result,
|
result = cast[NimString](growObj(result,
|
||||||
sizeof(TGenericSeq) + (result.reserved+1) * sizeof(char)))
|
sizeof(TGenericSeq) + result.reserved + 1))
|
||||||
result.data[result.len] = c
|
result.data[result.len] = c
|
||||||
result.data[result.len+1] = '\0'
|
result.data[result.len+1] = '\0'
|
||||||
inc(result.len)
|
inc(result.len)
|
||||||
|
|
@ -157,7 +169,7 @@ proc resizeString(dest: NimString, addlen: int): NimString {.compilerRtl.} =
|
||||||
result = cast[NimString](growObj(dest, sizeof(TGenericSeq) + sp + 1))
|
result = cast[NimString](growObj(dest, sizeof(TGenericSeq) + sp + 1))
|
||||||
result.reserved = sp
|
result.reserved = sp
|
||||||
#result = rawNewString(sp)
|
#result = rawNewString(sp)
|
||||||
#copyMem(result, dest, dest.len * sizeof(char) + sizeof(TGenericSeq))
|
#copyMem(result, dest, dest.len + sizeof(TGenericSeq))
|
||||||
# DO NOT UPDATE LEN YET: dest.len = newLen
|
# DO NOT UPDATE LEN YET: dest.len = newLen
|
||||||
|
|
||||||
proc appendString(dest, src: NimString) {.compilerproc, inline.} =
|
proc appendString(dest, src: NimString) {.compilerproc, inline.} =
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue