[ragel-users] [PATCH]: improvements to rlgen-java

Adrian Thurston thurs... at cs.queensu.ca
Wed Dec 26 21:44:28 UTC 2007


Hi David, could you resend as an attachment? Some lines wrapped. I tried
to fix them but the whitespace must have gone off. The patch still fails.

Thanks,
 Adrian

David Waite wrote:
> The following patch changes the Java generation a bit:
> 
> - Array row/max are changed slightly to 12/8184 based on 80 column
> display. This was just my own preference (some might prefer 10
> columns, some would prefer the power-of-2-edness of 8 columns).  Toss
> it if desired.
> 
> - Arrays are generated with an array constructor rather than a large
> block of statement constructors. This reduced my packaged JAR size by
> 14k for a relatively simple JSON vocabulary with 68 states.
> - Arrays are generated with whitespace padding in code for
> readability.
> - Array overflow (over the SAIIC limit) is done by creating multiple
> functions with numbered prefixes, then generating a combine_<name>
> function which builds a new array using new and arraycopy over all the
> previous init_<name>_<number> functions.
> 
> -David Waite
> 
> Index: rlgen-java/javacodegen.cpp
> ===================================================================
> --- rlgen-java/javacodegen.cpp	(revision 346)
> +++ rlgen-java/javacodegen.cpp	(working copy)
> @@ -28,11 +28,11 @@
>  #include <sstream>
> 
>  /* Integer array line length. */
> -#define IALL 8
> +#define IALL 12
> 
>  /* Static array initialization item count
>   * (should be multiple of IALL). */
> -#define SAIIC 8192
> +#define SAIIC 8184
> 
>  #define _resume    1
>  #define _again     2
> @@ -40,11 +40,11 @@
>  #define _test_eof  4
>  #define _out       5
> 
> -using std::ostream;
> +using std::setw;
> +using std::ios;
>  using std::ostringstream;
>  using std::string;
>  using std::cerr;
> -using std::endl;
> 
>  void lineDirective( ostream &out, char *fileName, int line )
>  {
> @@ -1125,54 +1125,59 @@
>  	item_count = 0;
>  	div_count = 1;
> 
> -	out <<
> -		"private static void init_" << name << "_0( " << type << "[] r )\n"
> -		"{\n\t";
> -
> +	out <<  "private static " << type << "[] init_" << name << "_0( )\n"
> +		"{\n\t"
> +		"return new " << type << " [] {\n\t";
>  	return out;
>  }
> 
>  std::ostream &JavaTabCodeGen::ARRAY_ITEM( string item, bool last )
>  {
> -	out << "r[" << item_count << "]=" << item << "; ";
> +	item_count++;
> 
> -	item_count += 1;
> +	out << setw(5) << setiosflags(ios::right) << item;
> 
>  	if ( !last ) {
>  		if ( item_count % SAIIC == 0 ) {
> -			out << "\n}\n\n";
> -			out << "private static void init_" << array_name << "_" <<
> div_count <<
> -					"( " << array_type << "[] r )\n{\n\t";
> -			div_count += 1;
> +			out << "\n\t};\n};\n"
> +				"private static "<< array_type << "[] init_" <<
> +				array_name << "_" << div_count << "( )\n"
> +				"{\n\t"
> +				"return new " << array_type << " [] {\n\t";
> +			div_count++;
> +		} else if (item_count % IALL == 0) {
> +			out << ",\n\t";
> +		} else {
> +			out << ",";
>  		}
> -		else if ( item_count % IALL == 0 )
> -			out << "\n\t";
>  	}
> -
>  	return out;
>  }
> 
>  std::ostream &JavaTabCodeGen::CLOSE_ARRAY()
>  {
> -	out << "\n}\n\n";
> +	out << "\n\t};\n}\n\n";
> 
> -	out <<
> -		"private static " << array_type << "[] create_" << array_name <<
> "( )\n"
> -		"{\n"
> -		"	" << array_type << "[] r = new " << array_type << "[" <<
> item_count << "];\n";
> -
> -	for ( int i = 0; i < div_count; i++ )
> -		out << "	init_" << array_name << "_" << i << "( r );\n";
> -
> -	out <<
> -		"	return r;\n"
> -		"}\n"
> -		"\n";
> -
> -	out <<
> -		"private static final " << array_type << " " << array_name <<
> -				"[] = create_" << array_name << "();\n\n";
> -
> +	if (item_count < SAIIC) {
> +		out << "private static final " << array_type << " " << array_name
> <<
> +			"[] = init_" << array_name << "_0();\n\n";
> +	} else {
> +		out << "private static final " << array_type << " [] combine_" <<
> array_name
> +			<< " () {\n\t"
> +			<< array_type << " [] combined = new " << array_type <<
> +			" [ " << item_count << " ];\n\t";
> +		int block = 0;
> +		int max_block = item_count / SAIIC;
> +		for (;block < max_block; ++block) {
> +			out << "System.arraycopy ( init_" << array_name << "_" << block
> <<
> +				" ( ), 0, combined, " << SAIIC * block << ", " << SAIIC << " );\n
> \t";
> +		}
> +		out << "System.arraycopy ( init_" << array_name << "_" << block <<
> +			" ( ), 0, combined, " << SAIIC * block << ", " << (item_count %
> SAIIC) << " );\n\t";
> +		out << "return combined;\n}\n";
> +		out << "private static final " << array_type << " [] " <<
> array_name <<
> +			" = combine_" << array_name << "();";
> +	}
>  	return out;
>  }
> 
> 
> 
> --~--~---------~--~----~------------~-------~--~----~
> You received this message because you are subscribed to the Google Groups "ragel-users" group.
> To post to this group, send email to ragel-users at googlegroups.com
> To unsubscribe from this group, send email to ragel-users-unsubscribe at googlegroups.com
> For more options, visit this group at http://groups.google.com/group/ragel-users?hl=en
> -~----------~----~----~----~------~----~------~--~---
> 
> 

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 252 bytes
Desc: OpenPGP digital signature
URL: <http://www.colm.net/pipermail/ragel-users/attachments/20071226/3dc6156c/attachment-0001.sig>


More information about the ragel-users mailing list