* To reduce the overhead of the profiler when calling lua functions we avoid using dmSnPrintf. * dmSnPrintf uses vsnprintf with variable number of arguments but we only need string concatenation for * the most part. Also, we use our knownledge when building the string to get the string length directly * without resorting to strlen. * Building this string is particularly expensive o
| 2113 | * the overhead of the profiler when enabled. |
| 2114 | */ |
| 2115 | const char* GetProfilerString(lua_State* L, int optional_callback_index, const char* source_file_name, const char* function_name, const char* optional_message_name, char* buffer, uint32_t buffer_size) |
| 2116 | { |
| 2117 | if (!ProfileIsInitialized()) |
| 2118 | return 0; |
| 2119 | |
| 2120 | char* w_ptr = buffer; |
| 2121 | const char* w_ptr_end = buffer + buffer_size - 1; |
| 2122 | |
| 2123 | const char* function_source = source_file_name; |
| 2124 | |
| 2125 | if (optional_callback_index != 0) |
| 2126 | { |
| 2127 | LuaFunctionInfo fi; |
| 2128 | if (dmScript::GetLuaFunctionRefInfo(L, optional_callback_index, &fi)) |
| 2129 | { |
| 2130 | if (fi.m_FileName) |
| 2131 | { |
| 2132 | function_source = fi.m_FileName; |
| 2133 | } |
| 2134 | if (fi.m_OptionalName) |
| 2135 | { |
| 2136 | w_ptr = ConcatString(w_ptr, w_ptr_end, fi.m_OptionalName); |
| 2137 | } |
| 2138 | else |
| 2139 | { |
| 2140 | char function_line_number_buffer[16]; |
| 2141 | dmSnPrintf(function_line_number_buffer, sizeof(function_line_number_buffer), "l(%d)", fi.m_LineNumber); |
| 2142 | w_ptr = ConcatString(w_ptr, w_ptr_end, function_line_number_buffer); |
| 2143 | } |
| 2144 | } |
| 2145 | else |
| 2146 | { |
| 2147 | w_ptr = ConcatString(w_ptr, w_ptr_end, "<unknown>"); |
| 2148 | } |
| 2149 | |
| 2150 | } |
| 2151 | else |
| 2152 | { |
| 2153 | w_ptr = ConcatString(w_ptr, w_ptr_end, function_name); |
| 2154 | } |
| 2155 | |
| 2156 | if (optional_message_name) |
| 2157 | { |
| 2158 | w_ptr = ConcatString(w_ptr, w_ptr_end, "["); |
| 2159 | w_ptr = ConcatString(w_ptr, w_ptr_end, optional_message_name); |
| 2160 | w_ptr = ConcatString(w_ptr, w_ptr_end, "]"); |
| 2161 | } |
| 2162 | w_ptr = ConcatString(w_ptr, w_ptr_end, "@"); |
| 2163 | w_ptr = ConcatString(w_ptr, w_ptr_end, function_source); |
| 2164 | *w_ptr++ = 0; |
| 2165 | |
| 2166 | return buffer; |
| 2167 | } |
| 2168 | |
| 2169 | const char* GetTableStringValue(lua_State* L, int table_index, const char* key, const char* default_value) |
| 2170 | { |
no test coverage detected