follow up #20109; remove shallow seqs/strings for ORC (#20502)

* remove `shallow` seqs/strings for ORC

* add a changelog item

* change url of DelaunayNim
This commit is contained in:
ringabout
2022-10-06 13:16:50 +08:00
committed by GitHub
parent 964afd3050
commit 723a71bd22
9 changed files with 36 additions and 45 deletions

View File

@@ -53,7 +53,7 @@
or define your own `Math.trunc` polyfill using the [`emit` pragma](https://nim-lang.org/docs/manual.html#implementation-specific-pragmas-emit-pragma).
Nim uses `Math.trunc` for the division and modulo operators for integers.
- `shallowCopy` is removed for ARC/ORC. Use `move` when possible or combine assignment and
- `shallowCopy` and `shallow` are removed for ARC/ORC. Use `move` when possible or combine assignment and
`sink` for optimization purposes.
- The `nimPreviewDotLikeOps` define is going to be removed or deprecated.

View File

@@ -778,20 +778,6 @@ For `let` symbols a copy is not always necessary:
let s = varA # may only copy a pointer if it safe to do so
```
If you know what you're doing, you can also mark single-string (or sequence)
objects as `shallow`:idx:\:
```Nim
var s = "abc"
shallow(s) # mark 's' as a shallow string
var x = s # now might not copy the string!
```
Usage of `shallow` is always safe once you know the string won't be modified
anymore, similar to Ruby's `freeze`:idx:.
The compiler optimizes string case statements: A hashing scheme is used for them
if several different string constants are used. So code like this is reasonably
efficient:

View File

@@ -2313,29 +2313,30 @@ when compileOption("rangechecks"):
else:
template rangeCheck*(cond) = discard
proc shallow*[T](s: var 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.
if s.len == 0: return
when not defined(js) and not defined(nimscript) and not defined(nimSeqsV2):
var s = cast[PGenericSeq](s)
s.reserved = s.reserved or seqShallowFlag
proc shallow*(s: var 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(js) and not defined(nimscript) and not defined(nimSeqsV2):
var s = cast[PGenericSeq](s)
if s == nil:
s = cast[PGenericSeq](newString(0))
# string literals cannot become 'shallow':
if (s.reserved and strlitFlag) == 0:
when not defined(gcArc) and not defined(gcOrc):
proc shallow*[T](s: var 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.
if s.len == 0: return
when not defined(js) and not defined(nimscript) and not defined(nimSeqsV2):
var s = cast[PGenericSeq](s)
s.reserved = s.reserved or seqShallowFlag
proc shallow*(s: var 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(js) and not defined(nimscript) and not defined(nimSeqsV2):
var s = cast[PGenericSeq](s)
if s == nil:
s = cast[PGenericSeq](newString(0))
# string literals cannot become 'shallow':
if (s.reserved and strlitFlag) == 0:
s.reserved = s.reserved or seqShallowFlag
type
NimNodeObj = object

View File

@@ -59,7 +59,7 @@ pkg "comprehension", "nimble test", "https://github.com/alehander92/comprehensio
pkg "criterion", allowFailure = true # pending https://github.com/disruptek/criterion/issues/3 (wrongly closed)
pkg "datamancer"
pkg "dashing", "nim c tests/functional.nim"
pkg "delaunay"
pkg "delaunay", url = "https://github.com/nim-lang/DelaunayNim", useHead = true
pkg "docopt"
pkg "easygl", "nim c -o:egl -r src/easygl.nim", "https://github.com/jackmott/easygl"
pkg "elvis"

View File

@@ -175,12 +175,14 @@ when not defined(nimseqsv2):
var emptySeq: seq[int] = newSeq[int]()
block:
var t = @[1,2,3]
shallow(nilSeq)
when defined(gcRefc):
shallow(nilSeq)
t = nilSeq
doAssert t == @[]
block:
var t = @[1,2,3]
shallow(emptySeq)
when defined(gcRefc):
shallow(emptySeq)
t = emptySeq
doAssert t == @[]
block:

View File

@@ -10,8 +10,8 @@ proc initBytesRange*(s: var Bytes, ibegin = 0, iend = -1): BytesRange =
let e = if iend < 0: s.len + iend + 1
else: iend
assert ibegin > 0 and e <= s.len
shallow(s)
when defined(gcRefc):
shallow(s)
result.bytes = s
result.ibegin = ibegin
result.iend = e

View File

@@ -14,8 +14,8 @@ proc initBytesRange*(s: var Bytes, ibegin = 0, iend = -1): BytesRange =
let e = if iend < 0: s.len + iend + 1
else: iend
assert ibegin >= 0 and e <= s.len
shallow(s)
when defined(gcRefc):
shallow(s)
result.bytes = s
result.ibegin = ibegin
result.iend = e

View File

@@ -18,7 +18,8 @@ block: # setLen
block: # forceCopy
var a: string
a = "foo"
shallow(a)
when defined(gcRefc):
shallow(a)
var b: string
b = a
doAssert b[0].addr == a[0].addr

View File

@@ -26,7 +26,8 @@ when true:
# casting an empty string as sequence with shallow() should not segfault
var s2: string
shallow(s2)
when defined(gcRefc):
shallow(s2)
s2 &= "foo"
doAssert s2 == "foo"