aboutsummaryrefslogtreecommitdiffstats
path: root/thread_pthread.c
diff options
context:
space:
mode:
authornormal <normal@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2015-01-28 17:06:40 +0000
committernormal <normal@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2015-01-28 17:06:40 +0000
commit8341136f0743199b77e2fd816d625e707b9cd485 (patch)
treec6efa44fcc4e47eec8516012731f52efd521d4bc /thread_pthread.c
parent05afc8a9d7712d1a18004d051398f1c11ef837e5 (diff)
downloadruby-8341136f0743199b77e2fd816d625e707b9cd485.tar.gz
thread.c: micro-optimize thread create/join
* thread.c (struct join_arg): restructure and make smaller (thread_join_sleep): avoid timeofday() call if forever (thread_join): pass join_arg.delay directly (rb_thread_inspect_msg): remove, inline into rb_thread_inspect (rb_thread_inspect): reduce branching and string creation * thread_pthread.c (native_set_thread_name): create string directly to avoid reparsing. [Misc #10723] This reduces time in benchmark/bm_vm_thread_create_join.rb by a few percent. Minor improvements only: target 0: 2.1.5 (ruby 2.1.5p273 (2014-11-13 revision 48405) [x86_64-linux]) target 1: trunk (ruby 2.3.0dev (2015-01-16 trunk 49282) [x86_64-linux]) target 2: built (ruby 2.3.0dev (2015-01-16 trunk 49282) [x86_64-linux]) benchmark results: minimum results in each 3 measurements. Execution time (sec) name 2.1.5 trunk built vm_thread_create_join 1.049 1.242 1.138 Speedup ratio: compare with the result of `2.1.5' (greater is better) name trunk built vm_thread_create_join 0.845 0.923 git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@49430 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'thread_pthread.c')
-rw-r--r--thread_pthread.c49
1 files changed, 24 insertions, 25 deletions
diff --git a/thread_pthread.c b/thread_pthread.c
index 7e4d36c479..3ef316c639 100644
--- a/thread_pthread.c
+++ b/thread_pthread.c
@@ -1447,36 +1447,35 @@ timer_thread_sleep(rb_global_vm_lock_t* unused)
# define SET_THREAD_NAME(name) (void)0
#endif
-static VALUE rb_thread_inspect_msg(VALUE thread, int show_enclosure, int show_location, int show_status);
-
static void
native_set_thread_name(rb_thread_t *th)
{
#if defined(__linux__) && defined(PR_SET_NAME)
- VALUE str;
- char *name, *p;
- char buf[16];
- size_t len;
-
- str = rb_thread_inspect_msg(th->self, 0, 1, 0);
- name = StringValueCStr(str);
- if (*name == '@')
- name++;
- p = strrchr(name, '/'); /* show only the basename of the path. */
- if (p && p[1])
- name = p + 1;
-
- len = strlen(name);
- if (len < sizeof(buf)) {
- memcpy(buf, name, len);
- buf[len] = '\0';
- }
- else {
- memcpy(buf, name, sizeof(buf)-2);
- buf[sizeof(buf)-2] = '*';
- buf[sizeof(buf)-1] = '\0';
+ if (!th->first_func && th->first_proc) {
+ VALUE loc = rb_proc_location(th->first_proc);
+ if (!NIL_P(loc)) {
+ const VALUE *ptr = RARRAY_CONST_PTR(loc); /* [ String, Fixnum ] */
+ char *name, *p;
+ char buf[16];
+ size_t len;
+ int n;
+
+ name = RSTRING_PTR(ptr[0]);
+ p = strrchr(name, '/'); /* show only the basename of the path. */
+ if (p && p[1])
+ name = p + 1;
+
+ n = snprintf(buf, sizeof(buf), "%s:%d", name, NUM2INT(ptr[1]));
+ rb_gc_force_recycle(loc); /* acts as a GC guard, too */
+
+ len = (size_t)n;
+ if (len >= sizeof(buf)) {
+ buf[sizeof(buf)-2] = '*';
+ buf[sizeof(buf)-1] = '\0';
+ }
+ SET_THREAD_NAME(buf);
+ }
}
- SET_THREAD_NAME(buf);
#endif
}