@@ -64,6 +64,9 @@ extern int TIGHTPACKBORDER;
6464extern int changedmaholescalled ;
6565int maxpasses = 2 ;
6666
67+ #define PREPROC_BUFFER_SIZE (256 * 1024) // 256KB buffer for preprocessed file
68+ static char * preproc_buffer = NULL ;
69+
6770#define BASIC_VERSION_INFO "7800basic v0.39"
6871
6972int main (int argc , char * argv [])
@@ -135,6 +138,21 @@ int main (int argc, char *argv[])
135138 fprintf (stderr , "unable to open preprocessed basic file: %s\n" ,prefilename );
136139 exit (2 );
137140 }
141+
142+ // Allocate a large buffer to speed up line-by-line reading in WASM
143+ preproc_buffer = (char * )malloc (PREPROC_BUFFER_SIZE );
144+ if (preproc_buffer == NULL )
145+ {
146+ prerror ("failed to allocate buffer for preprocessed file" );
147+ }
148+ if (setvbuf (preprocessedfd , preproc_buffer , _IOFBF , PREPROC_BUFFER_SIZE ) != 0 )
149+ {
150+ // Not a fatal error, but performance will be degraded.
151+ // Free the buffer as it won't be used.
152+ free (preproc_buffer );
153+ preproc_buffer = NULL ;
154+ prwarn ("failed to set large buffer for preprocessed file; performance may be slow." );
155+ }
138156 }
139157
140158 // this is the info we're trying to extract from the first pass,
@@ -440,5 +458,14 @@ int main (int argc, char *argv[])
440458
441459 lastrites ();
442460
461+ // Final cleanup of the preprocessed file descriptor and its buffer
462+ if (preprocessedfd != NULL && preprocessedfd != stdin ) {
463+ fclose (preprocessedfd );
464+ }
465+ if (preproc_buffer != NULL ) {
466+ free (preproc_buffer );
467+ preproc_buffer = NULL ;
468+ }
469+
443470 return 0 ;
444471}
0 commit comments