SObjectType.getDescribe() and SObjectField.getDescribe() increase Apex CPU consumption in API version 44+
Last updated 20 days ago ·Reference W-6105723 ·Reported By 191 users
In Review
Summary
SObjectType.getDescribe() and SObjectField.getDescribe() consume more Apex CPU time if API version is 44.0 or above.
Repro
1) In Workbench (https://workbench.developerforce.com), set the API version to 43.0, and run an the following Apex code in an anonymous block:
Map<String, Schema.SObjectType> globalDescribe = Schema.getGlobalDescribe();
for(Schema.SObjectType objectType : globalDescribe.values()) {
Schema.DescribeSObjectResult objDescribe = objectType.getDescribe();
for(Schema.SObjectField fieldType : objDescribe.fields.getMap().values()) {
Schema.DescribeFieldResult fieldDescribe = fieldType.getDescribe();
}
}
System.debug('CPU Time: ' + Limits.getCpuTime());
System.debug('Number of Entities: ' + globalDescribe.size());
You'll see an output similar to the one shown below:
|USER_DEBUG|[8]|DEBUG|CPU Time: 2559
|USER_DEBUG|[9]|DEBUG|Number of Entities: 548
2) Then, change the API version to 44.0, and run the code again. You'll see it consumes about 5 times more Apex CPU time in 44.0 even though the number of entities doesn't increase substantially.
|USER_DEBUG|[8]|DEBUG|CPU Time: 12351
|USER_DEBUG|[9]|DEBUG|Number of Entities: 558
Workaround
Change the affected Apex class' API version to 43.0 or below.
Ongoing performance review by R&D has indicated that there is still sub-optimal performance on describe calls in newer API versions. As a result this known issue is being re-opened and related to a new work item (W-6105723) reflecting more optimizations in this area. The initial fix remains in place and did improve performance, but did not fully resolve the issue.
--- 10/3/2019 ---
An improvement that allows deferring loading of describe attributes until first use instead of calculating on construction is included in Spring '20. This avoid overhead associated with pre-calculating attributes that may go unused and has shown substantial improvement in real-world use cases, particularly with complex customizations to the org schema. This is available via a new method overload on Schema.SObjectField.getDescribe() that allows specifying deferred vs full loading of describe details.
A critical update that is also included in Spring '20, "Use Improved Schema Caching" activates internal bug fixes and optimizations to the schema definition caching layer of the platform, and when enabled makes deferred loading the default behavior.
In other words, you can update your code to force the optimization regardless of the critical update's status (recommended for ISV development), or activate the critical update to opt-in to this change without code updates (recommended for admins of specific orgs).
Reported By (191)



































Is it Fixed?
Any unreleased services, features, statuses, or dates referenced in this or other public statements are not currently available and may not be delivered on time or at all. Customers who purchase our services should make their purchase decisions based upon features that are currently available.