[m-rev.] for review: work around long names in erlang

Peter Wang wangp at students.csse.unimelb.edu.au
Wed Jun 13 12:33:28 AEST 2007


Estimated hours taken: 1
Branches: main

compiler/elds_to_erlang.m:
	Some automatically generated function names are too long for Erlang
	atoms, which are limited to 255 chars.  Shorten long names by taking
	the left and right parts of the string and sticking the hash of the
	string in the middle.


Index: compiler/elds_to_erlang.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/elds_to_erlang.m,v
retrieving revision 1.15
diff -u -r1.15 elds_to_erlang.m
--- compiler/elds_to_erlang.m	12 Jun 2007 06:53:56 -0000	1.15
+++ compiler/elds_to_erlang.m	13 Jun 2007 02:31:45 -0000
@@ -705,7 +705,7 @@
         ),
 
         CRttiId = rtti.ctor_rtti_id(RttiTypeCtor, type_ctor_type_ctor_info),
-        rtti.id_to_c_identifier(CRttiId, Atom)
+        rtti.id_to_c_identifier(CRttiId, Atom1)
     ;
         RttiId = elds_rtti_type_info_id(TypeInfo),
 
@@ -714,7 +714,7 @@
         Atom0 = "ti_" ++ type_info_to_string(TypeInfo),
             
             % Erlang atoms have a maximum length, so shorten names
-        Atom = string.replace_all(Atom0, "type_ctor_info", "tci")
+        Atom1 = string.replace_all(Atom0, "type_ctor_info", "tci")
     ;
         RttiId = elds_rtti_pseudo_type_info_id(PseudoTypeInfo),
         ( PseudoTypeInfo = type_var(_) ->
@@ -728,16 +728,17 @@
         Atom0 = Prefix ++ pseudo_type_info_to_string(PseudoTypeInfo),
 
             % Erlang atoms have a maximum length, so shorten names
-        Atom = string.replace_all(Atom0, "type_ctor_info", "tci")
+        Atom1 = string.replace_all(Atom0, "type_ctor_info", "tci")
     ;
         RttiId = elds_rtti_base_typeclass_id(TCName, InstanceModule,
             InstanceStr),
         TCName = tc_name(ClassModuleName, ClassName, ClassArity),
         QClassName = qualified(ClassModuleName, ClassName),
         QClassNameStr = sym_name_to_string_sep(QClassName, "__"),
-        Atom = string.append_list(["BaseTypeclassInfo_", QClassNameStr,
+        Atom1 = string.append_list(["BaseTypeclassInfo_", QClassNameStr,
             "__arity", string.from_int(ClassArity), "__", InstanceStr])
     ),
+    Atom = shorten_long_atom_name(Atom1),
     (if CurModuleName \= InstanceModule then
         output_atom(erlang_module_name_to_str(InstanceModule), !IO),
         io.write_char(':', !IO)
@@ -746,6 +747,30 @@
     ),
     output_atom(Atom, !IO).
 
+    % Some RTTI function names can be longer than the 255 character limit on
+    % atom names.  To shorten long names, we take the left and right parts
+    % of the string and stick the hash of the string in the middle.
+    %
+:- func shorten_long_atom_name(string) = string.
+
+shorten_long_atom_name(Name0) = Name :-
+    (if string.length(Name0) =< 255 then
+        Name = Name0
+    else
+        % Use only lower 32 bits of the hash value so that the result is the
+        % same on 64-bit machines as 32-bit machines.
+        %
+        % XXX we assume that `int' is at least 32 bits wide
+        %
+        % XXX it would be better to use a cryptographic hash function
+        %
+        Hash = string.hash(Name0) /\ 0xffffffff,
+        Left = string.left(Name0, 64),
+        Right = string.right(Name0, 64),
+        Middle = string.int_to_base_string(Hash, 16),
+        Name = string.append_list([Left, "...", Middle, "...", Right])
+    ).
+
 %-----------------------------------------------------------------------------%
 
 :- pred erlang_proc_name(module_info::in, pred_proc_id::in,
--------------------------------------------------------------------------
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