summary refs log tree commit diff stats
path: root/lib
diff options
context:
space:
mode:
authorAraq <rumpf_a@web.de>2012-02-09 20:13:36 +0100
committerAraq <rumpf_a@web.de>2012-02-09 20:13:36 +0100
commitfb35b855d58d6ee2456f761cf57ff1d26022de72 (patch)
tree7a781dea5895efafa989d2acb11a7efa848f15a7 /lib
parentb458dc70096d3f23c72b28097ef4ce5329893db8 (diff)
downloadNim-fb35b855d58d6ee2456f761cf57ff1d26022de72.tar.gz
added 'system.shallow'
Diffstat (limited to 'lib')
-rwxr-xr-xlib/system.nim23
-rwxr-xr-xlib/system/assign.nim7
-rwxr-xr-xlib/system/gc.nim4
-rwxr-xr-xlib/system/sysstr.nim86
4 files changed, 58 insertions, 62 deletions
diff --git a/lib/system.nim b/lib/system.nim
index bf9250a81..6eb6b3996 100755
--- a/lib/system.nim
+++ b/lib/system.nim
@@ -145,12 +145,14 @@ proc contains*[T](s: TSlice[T], value: T): bool {.noSideEffect, inline.} =
 when not defined(EcmaScript) and not defined(NimrodVM):
   type
     TGenericSeq {.compilerproc, pure.} = object
-      len, space: int
+      len, reserved: int
     PGenericSeq {.exportc.} = ptr TGenericSeq
     # len and space without counting the terminating zero:
     NimStringDesc {.compilerproc, final.} = object of TGenericSeq
       data: array[0..100_000_000, char]
     NimString = ptr NimStringDesc
+    
+  template space(s: PGenericSeq): int = s.reserved and not seqShallowFlag
 
   include "system/hti"
 
@@ -775,6 +777,8 @@ const
     ## a string that describes the application type. Possible values:
     ## "console", "gui", "lib".
   
+  seqShallowFlag = 1 shl (sizeof(int)*8-1)
+  
 proc compileOption*(option: string): bool {.
   magic: "CompileOption", noSideEffect.}
   ## can be used to determine an on|off compile-time option. Example:
@@ -2185,6 +2189,23 @@ template doAssert*(cond: expr, msg = "") =
       raiseAssert(astToStr(cond) & ' ' & msg)
 
 
+proc shallow*[T](s: seq[T]) {.noSideEffect, inline.} =
+  ## marks a sequence `s` as `shallow`:idx:. Subsequent assignments will not
+  ## perform deep copies of `s`. This is only useful for optimization 
+  ## purposes.
+  when not defined(EcmaScript) and not defined(NimrodVM):
+    var s = cast[PGenericSeq](s)
+    s.reserved = s.reserved or seqShallowFlag
+
+proc shallow*(s: string) {.noSideEffect, inline.} =
+  ## marks a string `s` as `shallow`:idx:. Subsequent assignments will not
+  ## perform deep copies of `s`. This is only useful for optimization 
+  ## purposes.
+  when not defined(EcmaScript) and not defined(NimrodVM):
+    var s = cast[PGenericSeq](s)
+    s.reserved = s.reserved or seqShallowFlag
+
+
 when defined(initDebugger):
   initDebugger()
 
diff --git a/lib/system/assign.nim b/lib/system/assign.nim
index dac20f99e..d1055fbb5 100755
--- a/lib/system/assign.nim
+++ b/lib/system/assign.nim
@@ -38,7 +38,8 @@ proc genericAssignAux(dest, src: Pointer, mt: PNimType, shallow: bool) =
   of tyString:
     var x = cast[ppointer](dest)
     var s2 = cast[ppointer](s)[]
-    if s2 == nil or shallow:
+    if s2 == nil or shallow or (
+        cast[PGenericSeq](s2).reserved and seqShallowFlag) != 0:
       unsureAsgnRef(x, s2)
     else:
       unsureAsgnRef(x, copyString(cast[NimString](s2)))
@@ -46,7 +47,7 @@ proc genericAssignAux(dest, src: Pointer, mt: PNimType, shallow: bool) =
     var s2 = cast[ppointer](src)[]
     var seq = cast[PGenericSeq](s2)      
     var x = cast[ppointer](dest)
-    if s2 == nil or shallow:
+    if s2 == nil or shallow or (seq.reserved and seqShallowFlag) != 0:
       # this can happen! nil sequences are allowed
       unsureAsgnRef(x, s2)
       return
@@ -61,7 +62,7 @@ proc genericAssignAux(dest, src: Pointer, mt: PNimType, shallow: bool) =
         mt.Base, shallow)
     var dstseq = cast[PGenericSeq](dst)
     dstseq.len = seq.len
-    dstseq.space = seq.len
+    dstseq.reserved = seq.len
   of tyObject, tyTuple:
     # we don't need to copy m_type field for tyObject, as they are equal anyway
     genericAssignAux(dest, src, mt.node, shallow)
diff --git a/lib/system/gc.nim b/lib/system/gc.nim
index 57ee11f8b..31f0fd022 100755
--- a/lib/system/gc.nim
+++ b/lib/system/gc.nim
@@ -426,7 +426,7 @@ proc newSeq(typ: PNimType, len: int): pointer {.compilerRtl.} =
   # `newObj` already uses locks, so no need for them here.
   result = newObj(typ, addInt(mulInt(len, typ.base.size), GenericSeqSize))
   cast[PGenericSeq](result).len = len
-  cast[PGenericSeq](result).space = len
+  cast[PGenericSeq](result).reserved = len
 
 proc newObjRC1(typ: PNimType, size: int): pointer {.compilerRtl.} =
   # generates a new object and sets its reference counter to 1
@@ -457,7 +457,7 @@ proc newObjRC1(typ: PNimType, size: int): pointer {.compilerRtl.} =
 proc newSeqRC1(typ: PNimType, len: int): pointer {.compilerRtl.} =
   result = newObjRC1(typ, addInt(mulInt(len, typ.base.size), GenericSeqSize))
   cast[PGenericSeq](result).len = len
-  cast[PGenericSeq](result).space = len
+  cast[PGenericSeq](result).reserved = len
   
 proc growObj(old: pointer, newsize: int, gch: var TGcHeap): pointer =
   acquire(gch)
diff --git a/lib/system/sysstr.nim b/lib/system/sysstr.nim
index 52b15f698..eef8163fe 100755
--- a/lib/system/sysstr.nim
+++ b/lib/system/sysstr.nim
@@ -43,7 +43,7 @@ proc rawNewString(space: int): NimString {.compilerProc.} =
   var s = space
   if s < 8: s = 7
   result = allocStr(sizeof(TGenericSeq) + s + 1)
-  result.space = s
+  result.reserved = s
 
 proc mnewString(len: int): NimString {.compilerProc.} =
   result = rawNewString(len)
@@ -73,7 +73,9 @@ proc cstrToNimstr(str: CString): NimString {.compilerProc.} =
   result = toNimstr(str, c_strlen(str))
 
 proc copyString(src: NimString): NimString {.compilerProc.} =
-  if src != nil:
+  if (src.reserved and seqShallowFlag) != 0:
+    result = src
+  elif src != nil:
     result = rawNewString(src.space)
     result.len = src.len
     c_memcpy(result.data, src.data, (src.len + 1) * sizeof(Char))
@@ -87,7 +89,7 @@ proc copyStringRC1(src: NimString): NimString {.compilerProc.} =
                                s+1))
     else:
       result = allocStr(sizeof(TGenericSeq) + s + 1)
-    result.space = s
+    result.reserved = s
     result.len = src.len
     c_memcpy(result.data, src.data, src.len + 1)
 
@@ -108,13 +110,9 @@ proc addChar(s: NimString, c: char): NimString =
   # is compilerproc!
   result = s
   if result.len >= result.space:
-    result.space = resize(result.space)
+    result.reserved = resize(result.space)
     result = cast[NimString](growObj(result,
-      sizeof(TGenericSeq) + (result.space+1) * sizeof(char)))
-    #var space = resize(result.space)
-    #result = rawNewString(space)
-    #copyMem(result, s, s.len * sizeof(char) + sizeof(TGenericSeq))
-    #result.space = space
+      sizeof(TGenericSeq) + (result.reserved+1) * sizeof(char)))
   result.data[result.len] = c
   result.data[result.len+1] = '\0'
   inc(result.len)
@@ -156,7 +154,7 @@ proc resizeString(dest: NimString, addlen: int): NimString {.compilerproc.} =
   else: # slow path:
     var sp = max(resize(dest.space), dest.len + addLen)
     result = cast[NimString](growObj(dest, sizeof(TGenericSeq) + sp + 1))
-    result.space = sp
+    result.reserved = sp
     #result = rawNewString(sp)
     #copyMem(result, dest, dest.len * sizeof(char) + sizeof(TGenericSeq))
     # DO NOT UPDATE LEN YET: dest.len = newLen
@@ -188,55 +186,31 @@ proc incrSeq(seq: PGenericSeq, elemSize: int): PGenericSeq {.compilerProc.} =
   #  add(seq, x)  generates:
   #  seq = incrSeq(seq, sizeof(x));
   #  seq[seq->len-1] = x;
-  when false:
-    # broken version:
-    result = seq
-    if result.len >= result.space:
-      var s = resize(result.space)
-      result = cast[PGenericSeq](newSeq(extGetCellType(seq), s))
-      genericSeqAssign(result, seq, XXX)
-      #copyMem(result, seq, seq.len * elemSize + GenericSeqSize)
-    inc(result.len)
-  else:
-    result = seq
-    if result.len >= result.space:
-      result.space = resize(result.space)
-      result = cast[PGenericSeq](growObj(result, elemSize * result.space +
-                                 GenericSeqSize))
-      # set new elements to zero:
-      #var s = cast[TAddress](result)
-      #zeroMem(cast[pointer](s + GenericSeqSize + (result.len * elemSize)),
-      #  (result.space - result.len) * elemSize)
-      # for i in len .. space-1:
-      #   seq->data[i] = 0
-    inc(result.len)
+  result = seq
+  if result.len >= result.space:
+    result.reserved = resize(result.space)
+    result = cast[PGenericSeq](growObj(result, elemSize * result.reserved +
+                               GenericSeqSize))
+  inc(result.len)
 
 proc setLengthSeq(seq: PGenericSeq, elemSize, newLen: int): PGenericSeq {.
     compilerRtl.} =
-  when false:
-    # broken version:
-    result = seq
-    if result.space < newLen:
-      var s = max(resize(result.space), newLen)
-      result = cast[PGenericSeq](newSeq(extGetCellType(seq), s))
-    result.len = newLen
-  else:
-    result = seq
-    if result.space < newLen:
-      result.space = max(resize(result.space), newLen)
-      result = cast[PGenericSeq](growObj(result, elemSize * result.space +
-                                 GenericSeqSize))
-    elif newLen < result.len:
-      # we need to decref here, otherwise the GC leaks!
-      when not defined(boehmGC) and not defined(nogc):
-        for i in newLen..result.len-1:
-          forAllChildrenAux(cast[pointer](cast[TAddress](result) +%
-                            GenericSeqSize +% (i*%elemSize)),
-                            extGetCellType(result).base, waZctDecRef)
-      # and set the memory to nil:
-      zeroMem(cast[pointer](cast[TAddress](result) +% GenericSeqSize +%
-             (newLen*%elemSize)), (result.len-%newLen) *% elemSize)
-    result.len = newLen
+  result = seq
+  if result.space < newLen:
+    result.reserved = max(resize(result.space), newLen)
+    result = cast[PGenericSeq](growObj(result, elemSize * result.reserved +
+                               GenericSeqSize))
+  elif newLen < result.len:
+    # we need to decref here, otherwise the GC leaks!
+    when not defined(boehmGC) and not defined(nogc):
+      for i in newLen..result.len-1:
+        forAllChildrenAux(cast[pointer](cast[TAddress](result) +%
+                          GenericSeqSize +% (i*%elemSize)),
+                          extGetCellType(result).base, waZctDecRef)
+    # and set the memory to nil:
+    zeroMem(cast[pointer](cast[TAddress](result) +% GenericSeqSize +%
+           (newLen*%elemSize)), (result.len-%newLen) *% elemSize)
+  result.len = newLen
 
 # --------------- other string routines ----------------------------------
 proc nimIntToStr(x: int): string {.compilerRtl.} =