Proper syntax for 2D table with initial values


(Ken Burgett) #1

I have a large 2D table defined within a C .h file, as follows:

static const uint8_t blake2b_kat[256][64] =
{
   {
     0x78, 0x6A, 0x02, 0xF7, 0x42, 0x01, 0x59, 0x03,
     0xC6, 0xC6, 0xFD, 0x85, 0x25, 0x52, 0xD2, 0x72,
     0x91, 0x2F, 0x47, 0x40, 0xE1, 0x58, 0x47, 0x61,
     0x8A, 0x86, 0xE2, 0x17, 0xF7, 0x1F, 0x54, 0x19,
     0xD2, 0x5E, 0x10, 0x31, 0xAF, 0xEE, 0x58, 0x53,
     0x13, 0x89, 0x64, 0x44, 0x93, 0x4E, 0xB0, 0x4B,
     0x90, 0x3A, 0x68, 0x5B, 0x14, 0x48, 0xB7, 0x55,
     0xD5, 0x6F, 0x70, 0x1A, 0xFE, 0x9B, 0xE2, 0xCE
   }
    ... shortened for clarity ...
}
There are 16384 elements in this array. The C compiler accepts it with no difficulty

I converted it to a .swift file like so:

let blake2b_kat: [[UInt8]] =
[
  [
   0x78, 0x6A, 0x02, 0xF7, 0x42, 0x01, 0x59, 0x03,
   0xC6, 0xC6, 0xFD, 0x85, 0x25, 0x52, 0xD2, 0x72,
   0x91, 0x2F, 0x47, 0x40, 0xE1, 0x58, 0x47, 0x61,
   0x8A, 0x86, 0xE2, 0x17, 0xF7, 0x1F, 0x54, 0x19,
   0xD2, 0x5E, 0x10, 0x31, 0xAF, 0xEE, 0x58, 0x53,
   0x13, 0x89, 0x64, 0x44, 0x93, 0x4E, 0xB0, 0x4B,
   0x90, 0x3A, 0x68, 0x5B, 0x14, 0x48, 0xB7, 0x55,
   0xD5, 0x6F, 0x70, 0x1A, 0xFE, 0x9B, 0xE2, 0xCE
  ]
...
]

When I attempt a swift compile, the compiler seems to go away and never return. I tried the same test with the REPL and saw all the text list (2600 lines), but then the same thing happened. I left the REPL running for 10 minutes or so, with no output. I have tried the same swift table definition with fewer rows and columns, and it produces the correct 2D array.

My question is whether this is just an issue with the current Swift compiler (I am using the May 9th version for Ubuntu 14.04) or is this a recognized limitation in scale? In order to get my 2D table filled and work around this issue, I will have to read the file with file I/O and do my own parsing.

···

--
Ken Burgett
Principal Software Engineer
Email: kenb@iotone.io
Office: 530.693.4449
Mobile: 831.332.6846
URL: www.iotone.co


(Daniel Dunbar) #2

The type inference used in Swift can perform poorly when used in this fashion.

You may be able to get acceptable performance by inserting explicit casts, like:
  [...] as [UInt8]
around each of your individual arrays.

If not, my suggestion would be to embed your data in a C function, and expose it to Swift using a simple C function which you import.

In any case it is probably worth filing a bug with your example test case.

- Daniel

···

On May 23, 2016, at 11:04 AM, Ken Burgett via swift-users <swift-users@swift.org> wrote:

I have a large 2D table defined within a C .h file, as follows:

static const uint8_t blake2b_kat[256][64] =
{
{
   0x78, 0x6A, 0x02, 0xF7, 0x42, 0x01, 0x59, 0x03,
   0xC6, 0xC6, 0xFD, 0x85, 0x25, 0x52, 0xD2, 0x72,
   0x91, 0x2F, 0x47, 0x40, 0xE1, 0x58, 0x47, 0x61,
   0x8A, 0x86, 0xE2, 0x17, 0xF7, 0x1F, 0x54, 0x19,
   0xD2, 0x5E, 0x10, 0x31, 0xAF, 0xEE, 0x58, 0x53,
   0x13, 0x89, 0x64, 0x44, 0x93, 0x4E, 0xB0, 0x4B,
   0x90, 0x3A, 0x68, 0x5B, 0x14, 0x48, 0xB7, 0x55,
   0xD5, 0x6F, 0x70, 0x1A, 0xFE, 0x9B, 0xE2, 0xCE
}
  ... shortened for clarity ...
}
There are 16384 elements in this array. The C compiler accepts it with no difficulty

I converted it to a .swift file like so:

let blake2b_kat: [[UInt8]] =
[
[
0x78, 0x6A, 0x02, 0xF7, 0x42, 0x01, 0x59, 0x03,
0xC6, 0xC6, 0xFD, 0x85, 0x25, 0x52, 0xD2, 0x72,
0x91, 0x2F, 0x47, 0x40, 0xE1, 0x58, 0x47, 0x61,
0x8A, 0x86, 0xE2, 0x17, 0xF7, 0x1F, 0x54, 0x19,
0xD2, 0x5E, 0x10, 0x31, 0xAF, 0xEE, 0x58, 0x53,
0x13, 0x89, 0x64, 0x44, 0x93, 0x4E, 0xB0, 0x4B,
0x90, 0x3A, 0x68, 0x5B, 0x14, 0x48, 0xB7, 0x55,
0xD5, 0x6F, 0x70, 0x1A, 0xFE, 0x9B, 0xE2, 0xCE
]
...
]

When I attempt a swift compile, the compiler seems to go away and never return. I tried the same test with the REPL and saw all the text list (2600 lines), but then the same thing happened. I left the REPL running for 10 minutes or so, with no output. I have tried the same swift table definition with fewer rows and columns, and it produces the correct 2D array.

My question is whether this is just an issue with the current Swift compiler (I am using the May 9th version for Ubuntu 14.04) or is this a recognized limitation in scale? In order to get my 2D table filled and work around this issue, I will have to read the file with file I/O and do my own parsing.
--
Ken Burgett
Principal Software Engineer
Email: kenb@iotone.io
Office: 530.693.4449
Mobile: 831.332.6846
URL: www.iotone.co
_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users