[m-rev.] for review: Reduce size of MR_win32_error_name.

Peter Wang novalazy at gmail.com
Mon Aug 29 14:23:17 AEST 2022


All the strings returned by MR_win32_error_name include a common prefix
"ERROR_". Since there are many (1749) such strings, we can reduce the
size of the data by about 10 KB by returning only the part following the
prefix.

tools/generate_windows_error_name:
runtime/mercury_windows_error_name.c:
    Make MR_win32_error_name return strings without the "ERROR_" prefix.

library/io.m:
    Update caller system_error_win32_error_name to add the "ERROR_"
    prefix.

diff --git a/library/io.m b/library/io.m
index 525bf83ed..b9a8d91ef 100644
--- a/library/io.m
+++ b/library/io.m
@@ -4979,9 +4979,18 @@ system_error_errno_name(_, _) :-
     [will_not_call_mercury, promise_pure, thread_safe, may_not_export_body],
 "
 #ifdef MR_WIN32
-    const char *str = MR_win32_error_name(ErrorCode);
-    if (str != NULL) {
-        Name = (MR_String) str;
+    const char *suffix = MR_win32_error_name(ErrorCode);
+    if (suffix != NULL) {
+        const char prefix[6] = ""ERROR_"";
+        size_t prefix_len;
+        size_t suffix_len;
+
+        prefix_len = sizeof(prefix);
+        suffix_len = strlen(suffix);
+        MR_allocate_aligned_string_msg(Name, prefix_len + suffix_len,
+            MR_ALLOC_ID);
+        MR_memcpy(Name, prefix, prefix_len);
+        MR_memcpy(Name + prefix_len, suffix, suffix_len + 1); // include NUL
     } else {
         Name = MR_make_string(MR_ALLOC_ID, ""System error 0x%X"", ErrorCode);
     }
diff --git a/runtime/mercury_windows_error_name.c b/runtime/mercury_windows_error_name.c
index 92cfeeab2..77be522ae 100644
--- a/runtime/mercury_windows_error_name.c
+++ b/runtime/mercury_windows_error_name.c
[omitted]
diff --git a/tools/generate_windows_error_name b/tools/generate_windows_error_name
index 0ca6b6cac..14a1d0293 100755
--- a/tools/generate_windows_error_name
+++ b/tools/generate_windows_error_name
@@ -1787,7 +1787,8 @@ MR_win32_error_name(DWORD errcode)
 EOF
 
 for name in $names ; do
-    echo "    case $name: return \"$name\";"
+    shortname=${name#ERROR_}
+    echo "    case $name: return \"$shortname\";"
 done
 
 cat <<EOF
-- 
2.37.1



More information about the reviews mailing list