Open gbutt-tr opened 2 years ago
@gbutt-tr Would you like to see my version of this in action? https://github.com/dschach/salesforce-trigger-framework/tree/main/force-app/main/default/classes/TriggerHandler.cls#L210
I'd appreciate any feedback.
@gbutt-tr Sorry for the mega-delay. Happy to entertain a PR for this. Thanks!
@gbutt-tr I raised an issue with getHandlerName() here. Im wondering if my issue could be fixed by using types instead. Can you post a couple examples of how you are using Types?
I found this in your fork
@TestVisible private String getHandlerName() { return this.toString().substringBefore(':'); }
Just looked through the history of master, saw this got merged. Im updating mine getHandlerName() to match the above code.
I use
this.handlerName = String.valueOf(this).substring(0, String.valueOf(this).indexOf(':'));
in my version at
https://github.com/dschach/salesforce-trigger-framework/blob/main/force-app/main/default/classes/TriggerHandler.cls
@gbutt-tr @kevinohara80 While exploring this repo, saw this issue. Can anyone, guide me to an article or a programming concept that explains why this solution inproves the performence so dramatically? Thank you!
@gbutt-tr @kevinohara80 While exploring this repo, saw this issue. Can anyone, guide me to an article or a programming concept that explains why this solution inproves the performence so dramatically? Thank you!
The idea, I believe, is that by using .toString()
or String.valueOf(this)
you are forcing the interpreter to cast a trigger handler instance into a string, which internally might involve having the interpreter go through all of its attributes at runtime, and those might pose a bottleneck internally. By using the type directly, you are simply making the interpreter use its type directly, not accessing all of its instance attributes or anything like that.
Bear in mind that all of this is speculation though, since Apex is a black box for us.
@gbutt-tr @kevinohara80 While exploring this repo, saw this issue. Can anyone, guide me to an article or a programming concept that explains why this solution inproves the performence so dramatically? Thank you!
The idea, I believe, is that by using
.toString()
orString.valueOf(this)
you are forcing the interpreter to cast a trigger handler instance into a string, which internally might involve having the interpreter go through all of its attributes at runtime, and those might pose a bottleneck internally. By using the type directly, you are simply making the interpreter use its type directly, not accessing all of its instance attributes or anything like that.Bear in mind that all of this is speculation though, since Apex is a black box for us.
Great Idea, didn't thought about that. Thank you very much Renato!
@kevinohara80 if you're interested, I can create a PR for your review.
We have run into performance issues with getHandlerName. Apparently executing String.valueOf(this) can take a long time to execute if the concrete handler class has a lot of data stored as properties (i.e. storing newList and newMap as properties on your handler class). We have reason to believe this has become more of an issue since Summer 22, but I have no hard evidence on that.
The best solution for us was to replace usage of handlerName Strings with Types throughout the framework. When using types
Here are my test results.
Using Types
Using Strings