Make shared static this() encoding table compilable

Ali Çehreli acehreli at yahoo.com
Mon Mar 14 19:05:41 UTC 2022


On 3/14/22 11:36, Ali Çehreli wrote:

 > I would experiment with two arrays holding corresponding keys and
 > values separately:
 >
 >    ushort[] keys = /* ... */;
 >    ushort[] values = /* ... */;
 >
 > And then building the AA from those. Hopefully, -O works better for that
 > case.

Yes, better but not much: 37 seconds vs. 50+ seconds on my system.

Even though I am pretty sure the OP has access to the keys and the 
values separately, if it helps, I used the following code to separate 
the keys and values:

import std.stdio;
import std.algorithm;
import std.range;

void main() {
   auto f = File("deleteme.d", "w");

   enum lineFormat = "%-( %-( 0x%04X,%|%)\n%)";

   auto keys = cp949_table.keys.sort;
   auto values = keys.map!(key => cp949_table[key]);

   f.writefln!("ushort[] keys = [\n" ~ lineFormat ~ "\n];")(keys.chunks(8));
   f.writefln!("ushort[] values = [\n" ~ lineFormat ~ 
"\n];")(values.chunks(8));
}

That program will produce a deleteme.d. Then I copy-pasted the generated 
keys and values in the following code:

pure auto make_cp949_table() {
   ushort[] keys = [
     0x8141, 0x8142, 0x8143, 0x8144, 0x8145, 0x8146, 0x8147, 0x8148,
     // ...
     0xFDF7, 0xFDF8, 0xFDF9, 0xFDFA, 0xFDFB, 0xFDFC, 0xFDFD, 0xFDFE,
                    ];

   ushort[] values = [
     0xAC02, 0xAC03, 0xAC05, 0xAC06, 0xAC0B, 0xAC0C, 0xAC0D, 0xAC0E,
     // ...
     0x7199, 0x71B9, 0x71BA, 0x72A7, 0x79A7, 0x7A00, 0x7FB2, 0x8A70,
                      ];

   /*
     The following failed with segmentation fault during compilation:
     import std.array : assocArray;
     return assocArray(keys, values);
   */

   import std.range : zip;

   ushort[ushort] result;
   foreach (t; zip(keys, values)) {
     result[t[0]] = t[1];
   }

   return result;
}

shared static this() {
   cp949_table = make_cp949_table();
}

Yeah, dmd's -O performance with those tables is still very poor.

Ali



More information about the Digitalmars-d-learn mailing list