Optimize String#to_s - yjit
According to @casperisfine, much of the time on some SFR requests is spent in String#to_s, which just returns self.
I was thinking that this should be pretty easy to optimize. In some cases we might already know the type of self to be a string, in which case it's basically just an identity op. For the other cases we could guard that the type of self is a T_STRING
which should also be quicker than calling into a C function I think. For the other cases we could call into the original C function?
@jhawthorn Is there complexity I'm not seeing and would you have time to take this one on? :)
3 Answer:
about 15% of the time on some SFR requests is spent in String#to_s
To clarify a bit, it might have been a one off, and I'm always a bit doubtful of the StackProf results, but yes it reported 5ms in String#to_s
, mostly coming from a tracing library. But more generally the pattern is:
def initialize(arg)
@arg = arg.to_s
end
But I suppose it's tricky, in term of ISeq to_s
is
opt_send_without_block <calldata!mid:to_s, argc:0, ARGS_SIMPLE>
So you'd need to check:
String#to_s
wasn't redefined or refinedarg
isT_STRING
AND its class isrb_cString
.
But yeah, being able to compile out these useless casting calls could be interesting.
I believe we can do this relatively easily by adding it to our method codegen table, which should cover both checking for to_s redefinition and will check the class.
I did some profiling on this with perf
and it does look like we spend a sizable amount of cycles for the simple case even in railsbench
. On my profile it's about 1% of he cycles out of all cycles spent in generated code. I sent a PR for this.
Read next
- jest @jest/types config is missing timers option - TypeScript
- qBittorrent qBittorrent has crashed - Cplusplus
- appium-desktop An unknown server-side error occurred while processing the command. Original error: Could not find adb Please set the ANDROID_HOME environment variable with the Android SDK root directory path. - JavaScript
- Reshape function works only for numpy array and raise error with xarray variable. Python
- vcpkg [osgEarth] update to <3.2> - CMake
- keras generator seems to be memory leak(tf.1.3 keras:2.0.9) - Python
- MISP Issue after 2.4.134 to 2.4.135 upgrade - PHP
- Argument of type 'Handler' is not assignable to parameter of type 'UntypedServiceImplementation' - grpc_tools_node_protoc_ts