Skip to content
This repository was archived by the owner on Oct 25, 2021. It is now read-only.

Avoid copying const string constants to the stack.#674

Open
jaykrell wants to merge 1 commit intomono:mainfrom
jaykrell:master
Open

Avoid copying const string constants to the stack.#674
jaykrell wants to merge 1 commit intomono:mainfrom
jaykrell:master

Conversation

@jaykrell
Copy link
Copy Markdown

No description provided.

implementation.WriteLine ($"__method = mono_get_method (__{AssemblySafeName}_image, 0x{MetadataToken:X8}, {ObjCTypeName}_class);");
implementation.WriteLineUnindented ("#else");
implementation.WriteLine ($"const char __method_name [] = \"{ManagedTypeName}:{MonoSignature}\";");
implementation.WriteLine ($"static const char __method_name [] = \"{ManagedTypeName}:{MonoSignature}\";");
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will this generate the same native code as if the string constant had been passed directly to the method call?

i.e.:

implementation.WriteLine ($"__method = mono_embeddinator_lookup_method (\"{ManagedTypeName}:{MonoSignature}\", {ObjCTypeName}_class);");

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another way is to pass the literal directly.
There are tradeoffs either way.
With msvc:
Literals can be writable or not, depending on version/flags.
Literals can be single-instanced or not, depending on version/flags.
static const will be not writable and not single-instanced.
Ideal is readonly and single instanced.
Non-static const at least with msvc will copy to stack.
Though I have seen clang optimize similar, kinda surprising.
Perhaps literals w/o temporary are best.
Sometimes people will also assign the literal to a pointer.
That is a wasted assignment but only of a pointer, so minor.

Base automatically changed from master to main March 9, 2021 10:25
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants