preparations for string optimizations
This commit is contained in:
parent
3936587c06
commit
c383e4d35e
3 changed files with 38 additions and 9 deletions
|
|
@ -95,6 +95,11 @@ proc cstrToNimstr(str: cstring): NimString {.compilerRtl.} =
|
|||
if str == nil: NimString(nil)
|
||||
else: toNimStr(str, str.len)
|
||||
|
||||
when defined(nimImmutableStrings):
|
||||
template wasMoved(x: NimString): bool = (x.reserved and seqShallowFlag) != 0
|
||||
else:
|
||||
template wasMoved(x: NimString): bool = false
|
||||
|
||||
proc copyString(src: NimString): NimString {.compilerRtl.} =
|
||||
if src != nil:
|
||||
if (src.reserved and seqShallowFlag) != 0:
|
||||
|
|
@ -103,6 +108,16 @@ proc copyString(src: NimString): NimString {.compilerRtl.} =
|
|||
result = rawNewStringNoInit(src.len)
|
||||
result.len = src.len
|
||||
copyMem(addr(result.data), addr(src.data), src.len + 1)
|
||||
sysAssert((seqShallowFlag and result.reserved) == 0, "copyString")
|
||||
when defined(nimImmutableStrings):
|
||||
if (src.reserved and strlitFlag) != 0:
|
||||
result.reserved = (result.reserved and not strlitFlag) or seqShallowFlag
|
||||
|
||||
proc newOwnedString(src: NimString; n: int): NimString =
|
||||
result = rawNewStringNoInit(n)
|
||||
result.len = n
|
||||
copyMem(addr(result.data), addr(src.data), n)
|
||||
result.data[n] = '\0'
|
||||
|
||||
proc copyStringRC1(src: NimString): NimString {.compilerRtl.} =
|
||||
if src != nil:
|
||||
|
|
@ -116,6 +131,10 @@ proc copyStringRC1(src: NimString): NimString {.compilerRtl.} =
|
|||
result = rawNewStringNoInit(src.len)
|
||||
result.len = src.len
|
||||
copyMem(addr(result.data), addr(src.data), src.len + 1)
|
||||
sysAssert((seqShallowFlag and result.reserved) == 0, "copyStringRC1")
|
||||
when defined(nimImmutableStrings):
|
||||
if (src.reserved and strlitFlag) != 0:
|
||||
result.reserved = (result.reserved and not strlitFlag) or seqShallowFlag
|
||||
|
||||
proc copyDeepString(src: NimString): NimString {.inline.} =
|
||||
if src != nil:
|
||||
|
|
@ -144,6 +163,8 @@ proc addChar(s: NimString, c: char): NimString =
|
|||
result = cast[NimString](growObj(result,
|
||||
sizeof(TGenericSeq) + r + 1))
|
||||
result.reserved = r
|
||||
elif wasMoved(s):
|
||||
result = newOwnedString(s, s.len)
|
||||
result.data[result.len] = c
|
||||
result.data[result.len+1] = '\0'
|
||||
inc(result.len)
|
||||
|
|
@ -180,7 +201,7 @@ proc addChar(s: NimString, c: char): NimString =
|
|||
# s = rawNewString(0);
|
||||
|
||||
proc resizeString(dest: NimString, addlen: int): NimString {.compilerRtl.} =
|
||||
if dest.len + addlen <= dest.space:
|
||||
if dest.len + addlen <= dest.space and not wasMoved(dest):
|
||||
result = dest
|
||||
else: # slow path:
|
||||
var sp = max(resize(dest.space), dest.len + addlen)
|
||||
|
|
@ -201,7 +222,9 @@ proc appendChar(dest: NimString, c: char) {.compilerproc, inline.} =
|
|||
|
||||
proc setLengthStr(s: NimString, newLen: int): NimString {.compilerRtl.} =
|
||||
var n = max(newLen, 0)
|
||||
if n <= s.space:
|
||||
if wasMoved(s):
|
||||
result = newOwnedString(s, n)
|
||||
elif n <= s.space:
|
||||
result = s
|
||||
else:
|
||||
result = resizeString(s, n)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue