dumping ground for random patches and texts
 help / color / mirror / Atom feed
* [PATCH 1/3] deduplicate "/", ":" and "\n" strings
@ 2017-03-16 23:57 Eric Wong
  2017-03-16 23:57 ` [PATCH 2/3] deduplicate File::NULL string Eric Wong
  2017-03-16 23:57 ` [PATCH 3/3] deduplicate static rb_str_format format strings Eric Wong
  0 siblings, 2 replies; 3+ messages in thread
From: Eric Wong @ 2017-03-16 23:57 UTC (permalink / raw)
  To: spew

"/" and ":" are always statically registered in symbol.c (Init_op_tbl),
and "\n" is a commonly seen in source code.

* file.c (Init_File): fstring on File::SEPARATOR and File::PATH_SEPARATOR
* io.c (Init_IO): fstring on rb_default_rs ("\n")
---
 file.c | 4 ++--
 io.c   | 4 ++--
 2 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/file.c b/file.c
index 3f1212d91c..8bca84283d 100644
--- a/file.c
+++ b/file.c
@@ -6013,7 +6013,7 @@ Init_File(void)
     rb_define_singleton_method(rb_cFile, "extname", rb_file_s_extname, 1);
     rb_define_singleton_method(rb_cFile, "path", rb_file_s_path, 1);
 
-    separator = rb_obj_freeze(rb_usascii_str_new2("/"));
+    separator = rb_fstring_cstr("/");
     /* separates directory parts in path */
     rb_define_const(rb_cFile, "Separator", separator);
     rb_define_const(rb_cFile, "SEPARATOR", separator);
@@ -6027,7 +6027,7 @@ Init_File(void)
     rb_define_const(rb_cFile, "ALT_SEPARATOR", Qnil);
 #endif
     /* path list separator */
-    rb_define_const(rb_cFile, "PATH_SEPARATOR", rb_obj_freeze(rb_str_new2(PATH_SEP)));
+    rb_define_const(rb_cFile, "PATH_SEPARATOR", rb_fstring_cstr(PATH_SEP));
 
     rb_define_method(rb_cIO, "stat",  rb_io_stat, 0); /* this is IO's method */
     rb_define_method(rb_cFile, "lstat",  rb_file_lstat, 0);
diff --git a/io.c b/io.c
index 3c2a0a46c4..29b3f525bb 100644
--- a/io.c
+++ b/io.c
@@ -12413,10 +12413,10 @@ Init_IO(void)
     rb_output_fs = Qnil;
     rb_define_hooked_variable("$,", &rb_output_fs, 0, rb_str_setter);
 
-    rb_rs = rb_default_rs = rb_usascii_str_new2("\n");
+    rb_default_rs = rb_fstring_cstr("\n"); /* avoid modifying RS_default */
     rb_gc_register_mark_object(rb_default_rs);
+    rb_rs = rb_default_rs;
     rb_output_rs = Qnil;
-    OBJ_FREEZE(rb_default_rs);	/* avoid modifying RS_default */
     rb_define_hooked_variable("$/", &rb_rs, 0, rb_str_setter);
     rb_define_hooked_variable("$-0", &rb_rs, 0, rb_str_setter);
     rb_define_hooked_variable("$\\", &rb_output_rs, 0, rb_str_setter);
-- 
EW


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* [PATCH 2/3] deduplicate File::NULL string
  2017-03-16 23:57 [PATCH 1/3] deduplicate "/", ":" and "\n" strings Eric Wong
@ 2017-03-16 23:57 ` Eric Wong
  2017-03-16 23:57 ` [PATCH 3/3] deduplicate static rb_str_format format strings Eric Wong
  1 sibling, 0 replies; 3+ messages in thread
From: Eric Wong @ 2017-03-16 23:57 UTC (permalink / raw)
  To: spew

"/dev/null" is a common sight for pre-1.9.3-compatible code
targeting *nix systems, so deduplicate it here, as well.

* file.c (Init_File): use fstring for File::NULL
---
 file.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/file.c b/file.c
index 8bca84283d..c576c85c0a 100644
--- a/file.c
+++ b/file.c
@@ -6134,7 +6134,7 @@ Init_File(void)
     rb_define_const(rb_mFConst, "LOCK_NB", INT2FIX(LOCK_NB));
 
     /* Name of the null device */
-    rb_define_const(rb_mFConst, "NULL", rb_obj_freeze(rb_usascii_str_new2(null_device)));
+    rb_define_const(rb_mFConst, "NULL", rb_fstring_cstr(null_device));
 
     rb_define_method(rb_cFile, "path",  rb_file_path, 0);
     rb_define_method(rb_cFile, "to_path",  rb_file_path, 0);
-- 
EW


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* [PATCH 3/3] deduplicate static rb_str_format format strings
  2017-03-16 23:57 [PATCH 1/3] deduplicate "/", ":" and "\n" strings Eric Wong
  2017-03-16 23:57 ` [PATCH 2/3] deduplicate File::NULL string Eric Wong
@ 2017-03-16 23:57 ` Eric Wong
  1 sibling, 0 replies; 3+ messages in thread
From: Eric Wong @ 2017-03-16 23:57 UTC (permalink / raw)
  To: spew

Anybody who hits these code paths can hit them again in the
future, so try deduplicating across multiple runs of these
methods to reduce garbage.

* string.c (str_upto_each): fstring on "%.*d"
* strftime.c (rb_strftime_with_timespec): fstring on "%0*d"
---
 strftime.c | 3 ++-
 string.c   | 2 +-
 2 files changed, 3 insertions(+), 2 deletions(-)

diff --git a/strftime.c b/strftime.c
index 42e733818c..dbb593ae1e 100644
--- a/strftime.c
+++ b/strftime.c
@@ -820,7 +820,8 @@ rb_strftime_with_timespec(VALUE ftime, const char *format, size_t format_len,
                                         VALUE args[2], result;
                                         args[0] = INT2FIX(precision);
                                         args[1] = subsec;
-                                        result = rb_str_format(2, args, rb_str_new2("%0*d"));
+                                        result = rb_str_format(2, args,
+                                                      rb_fstring_cstr("%0*d"));
                                         (void)strlcpy(s, StringValueCStr(result), endp-s);
                                         s += precision;
                                 }
diff --git a/string.c b/string.c
index 1bfdd00e48..4c6fe2b5f9 100644
--- a/string.c
+++ b/string.c
@@ -4147,7 +4147,7 @@ str_upto_each(VALUE beg, VALUE end, int excl, int (*each)(VALUE, VALUE), VALUE a
 	}
 	else {
 	    ID op = excl ? '<' : idLE;
-	    VALUE args[2], fmt = rb_obj_freeze(rb_usascii_str_new_cstr("%.*d"));
+	    VALUE args[2], fmt = rb_fstring_cstr("%.*d");
 
 	    args[0] = INT2FIX(width);
 	    while (rb_funcall(b, op, 1, e)) {
-- 
EW


^ permalink raw reply related	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2017-03-16 23:57 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-03-16 23:57 [PATCH 1/3] deduplicate "/", ":" and "\n" strings Eric Wong
2017-03-16 23:57 ` [PATCH 2/3] deduplicate File::NULL string Eric Wong
2017-03-16 23:57 ` [PATCH 3/3] deduplicate static rb_str_format format strings Eric Wong

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).