[m-rev.] for post-commit review: speed up name mangling

Zoltan Somogyi zs at csse.unimelb.edu.au
Mon Aug 31 09:13:26 AEST 2009


library/string.m:
	Provide C implementations of some predicates that test all characters
	in a string. The Mercury implementation uses higher order calls
	to cross-module predicates, and thus has substantially higher overhead
	than a direct C implementation.

	This matters because some of these predicates are heavily used
	by the name mangling algorithm, and thus can be responsible for
	a significant chunk of the compiler's runtime.

Zoltan.

cvs diff: Diffing .
Index: string.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/library/string.m,v
retrieving revision 1.288
diff -u -b -r1.288 string.m
--- string.m	14 Aug 2009 06:11:45 -0000	1.288
+++ string.m	28 Aug 2009 14:09:51 -0000
@@ -1594,15 +1594,143 @@
 string.is_all_alpha(S) :-
     string.all_match(char.is_alpha, S).
 
+    % The C version is faster than the Mercury version.
+:- pragma foreign_proc("C",
+    is_all_alpha(S::in),
+    [will_not_call_mercury, promise_pure, thread_safe, will_not_modify_trail,
+        does_not_affect_liveness, may_duplicate, no_sharing],
+"
+    const char  *p;
+
+    SUCCESS_INDICATOR = MR_TRUE;
+    for (p = S; *p != '\\0'; p++) {
+        switch (*p) {
+            case 'a': case 'b': case 'c': case 'd': case 'e':
+            case 'f': case 'g': case 'h': case 'i': case 'j':
+            case 'k': case 'l': case 'm': case 'n': case 'o':
+            case 'p': case 'q': case 'r': case 's': case 't':
+            case 'u': case 'v': case 'w': case 'x': case 'y':
+            case 'z':
+
+            case 'A': case 'B': case 'C': case 'D': case 'E':
+            case 'F': case 'G': case 'H': case 'I': case 'J':
+            case 'K': case 'L': case 'M': case 'N': case 'O':
+            case 'P': case 'Q': case 'R': case 'S': case 'T':
+            case 'U': case 'V': case 'W': case 'X': case 'Y':
+            case 'Z':
+                continue;
+
+            default:
+                SUCCESS_INDICATOR = MR_FALSE;
+                break;
+        }
+    }
+").
+
 string.is_all_alpha_or_underscore(S) :-
     string.all_match(char.is_alpha_or_underscore, S).
 
+    % The C version is faster than the Mercury version.
+:- pragma foreign_proc("C",
+    is_all_alpha_or_underscore(S::in),
+    [will_not_call_mercury, promise_pure, thread_safe, will_not_modify_trail,
+        does_not_affect_liveness, may_duplicate, no_sharing],
+"
+    const char  *p;
+
+    SUCCESS_INDICATOR = MR_TRUE;
+    for (p = S; *p != '\\0'; p++) {
+        switch (*p) {
+            case 'a': case 'b': case 'c': case 'd': case 'e':
+            case 'f': case 'g': case 'h': case 'i': case 'j':
+            case 'k': case 'l': case 'm': case 'n': case 'o':
+            case 'p': case 'q': case 'r': case 's': case 't':
+            case 'u': case 'v': case 'w': case 'x': case 'y':
+            case 'z':
+
+            case 'A': case 'B': case 'C': case 'D': case 'E':
+            case 'F': case 'G': case 'H': case 'I': case 'J':
+            case 'K': case 'L': case 'M': case 'N': case 'O':
+            case 'P': case 'Q': case 'R': case 'S': case 'T':
+            case 'U': case 'V': case 'W': case 'X': case 'Y':
+            case 'Z':
+
+            case '_':
+                continue;
+
+            default:
+                SUCCESS_INDICATOR = MR_FALSE;
+                break;
+        }
+    }
+").
+
 string.is_all_alnum_or_underscore(S) :-
     string.all_match(char.is_alnum_or_underscore, S).
     
+    % The C version is faster than the Mercury version.
+:- pragma foreign_proc("C",
+    is_all_alnum_or_underscore(S::in),
+    [will_not_call_mercury, promise_pure, thread_safe, will_not_modify_trail,
+        does_not_affect_liveness, may_duplicate, no_sharing],
+"
+    const char  *p;
+
+    SUCCESS_INDICATOR = MR_TRUE;
+    for (p = S; *p != '\\0'; p++) {
+        switch (*p) {
+            case '0': case '1': case '2': case '3': case '4':
+            case '5': case '6': case '7': case '8': case '9':
+
+            case 'a': case 'b': case 'c': case 'd': case 'e':
+            case 'f': case 'g': case 'h': case 'i': case 'j':
+            case 'k': case 'l': case 'm': case 'n': case 'o':
+            case 'p': case 'q': case 'r': case 's': case 't':
+            case 'u': case 'v': case 'w': case 'x': case 'y':
+            case 'z':
+
+            case 'A': case 'B': case 'C': case 'D': case 'E':
+            case 'F': case 'G': case 'H': case 'I': case 'J':
+            case 'K': case 'L': case 'M': case 'N': case 'O':
+            case 'P': case 'Q': case 'R': case 'S': case 'T':
+            case 'U': case 'V': case 'W': case 'X': case 'Y':
+            case 'Z':
+
+            case '_':
+                continue;
+
+            default:
+                SUCCESS_INDICATOR = MR_FALSE;
+                break;
+        }
+    }
+").
+    
 string.is_all_digits(S) :-
     string.all_match(char.is_digit, S).
 
+    % The C version is faster than the Mercury version.
+:- pragma foreign_proc("C",
+    is_all_digits(S::in),
+    [will_not_call_mercury, promise_pure, thread_safe, will_not_modify_trail,
+        does_not_affect_liveness, may_duplicate, no_sharing],
+"
+    const char  *p;
+
+    SUCCESS_INDICATOR = MR_TRUE;
+    for (p = S; *p != '\\0'; p++) {
+        switch (*p) {
+            case '0': case '1': case '2': case '3': case '4':
+            case '5': case '6': case '7': case '8': case '9':
+                continue;
+
+            default:
+                SUCCESS_INDICATOR = MR_FALSE;
+                break;
+        }
+    }
+").
+
 string.pad_left(String0, PadChar, Width, String) :-
     string.length(String0, Length),
     ( Length < Width ->
--------------------------------------------------------------------------
mercury-reviews mailing list
Post messages to:       mercury-reviews at csse.unimelb.edu.au
Administrative Queries: owner-mercury-reviews at csse.unimelb.edu.au
Subscriptions:          mercury-reviews-request at csse.unimelb.edu.au
--------------------------------------------------------------------------



More information about the reviews mailing list