remove ArrayDummySize with unchecked arrays (#5818)

This commit is contained in:
Jacek Sieka 2017-08-28 21:44:35 +08:00 • committed by Andreas Rumpf
commit 22684370b0
11 changed files with 25 additions and 28 deletions

View file

@ -132,7 +132,7 @@ translated into a C array of undetermined size:
.. code-block:: nim .. code-block:: nim
type type
ArrayPart{.unchecked.} = array[0..0, int] ArrayPart{.unchecked.} = array[0, int]
MySeq = object MySeq = object
len, cap: int len, cap: int
data: ArrayPart data: ArrayPart
@ -146,10 +146,6 @@ Produces roughly this C code:
NI data[]; NI data[];
} MySeq; } MySeq;
The bounds checking done at compile time is not disabled for now, so to access
``s.data[C]`` (where ``C`` is a constant) the array's index needs to
include ``C``.
The base type of the unchecked array may not contain any GC'ed memory but this The base type of the unchecked array may not contain any GC'ed memory but this
is currently not checked. is currently not checked.

View file

@ -19,7 +19,7 @@ type
L: Natural L: Natural
spart: seq[T] spart: seq[T]
apart: array[ArrayPartSize, T] apart: array[ArrayPartSize, T]
UncheckedArray* {.unchecked.}[T] = array[0..100_000_000, T] UncheckedArray* {.unchecked.}[T] = array[0, T]
template usesSeqPart(x): untyped = x.L > ArrayPartSize template usesSeqPart(x): untyped = x.L > ArrayPartSize

View file

@ -9,10 +9,8 @@
## Shared string support for Nim. ## Shared string support for Nim.
const ArrayDummySize = when defined(cpu16): 10_000 else: 100_000_000
type type
UncheckedCharArray {.unchecked.} = array[0..ArrayDummySize, char] UncheckedCharArray = UncheckedArray[char]
type type
Buffer = ptr object Buffer = ptr object

View file

@ -208,7 +208,7 @@ else:
import locks import locks
type type
SharedArray {.unchecked.}[T] = array[0..100, T] SharedArray[T] = UncheckedArray[T]
proc allocSharedArray[T](nsize: int): ptr SharedArray[T] = proc allocSharedArray[T](nsize: int): ptr SharedArray[T] =
result = cast[ptr SharedArray[T]](allocShared0(sizeof(T) * nsize)) result = cast[ptr SharedArray[T]](allocShared0(sizeof(T) * nsize))

View file

@ -268,6 +268,9 @@ type
seq*{.magic: "Seq".}[T] ## Generic type to construct sequences. seq*{.magic: "Seq".}[T] ## Generic type to construct sequences.
set*{.magic: "Set".}[T] ## Generic type to construct bit sets. set*{.magic: "Set".}[T] ## Generic type to construct bit sets.
UncheckedArray* {.unchecked.}[T] = array[0, T]
## Array with no boudns checking
when defined(nimArrIdx): when defined(nimArrIdx):
# :array|openarray|string|seq|cstring|tuple # :array|openarray|string|seq|cstring|tuple
proc `[]`*[I: Ordinal;T](a: T; i: I): T {. proc `[]`*[I: Ordinal;T](a: T; i: I): T {.
@ -381,8 +384,6 @@ include "system/inclrtl"
const NoFakeVars* = defined(nimscript) ## true if the backend doesn't support \ const NoFakeVars* = defined(nimscript) ## true if the backend doesn't support \
## "fake variables" like 'var EBADF {.importc.}: cint'. ## "fake variables" like 'var EBADF {.importc.}: cint'.
const ArrayDummySize = when defined(cpu16): 10_000 else: 100_000_000
when not defined(JS): when not defined(JS):
type type
TGenericSeq {.compilerproc, pure, inheritable.} = object TGenericSeq {.compilerproc, pure, inheritable.} = object
@ -390,10 +391,9 @@ when not defined(JS):
when defined(gogc): when defined(gogc):
elemSize: int elemSize: int
PGenericSeq {.exportc.} = ptr TGenericSeq PGenericSeq {.exportc.} = ptr TGenericSeq
UncheckedCharArray {.unchecked.} = array[0..ArrayDummySize, char]
# len and space without counting the terminating zero: # len and space without counting the terminating zero:
NimStringDesc {.compilerproc, final.} = object of TGenericSeq NimStringDesc {.compilerproc, final.} = object of TGenericSeq
data: UncheckedCharArray data: UncheckedArray[char]
NimString = ptr NimStringDesc NimString = ptr NimStringDesc
when not defined(JS) and not defined(nimscript): when not defined(JS) and not defined(nimscript):
@ -1600,8 +1600,7 @@ type # these work for most platforms:
culonglong* {.importc: "unsigned long long", nodecl.} = uint64 culonglong* {.importc: "unsigned long long", nodecl.} = uint64
## This is the same as the type ``unsigned long long`` in *C*. ## This is the same as the type ``unsigned long long`` in *C*.
cstringArray* {.importc: "char**", nodecl.} = ptr cstringArray* {.importc: "char**", nodecl.} = ptr UncheckedArray[cstring]
array[0..ArrayDummySize, cstring]
## This is binary compatible to the type ``char**`` in *C*. The array's ## This is binary compatible to the type ``char**`` in *C*. The array's
## high value is large enough to disable bounds checking in practice. ## high value is large enough to disable bounds checking in practice.
## Use `cstringArrayToSeq` to convert it into a ``seq[string]``. ## Use `cstringArrayToSeq` to convert it into a ``seq[string]``.
@ -3045,7 +3044,8 @@ when not defined(JS): #and not defined(nimscript):
## creates a NULL terminated cstringArray from `a`. The result has to ## creates a NULL terminated cstringArray from `a`. The result has to
## be freed with `deallocCStringArray` after it's not needed anymore. ## be freed with `deallocCStringArray` after it's not needed anymore.
result = cast[cstringArray](alloc0((a.len+1) * sizeof(cstring))) result = cast[cstringArray](alloc0((a.len+1) * sizeof(cstring)))
let x = cast[ptr array[0..ArrayDummySize, string]](a)
let x = cast[ptr UncheckedArray[string]](a)
for i in 0 .. a.high: for i in 0 .. a.high:
result[i] = cast[cstring](alloc0(x[i].len+1)) result[i] = cast[cstring](alloc0(x[i].len+1))
copyMem(result[i], addr(x[i][0]), x[i].len) copyMem(result[i], addr(x[i][0]), x[i].len)

View file

@ -30,13 +30,12 @@ type
key: ByteAddress # start address at bit 0 key: ByteAddress # start address at bit 0
bits: array[BitIndex, int] # a bit vector bits: array[BitIndex, int] # a bit vector
PPageDescArray = ptr array[ArrayDummySize, PPageDesc] PPageDescArray = ptr UncheckedArray[PPageDesc]
CellSet {.final, pure.} = object CellSet {.final, pure.} = object
counter, max: int counter, max: int
head: PPageDesc head: PPageDesc
data: PPageDescArray data: PPageDescArray
PCellArray = ptr UncheckedArray[PCell]
PCellArray = ptr array[ArrayDummySize, PCell]
CellSeq {.final, pure.} = object CellSeq {.final, pure.} = object
len, cap: int len, cap: int
d: PCellArray d: PCellArray

View file

@ -34,7 +34,7 @@ const
type type
PPointer = ptr pointer PPointer = ptr pointer
ByteArray = array[0..ArrayDummySize, byte] ByteArray = UncheckedArray[byte]
PByte = ptr ByteArray PByte = ptr ByteArray
PString = ptr string PString = ptr string
{.deprecated: [TByteArray: ByteArray].} {.deprecated: [TByteArray: ByteArray].}

View file

@ -86,11 +86,11 @@ proc writeBuffer(f: File, buffer: pointer, len: Natural): int =
checkErr(f) checkErr(f)
proc writeBytes(f: File, a: openArray[int8|uint8], start, len: Natural): int = proc writeBytes(f: File, a: openArray[int8|uint8], start, len: Natural): int =
var x = cast[ptr array[ArrayDummySize, int8]](a) var x = cast[ptr UncheckedArray[int8]](a)
result = writeBuffer(f, addr(x[start]), len) result = writeBuffer(f, addr(x[int(start)]), len)
proc writeChars(f: File, a: openArray[char], start, len: Natural): int = proc writeChars(f: File, a: openArray[char], start, len: Natural): int =
var x = cast[ptr array[ArrayDummySize, int8]](a) var x = cast[ptr UncheckedArray[int8]](a)
result = writeBuffer(f, addr(x[start]), len) result = writeBuffer(f, addr(x[int(start)]), len)
proc write(f: File, s: string) = proc write(f: File, s: string) =
if writeBuffer(f, cstring(s), s.len) != s.len: if writeBuffer(f, cstring(s), s.len) != s.len:

View file

@ -15,7 +15,7 @@ when not declared(NimString):
type type
Utf16Char* = distinct int16 Utf16Char* = distinct int16
WideCString* = ref array[ArrayDummySize, Utf16Char] WideCString* = ref UncheckedArray[Utf16Char]
{.deprecated: [TUtf16Char: Utf16Char].} {.deprecated: [TUtf16Char: Utf16Char].}
proc len*(w: WideCString): int = proc len*(w: WideCString): int =

View file

@ -0,0 +1,5 @@
{.boundchecks: on.}
type Unchecked {.unchecked.} = array[0, char]
var x = cast[ptr Unchecked](alloc(100))
x[5] = 'x'

View file

@ -18,11 +18,10 @@ when true:
uoffset_t* = uint32 uoffset_t* = uint32
FlatBufferBuilder* = object FlatBufferBuilder* = object
uarray* {.unchecked.} [T] = array [0..0, T]
Array* [T] = object Array* [T] = object
o*: uoffset_t o*: uoffset_t
len*: int len*: int
data*: ptr uarray[T] data*: ptr UncheckedArray[T]
proc ca* (fbb: ptr FlatBufferBuilder, T: typedesc, len: int): Array[T] {.noinit.} = proc ca* (fbb: ptr FlatBufferBuilder, T: typedesc, len: int): Array[T] {.noinit.} =
result.len = len result.len = len