While very few users might have the administrative permission, a call to user_access() takes a fraction of the resources that loading and access-checking an entity does and won’t cause a measurable delay. Both checks are necessary regardless, but the order is important. Whether this kind of optimization is a problem depends on the relative performance increase you hope to gain.įor example, when checking access rights, you may need to check an administrative permission via user_access() as well as access permissions based on an entity ID, which requires loading the entity via entity_load() first. This comes back to context-it’s not so much the content of the code itself that determines its performance, but how exactly it is executed. The only way to know the effect of this is to create a realistic scenario or test on a real install, and see which code execution path is actually the most common. However, if $array doesn’t exist or is set to NULL, then the code actually has to do more work-checking isset() then the array_key_exists(), as well as the || operator-all of which is going to be slower than just running array_key_exists() in the first place! The last example is semantically identical to just an array_key_exists() call, but in the case that $array is set to a non- NULL value, only the isset() check needs to be made, avoiding the more expensive function call. isset ( $array ) || array_key_exists ( 'foo', $array ) // returns TRUE. array_key_exists ( 'foo', $array ) // returns TRUE. You might write code like this: NULL ) isset ( $array ) // returns FALSE. Let’s assume you had some code that definitely needed to use array_key_exists() because of the NULL check, but you wanted to try to run the faster isset() first, to skip the function call when it’s not needed. Hence, a common micro-optimization is to use isset() unless it’s absolutely necessary to check for NULL. This is a function that tells you if an array key exists regardless of the value.įunction calls in PHP have more overhead than language constructs, so isset() takes less time than a function call, and while the semantics are different, they can be used interchangeably if you don’t need to explicitly check for array keys set to NULL. This is a language construct that tells you whether a variable is set or not, and returns false if that variable is explicitly set to NULL. The following are some general questions to ask, before you start trying to optimize at all: When writing or reviewing code, it’s important to think of the context the code will be executed in-both the immediate use case and whether it might also be applied to other contexts. There are no hard and fast rules about what makes code “fast” or “slow”-exactly the same code could be acceptable in one situation but not in another, and performance often needs to be balanced against other programming issues such as testability, readability, and maintainability. Whether any of these turns out to be a performance bottleneck is always down to context. All code uses at least some CPU and memory, and all sites will access the database and filesystem and potentially make network requests. Performance and scalability issues in code can affect CPU, memory, filesystem, database, and network usage, either individually or in combination. Before discussing the APIs and patterns that Drupal provides, it’s worth discussing which types of issues are often introduced when writing Drupal code.
0 Comments
Leave a Reply. |