View Issue Details

IDProjectCategoryView StatusLast Update
0035198mantisbtperformancepublic2025-03-01 18:40
Reporterraspopov Assigned Todregad  
PrioritynormalSeverityminorReproducibilityalways
Status closedResolutionfixed 
Product Version2.27.0 
Target Version2.27.1Fixed in Version2.27.1 
Summary0035198: Caching language loading can be more efficient.
Description

Found it almost by accident when I was researching how MantisBT works with languages. The current set of plugins uses languages before caching, which repeatedly causes the array of languages and their codes to be rebuilt. The caching code in core.php probably needs to be placed a bit higher than the plugin loading code.

TagsNo tags attached.

Activities

raspopov

raspopov

2024-12-21 13:55

reporter   ~0069595

PR: 2060.

raspopov

raspopov

2024-12-21 14:00

reporter   ~0069596

Last edited: 2024-12-21 14:01

I noticed that one more optimization wouldn't hurt the language loading code, replacing the expensive preg_replace() function which is called (2500 times) for all defined variables to look for translations.

PR 2059.

raspopov

raspopov

2024-12-23 10:55

reporter   ~0069600

Last edited: 2024-12-23 10:57

Program performance and performance measurement as we know it is a very tricky thing where intuition usually fools the programmer.

Knowing this, I decided to measure the code performance before and after the commit once again. And ... my goodness I didn't find any performance gain after replacing the seemingly heavy preg_replace() with a couple of simple, easy and fast strncmp() and substr(). Perhaps I measured without preloading or I was just so happy that the function disappeared from the hot functions and did not pay attention to the overall performance of the parent function. The PHP function call overhead is so high that the second call just ate up all the performance gain and the two “fast” functions work about the same as one “slow” function.

I've attached a graphical performance report for different versions (the unit on the charts is 10ns) of lang_load() function:

The charts show that only the last variant has a performance gain. I did not evaluate memory, perhaps preg_replace() has more memory usage.

PR: 2061.

dregad

dregad

2024-12-23 12:23

developer   ~0069601

Wow you sure spent a lot of time profiling this, for a 0.7 ms gain ;-)
Thanks for your optimization efforts.

raspopov

raspopov

2024-12-23 14:01

reporter   ~0069602

Basically, the idea is to get to know the code base and understand how things actually work. I also want to refresh my PHP knowledge and learn about debugging and profiling tools that I haven't used before (my main language is C++). Thanks for the opportunity and for being so patient.

Related Changesets

MantisBT: master-2.27 9e2b936c

2024-12-21 01:43

raspopov

Committer: dregad


Details Diff
Optimize lang_load() function

Replacing preg_replace() with strncmp() and substr().

Note: the original implementation used str_starts_with() but that
requires PHP 8.0 or later, and we're on 7.4.

Fixes 0035198, PR https://github.com/mantisbt/mantisbt/pull/2059

Signed-off-by: Damien Regad <dregad@mantisbt.org>
Affected Issues
0035198
mod - core/lang_api.php Diff File

MantisBT: master-2.27 317abee4

2024-12-21 13:43

raspopov

Committer: dregad


Details Diff
Fix caching when current language is 'auto'

Caching the languages before initializing the plugin API results in a
single call instead of a minimum of five.

Fixes 0035198, PR https://github.com/mantisbt/mantisbt/pull/2060

Signed-off-by: Damien Regad <dregad@mantisbt.org>
Affected Issues
0035198
mod - core.php Diff File

MantisBT: master-2.27 36481ac9

2024-12-23 10:42

raspopov

Committer: dregad


Details Diff
Fix lang_load() performance again

Micro-optimization, using string index comparison instead of strncmp(),
as explained in [1].

Fixes 0035198, PR https://github.com/mantisbt/mantisbt/pull/2061

[1]: https://mantisbt.org/bugs/view.php?id=35198#c69600

Signed-off-by: Damien Regad <dregad@mantisbt.org>
Affected Issues
0035198
mod - core/lang_api.php Diff File