Only zero strings when necessary.

This removes the zeroing when the string is subsequently overwritten by
a memcpy anyway.
This commit is contained in:
def 2015-03-04 01:48:09 +01:00
commit f5968c7946
2 changed files with 25 additions and 14 deletions

View file

@ -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")

View file

@ -35,13 +35,25 @@ proc eqStrings(a, b: NimString): bool {.inline, compilerProc.} =
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,9 @@ 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 + 1) * sizeof(char))
#result.data[len] = '\0'
else: else:
result = rawNewString(len) result = rawNewString(len)
@ -64,10 +75,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) * sizeof(char))
#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,22 +87,23 @@ 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.len) 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) * sizeof(char))
proc copyStringRC1(src: NimString): NimString {.compilerRtl.} = proc copyStringRC1(src: NimString): NimString {.compilerRtl.} =
if src != nil: if src != nil:
var s = src.len
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) * sizeof(char))
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!
@ -161,7 +172,7 @@ proc resizeString(dest: NimString, addlen: int): NimString {.compilerRtl.} =
# 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.} =
c_memcpy(addr(dest.data[dest.len]), src.data, src.len + 1) c_memcpy(addr(dest.data[dest.len]), src.data, (src.len + 1) * sizeof(char))
inc(dest.len, src.len) inc(dest.len, src.len)
proc appendChar(dest: NimString, c: char) {.compilerproc, inline.} = proc appendChar(dest: NimString, c: char) {.compilerproc, inline.} =