Repository navigation
Dart: cache function pointers - #1830
Merged
Merged
Conversation
---------- Motivation ---------- In the case of the generated Dart code each function call looked up for the native function pointer to execute C++ code. The same applied to property getters and setters. Instead of caching the obtained pointer to avoid excessive lookups, the code executed it for each invocation of any function. Internally, the 'DynamicLibrary.lookupFunction()' executes 'dlsym()' or similar OS level function that must lock the global linker mutex. Because Flutter 3.29 or newer executes the Dart code in the main platform thread, the frequent execution of 'dlsym()' and locking of global mutex may encounter contention when other threads load dynamic library or look for symbols. Frequent lookups cause the following: - performance penalty is there, because we always need to lookup the symbol and lock global mutex - the locking of mutex can lead to ANRs of the main thread in Flutter 3.29+ ---------- Solution ---------- This commit implements caching of the function pointers via usage of private global variables. The private variables are initialized lazily in Dart. It means that symbol is loaded on the first call to the function. This reduces the possibility of ANRs. Signed-off-by: Patryk Wrobel <183546751+pwrobeldev@users.noreply.github.com>
The caching of function pointers is visible. Signed-off-by: Patryk Wrobel <183546751+pwrobeldev@users.noreply.github.com>
To ensure that declared private variable is used do not lookup symbol in getter of cached property. Signed-off-by: Patryk Wrobel <183546751+pwrobeldev@users.noreply.github.com>
Signed-off-by: Patryk Wrobel <183546751+pwrobeldev@users.noreply.github.com>
The partials were not used. This is dead code. It is removed by this commit. Signed-off-by: Patryk Wrobel <183546751+pwrobeldev@users.noreply.github.com>
pwrobeldev
commented
Oct 14, 2026
Hsilgos
previously approved these changes
Oct 15, 2026
When we have two nested interfaces, which extend the same parent then Gluecodium generates duplicated name. This triggers compilation error. In the next commit the fix will be delivered. Signed-off-by: Patryk Wrobel <183546751+pwrobeldev@users.noreply.github.com>
In the previous commit the names collision for nested interfaces that inherit from the same parent interface was showed. This commit resolves the problem by injecting the type name that owns the function (in case when native symbol is identical). Signed-off-by: Patryk Wrobel <183546751+pwrobeldev@users.noreply.github.com>
Show that owning type name is included in the name of cached variable with function. Signed-off-by: Patryk Wrobel <183546751+pwrobeldev@users.noreply.github.com>
Hsilgos
approved these changes
Oct 19, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
---------- Motivation ----------
In the case of the generated Dart code each function call looked up
for the native function pointer to execute C++ code. The same applied
to property getters and setters.
Instead of caching the obtained pointer to avoid excessive lookups,
the code executed it for each invocation of any function.
Internally, the 'DynamicLibrary.lookupFunction()' executes 'dlsym()' or
similar OS level function that must lock the global linker mutex.
Because Flutter 3.29 or newer executes the Dart code in the main platform
thread, the frequent execution of 'dlsym()' and locking of global mutex
may encounter contention when other threads load dynamic library or look
for symbols.
Frequent lookups cause the following:
and lock global mutex
---------- Solution ----------
This commit implements caching of the function pointers via usage of private
global variables.
The private variables are initialized lazily in Dart. It means that symbol
is loaded on the first call to the function.
This reduces the possibility of ANRs.