summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ChangeLog8
-rw-r--r--ext/socket/mkconstants.rb40
-rw-r--r--ext/socket/socket.c2
3 files changed, 32 insertions, 18 deletions
diff --git a/ChangeLog b/ChangeLog
index 30d91a0dbe9..badd3481cc2 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+Fri Jan 2 14:59:52 2009 Tanaka Akira <akr@fsij.org>
+
+ * ext/socket/mkconstants.rb: use hash for family_to_str to avoid
+ linear search. lenp argument removed.
+
+ * ext/socket/socket.c (ipaddr): call family_to_str without lenp
+ argument.
+
Fri Jan 2 14:33:12 2009 Tanaka Akira <akr@fsij.org>
* ext/socket/mkconstants.rb (gen_int_to_name): don't compare constants
diff --git a/ext/socket/mkconstants.rb b/ext/socket/mkconstants.rb
index 32b99d2fe3f..0491924a86f 100644
--- a/ext/socket/mkconstants.rb
+++ b/ext/socket/mkconstants.rb
@@ -66,6 +66,13 @@ def each_name(pat)
}
end
+def reverse_each_name(pat)
+ DEFS.reverse_each {|name, default_value|
+ next if pat !~ name
+ yield name
+ }
+end
+
def each_names_with_len(pat)
h = {}
DEFS.each {|name, default_value|
@@ -95,27 +102,26 @@ ERB.new(<<'EOS', nil, '%').def_method(Object, "gen_name_to_int(str_var, len_var,
}
EOS
-def each_alias(pat)
- names = []
- each_name(pat) {|n|
- names << n
- }
- yield names
-end
-
-ERB.new(<<'EOS', nil, '%').def_method(Object, "gen_int_to_name(int_var, lenp_var, pat)")
-% each_name(pat) {|n|
+ERB.new(<<'EOS', nil, '%').def_method(Object, "gen_int_to_name_hash(hash_var, pat)")
+ <%=hash_var%> = st_init_numtable();
+% reverse_each_name(pat) {|n|
#ifdef <%=n%>
- if (<%=int_var%> == <%=n%>) {
- if (<%=lenp_var%>) *<%=lenp_var%> = <%=n.bytesize%>;
- return <%=c_str n%>;
- }
+ st_insert(<%=hash_var%>, (st_data_t)<%=n%>, (st_data_t)<%=c_str n%>);
#endif
% }
+EOS
+
+ERB.new(<<'EOS', nil, '%').def_method(Object, "gen_int_to_name(int_var, hash_var)")
+ st_data_t name;
+ if (st_lookup(<%=hash_var%>, (st_data_t)<%=int_var%>, &name))
+ return (char*)name;
return NULL;
EOS
result << ERB.new(<<'EOS', nil, '%').result(binding)
+
+static st_table *family_to_str_hash;
+
static void
init_constants(VALUE mConst)
{
@@ -133,8 +139,8 @@ init_constants(VALUE mConst)
% if guard
#endif
% end
-
% }
+<%= gen_int_to_name_hash('family_to_str_hash', /\AAF_/) %>
}
static int
@@ -186,9 +192,9 @@ udp_optname_to_int(char *str, int len, int *valp)
}
static char *
-family_to_str(int val, int *lenp)
+family_to_str(int val)
{
-<%= gen_int_to_name("val", "lenp", /\AAF_/) %>
+<%= gen_int_to_name("val", "family_to_str_hash") %>
}
EOS
diff --git a/ext/socket/socket.c b/ext/socket/socket.c
index 0f6f9e8a03d..b50747a5e1b 100644
--- a/ext/socket/socket.c
+++ b/ext/socket/socket.c
@@ -1063,7 +1063,7 @@ ipaddr(struct sockaddr *sockaddr, int norevlookup)
char hbuf[1024], pbuf[1024];
char *name;
- name = family_to_str(sockaddr->sa_family, NULL);
+ name = family_to_str(sockaddr->sa_family);
if (name)
family = rb_str_new2(name);
else {