Skip to content

Commit

Permalink
Make mkbundle bundle assemblies a lot faster.
Browse files Browse the repository at this point in the history
Preallocate the 256 different lines we might write to the assembly file
instead of allocating several strings for every line.

With compression mkbundle is about twice as fast now (since compression
speed hasn't changed), without compression it's 5-10x faster, depending
on the number of assemblies to embed.
  • Loading branch information
rolfbjarne committed Jul 2, 2012
1 parent be4ef5f commit a7f9abb
Showing 1 changed file with 27 additions and 26 deletions.
53 changes: 27 additions & 26 deletions mcs/tools/mkbundle/mkbundle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,26 @@ static void WriteSymbol (StreamWriter sw, string name, long size)
}
}

static string [] chars = new string [256];

static void WriteBuffer (StreamWriter ts, Stream stream, byte[] buffer)
{
int n;

// Preallocate the strings we need.
if (chars [0] == null) {
for (int i = 0; i < chars.Length; i++)
chars [i] = string.Format ("\t.byte {0}\n", i.ToString ());
}

while ((n = stream.Read (buffer, 0, buffer.Length)) != 0) {
for (int i = 0; i < n; i++)
ts.Write (chars [buffer [i]]);
}

ts.WriteLine ();
}

static void GenerateBundles (ArrayList files)
{
string temp_s = "temp.s"; // Path.GetTempFileName ();
Expand Down Expand Up @@ -244,12 +264,13 @@ static void GenerateBundles (ArrayList files)

Stream stream = File.OpenRead (fname);

// Compression can be parallelized
long real_size = stream.Length;
int n;
if (compress) {
MemoryStream ms = new MemoryStream ();
DeflaterOutputStream deflate = new DeflaterOutputStream (ms);
while ((n = stream.Read (buffer, 0, 8192)) != 0){
while ((n = stream.Read (buffer, 0, buffer.Length)) != 0){
deflate.Write (buffer, 0, n);
}
stream.Close ();
Expand All @@ -259,13 +280,8 @@ static void GenerateBundles (ArrayList files)
}

WriteSymbol (ts, "assembly_data_" + encoded, stream.Length);

while ((n = stream.Read (buffer, 0, 8192)) != 0){
for (int i = 0; i < n; i++){
ts.Write ("\t.byte {0}\n", buffer [i]);
}
}
ts.WriteLine ();

WriteBuffer (ts, stream, buffer);

if (compress) {
tc.WriteLine ("extern const unsigned char assembly_data_{0} [];", encoded);
Expand All @@ -288,11 +304,7 @@ static void GenerateBundles (ArrayList files)
Console.WriteLine (" config from: " + fname + ".config");
tc.WriteLine ("extern const unsigned char assembly_config_{0} [];", encoded);
WriteSymbol (ts, "assembly_config_" + encoded, cf.Length);
while ((n = cf.Read (buffer, 0, 8192)) != 0){
for (int i = 0; i < n; i++){
ts.Write ("\t.byte {0}\n", buffer [i]);
}
}
WriteBuffer (ts, cf, buffer);
ts.WriteLine ();
config_names.Add (new string[] {aname, encoded});
} catch (FileNotFoundException) {
Expand All @@ -312,12 +324,7 @@ static void GenerateBundles (ArrayList files)
tc.WriteLine ("extern const char system_config;");
WriteSymbol (ts, "system_config", config_file.Length);

int n;
while ((n = conf.Read (buffer, 0, 8192)) != 0){
for (int i = 0; i < n; i++){
ts.Write ("\t.byte {0}\n", buffer [i]);
}
}
WriteBuffer (ts, conf, buffer);
// null terminator
ts.Write ("\t.byte 0\n");
ts.WriteLine ();
Expand All @@ -335,13 +342,7 @@ static void GenerateBundles (ArrayList files)
tc.WriteLine ("extern const char machine_config;");
WriteSymbol (ts, "machine_config", machine_config_file.Length);

int n;
while ((n = conf.Read (buffer, 0, 8192)) != 0){
for (int i = 0; i < n; i++){
ts.Write ("\t.byte {0}\n", buffer [i]);
}
}
// null terminator
WriteBuffer (ts, conf, buffer);
ts.Write ("\t.byte 0\n");
ts.WriteLine ();
}
Expand Down

0 comments on commit a7f9abb

Please sign in to comment.