Re: test results mc.hlp.it 156+ M



Hi, Tribhuvan!
Pavel Tsekov wrote:
On Tue, 14 Jan 2003, Tribhuvan wrote:
I don't have the experience to figure this out as yet.... but if you have any more ideas for me to try
let me know as I'm ready to test further as needed.

Can you test attached patch? I eliminate ftell()s, it is one of the possible bottlenecks.


--
Regards,
Andrew V. Samoilov
--- src/man2hlp.c	Wed Jan 15 16:25:46 2003
+++ src/man2hlp.c	Wed Jan 15 16:34:15 2003
@@ -61,38 +61,9 @@ struct node {
 static struct node nodes;
 static struct node *cnode;	/* Current node */
 
-#define MAX_STREAM_BLOCK 8192
-
-/*
- * Read in blocks of reasonable size and make sure we read everything.
- * Failure to read everything is an error, indicated by returning 0.
- */
-static size_t
-persistent_fread (void *data, size_t len, FILE *stream)
-{
-    size_t count;
-    size_t bytes_done = 0;
-    char *ptr = (char *) data;
-
-    if (len <= 0)
-	return 0;
-
-    while (bytes_done < len) {
-	count = len - bytes_done;
-	if (count > MAX_STREAM_BLOCK)
-	    count = MAX_STREAM_BLOCK;
-
-	count = fread (ptr, 1, count, stream);
-
-	if (count <= 0)
-	    return 0;
+static GString *outfile = NULL;	/* Output file content */
 
-	bytes_done += count;
-	ptr += count;
-    }
-
-    return bytes_done;
-}
+#define MAX_STREAM_BLOCK 8192
 
 /*
  * Write in blocks of reasonable size and make sure we write everything.
@@ -170,7 +141,7 @@ newline (void)
 {
     out_row++;
     col = 0;
-    fprintf (f_out, "\n");
+    g_string_append_c (outfile, '\n');
 }
 
 /* Calculate the length of string */
@@ -236,7 +207,8 @@ print_string (char *buffer)
 		continue;
 	    }
 	    backslash_flag = 0;
-	    fputc (c, f_out);
+
+	    g_string_append_c (outfile, c);
 	}
     } else {
 	/* Split into words */
@@ -251,11 +223,11 @@ print_string (char *buffer)
 		    newline ();
 		/* Words are separated by spaces */
 		if (col > 0) {
-		    fputc (' ', f_out);
+		    g_string_append_c (outfile, ' ');
 		    col++;
 		} else if (indentation) {
 		    while (col++ < indentation)
-			fputc (' ', f_out);
+			g_string_append_c (outfile, ' ');
 		}
 		/* Attempt to handle backslash quoting */
 		while (*(buffer)) {
@@ -265,7 +237,7 @@ print_string (char *buffer)
 			continue;
 		    }
 		    backslash_flag = 0;
-		    fputc (c, f_out);
+		    g_string_append_c (outfile, c);
 		}
 		/* Increase column */
 		col += len;
@@ -336,7 +308,7 @@ handle_node (char *buffer, int is_sh)
 	    if (!is_sh || !node) {
 		/* Start a new section, but omit empty section names */
 		if (*buffer) {
-		    fprintf (f_out, "%c[%s]", CHAR_NODE_END, buffer);
+		    g_string_sprintfa (outfile, "%c[%s]", CHAR_NODE_END, buffer);
 		    col++;
 		    newline ();
 		}
@@ -663,9 +635,6 @@ main (int argc, char **argv)
     FILE *f_tmpl;		/* Template file */
     char buffer[BUFFER_SIZE];	/* Full input line */
     char *node = NULL;
-    char *outfile_buffer;	/* Large buffer to keep the output file */
-    long cont_start;		/* Start of [Contents] */
-    long file_end;		/* Length of the output file */
 
     /* Validity check for arguments */
     if (argc != 4) {
@@ -681,6 +650,7 @@ main (int argc, char **argv)
     /* First stage - process the manual, write to the output file */
     f_man = fopen_check (c_man, "r");
     f_out = fopen_check (c_out, "w");
+    outfile = g_string_sized_new (128*1024);
     c_in = c_man;
 
     /* Repeat for each input line */
@@ -734,7 +704,7 @@ main (int argc, char **argv)
 		    newline ();
 		else
 		    while (++col < indentation)
-			fputc (' ', f_out);
+			g_string_append_c (outfile, ' ');
 	    }
 	}
     }
@@ -782,13 +752,7 @@ main (int argc, char **argv)
 	    } else
 		node = NULL;
 	}
-	fputs (buffer, f_out);
-    }
-
-    cont_start = ftell (f_out);
-    if (cont_start <= 0) {
-	perror (c_out);
-	return 1;
+	g_string_append (outfile, buffer);
     }
 
     if (topics)
@@ -840,53 +804,17 @@ main (int argc, char **argv)
 	cnode = next;
     }
 
-    file_end = ftell (f_out);
-
-    /* Sanity check */
-    if ((file_end <= 0) || (file_end - cont_start <= 0)) {
-	perror (c_out);
-	return 1;
-    }
-
-    fclose_check (f_out);
     fclose_check (f_tmpl);
-    /* Second stage ends here, closing all files, note the end of output */
-
-    /*
-     * Third stage - swap two parts of the output file.
-     * First, open the output file for reading and load it into the memory.
-     */
-    f_out = fopen_check (c_out, "r");
+    /* Second stage ends here */
 
-    outfile_buffer = malloc (file_end);
-    if (!outfile_buffer)
-	return 1;
-
-    if (!persistent_fread (outfile_buffer, file_end, f_out)) {
-	perror (c_out);
-	return 1;
-    }
-
-    fclose_check (f_out);
-    /* Now the output file is in the memory */
-
-    /* Again open output file for writing */
-    f_out = fopen_check (c_out, "w");
-
-    /* Write part after the "Contents" node */
-    if (!persistent_fwrite
-	(outfile_buffer + cont_start, file_end - cont_start, f_out)) {
-	perror (c_out);
-	return 1;
-    }
+    /* Third stage - store output file. */
 
-    /* Write part before the "Contents" node */
-    if (!persistent_fwrite (outfile_buffer, cont_start, f_out)) {
+    if (!persistent_fwrite (outfile->str, outfile->len, f_out)) {
 	perror (c_out);
 	return 1;
     }
 
-    free (outfile_buffer);
+    g_string_free (outfile, TRUE);
     fclose_check (f_out);
     /* Closing everything */
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]