Skip to content

Commit 256f320

Browse files
authored
[mono] Remove unnecessary indirect use of managed wcslen from AppContext setup (#101318)
Remove unnecessary indirect use of managed wcslen from AppContext setup since it pulls in vector dependencies and we already know the length of each string
1 parent a9f43da commit 256f320

File tree

2 files changed

+50
-13
lines changed

2 files changed

+50
-13
lines changed

src/libraries/System.Private.CoreLib/src/System/AppContext.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,17 @@ public static void SetSwitch(string switchName, bool isEnabled)
141141
}
142142
}
143143

144-
#if !NATIVEAOT
144+
#if MONO
145+
internal static unsafe void Setup(char** pNames, uint* pNameLengths, char** pValues, uint* pValueLengths, int count)
146+
{
147+
Debug.Assert(s_dataStore == null, "s_dataStore is not expected to be inited before Setup is called");
148+
s_dataStore = new Dictionary<string, object?>(count);
149+
for (int i = 0; i < count; i++)
150+
{
151+
s_dataStore.Add(new string(pNames[i], 0, (int)pNameLengths[i]), new string(pValues[i], 0, (int)pValueLengths[i]));
152+
}
153+
}
154+
#elif !NATIVEAOT
145155
internal static unsafe void Setup(char** pNames, char** pValues, int count)
146156
{
147157
Debug.Assert(s_dataStore == null, "s_dataStore is not expected to be inited before Setup is called");

src/mono/mono/metadata/appdomain.c

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,11 @@ static const char *
9999
runtimeconfig_json_get_buffer (MonovmRuntimeConfigArguments *arg, MonoFileMap **file_map, gpointer *buf_handle);
100100

101101
static void
102-
runtimeconfig_json_read_props (const char *ptr, const char **endp, int nprops, gunichar2 **dest_keys, gunichar2 **dest_values);
102+
runtimeconfig_json_read_props (
103+
const char *ptr, const char **endp, int nprops,
104+
gunichar2 **dest_keys, guint32 *dest_key_lengths,
105+
gunichar2 **dest_values, guint32 *dest_value_lengths
106+
);
103107

104108
static MonoLoadFunc load_function = NULL;
105109

@@ -846,17 +850,19 @@ void
846850
mono_runtime_install_appctx_properties (void)
847851
{
848852
ERROR_DECL (error);
849-
gpointer args [3];
853+
gpointer args [5];
850854
int n_runtimeconfig_json_props = 0;
851855
int n_combined_props;
852856
gunichar2 **combined_keys;
853857
gunichar2 **combined_values;
858+
guint32 *combined_key_lengths;
859+
guint32 *combined_value_lengths;
854860
MonoFileMap *runtimeconfig_json_map = NULL;
855861
gpointer runtimeconfig_json_map_handle = NULL;
856862
const char *buffer_start = runtimeconfig_json_get_buffer (runtime_config_arg, &runtimeconfig_json_map, &runtimeconfig_json_map_handle);
857863
const char *buffer = buffer_start;
858864

859-
MonoMethod *setup = mono_class_get_method_from_name_checked (mono_class_get_appctx_class (), "Setup", 3, 0, error);
865+
MonoMethod *setup = mono_class_get_method_from_name_checked (mono_class_get_appctx_class (), "Setup", 5, 0, error);
860866
g_assert (setup);
861867

862868
// FIXME: TRUSTED_PLATFORM_ASSEMBLIES is very large
@@ -867,19 +873,31 @@ mono_runtime_install_appctx_properties (void)
867873

868874
n_combined_props = n_appctx_props + n_runtimeconfig_json_props;
869875
combined_keys = g_new0 (gunichar2 *, n_combined_props);
876+
combined_key_lengths = g_new0 (guint32, n_combined_props);
870877
combined_values = g_new0 (gunichar2 *, n_combined_props);
878+
combined_value_lengths = g_new0 (guint32, n_combined_props);
871879

872880
for (int i = 0; i < n_appctx_props; ++i) {
873-
combined_keys [i] = g_utf8_to_utf16 (appctx_keys [i], -1, NULL, NULL, NULL);
874-
combined_values [i] = g_utf8_to_utf16 (appctx_values [i], -1, NULL, NULL, NULL);
881+
glong num_chars;
882+
combined_keys [i] = g_utf8_to_utf16 (appctx_keys [i], -1, NULL, &num_chars, NULL);
883+
// HACK: items_written from g_utf8_to_utf16 includes the null terminator unless you pass an explicit length.
884+
combined_key_lengths [i] = GLONG_TO_UINT32 (num_chars ? num_chars - 1 : 0);
885+
combined_values [i] = g_utf8_to_utf16 (appctx_values [i], -1, NULL, &num_chars, NULL);
886+
combined_value_lengths [i] = GLONG_TO_UINT32 (num_chars ? num_chars - 1 : 0);
875887
}
876888

877-
runtimeconfig_json_read_props (buffer, &buffer, n_runtimeconfig_json_props, combined_keys + n_appctx_props, combined_values + n_appctx_props);
889+
runtimeconfig_json_read_props (
890+
buffer, &buffer, n_runtimeconfig_json_props,
891+
combined_keys + n_appctx_props, combined_key_lengths + n_appctx_props,
892+
combined_values + n_appctx_props, combined_value_lengths + n_appctx_props
893+
);
878894

879-
/* internal static unsafe void Setup(char** pNames, char** pValues, int count) */
895+
/* internal static unsafe void Setup(char** pNames, uint* pNameLengths, char** pValues, uint* pValueLengths, int count) */
880896
args [0] = combined_keys;
881-
args [1] = combined_values;
882-
args [2] = &n_combined_props;
897+
args [1] = combined_key_lengths;
898+
args [2] = combined_values;
899+
args [3] = combined_value_lengths;
900+
args [4] = &n_combined_props;
883901

884902
mono_runtime_invoke_checked (setup, NULL, args, error);
885903
mono_error_assert_ok (error);
@@ -900,6 +918,8 @@ mono_runtime_install_appctx_properties (void)
900918
}
901919
g_free (combined_keys);
902920
g_free (combined_values);
921+
g_free (combined_key_lengths);
922+
g_free (combined_value_lengths);
903923
for (int i = 0; i < n_appctx_props; ++i) {
904924
g_free (appctx_keys [i]);
905925
g_free (appctx_values [i]);
@@ -949,17 +969,24 @@ runtimeconfig_json_get_buffer (MonovmRuntimeConfigArguments *arg, MonoFileMap **
949969
}
950970

951971
static void
952-
runtimeconfig_json_read_props (const char *ptr, const char **endp, int nprops, gunichar2 **dest_keys, gunichar2 **dest_values)
972+
runtimeconfig_json_read_props (
973+
const char *ptr, const char **endp, int nprops,
974+
gunichar2 **dest_keys, guint32 *dest_key_lengths,
975+
gunichar2 **dest_values, guint32 *dest_value_lengths
976+
)
953977
{
954978
for (int i = 0; i < nprops; ++i) {
955979
int str_len;
980+
glong chars_written;
956981

957982
str_len = mono_metadata_decode_value (ptr, &ptr);
958-
dest_keys [i] = g_utf8_to_utf16 (ptr, str_len, NULL, NULL, NULL);
983+
dest_keys [i] = g_utf8_to_utf16 (ptr, str_len, NULL, &chars_written, NULL);
984+
dest_key_lengths [i] = GLONG_TO_UINT32 (chars_written);
959985
ptr += str_len;
960986

961987
str_len = mono_metadata_decode_value (ptr, &ptr);
962-
dest_values [i] = g_utf8_to_utf16 (ptr, str_len, NULL, NULL, NULL);
988+
dest_values [i] = g_utf8_to_utf16 (ptr, str_len, NULL, &chars_written, NULL);
989+
dest_value_lengths [i] = GLONG_TO_UINT32 (chars_written);
963990
ptr += str_len;
964991
}
965992

0 commit comments

Comments
 (0)