Bug #13085

Updated by nobu (Nobuyoshi Nakada) almost 4 years ago

Relying on rb_str_new_frozen for unconverted strings does not 
 save memory because copy-on-write is always triggered in 
 read-write I/O loops were subsequent IO#read calls will 
 clobber the given write buffer. 

   buf = ''.b 
   while, buf) 

 This generates a lot of garbage starting with Ruby 2.2 (r44471). 
 For my use case, even `IO.copy_stream` IO.copy_stream generates garbage, since 
 I wrap "write" to do Digest calculation in a single pass. 

 I tried using rb_str_replace and reusing the string as a hidden 
 `(klass (klass == 0)` 0) thread-local, but `rb_str_replace` rb_str_replace attempts CoW 
 optimization by creating new frozen objects, too: 

 So, I'm not sure what to do, temporal locking seems wrong for 
 writing strings (I guess it's for reading?).    I get 
 `test_threaded_flush` test_threaded_flush failures with the following: 

 `IO#syswrite` IO#syswrite has the same problem with garbage.    I can use 
 `IO#write_nonblock` IO#write_nonblock on fast filesystems while holding GVL, 
 I guess...