[TYPO3-core] RFC #14997: Performance: t3lib_db->escapeStrForLike() is using preg_replace, which is not necessary

François Suter fsu-lists at cobweb.ch
Sat Sep 18 10:38:48 CEST 2010


Hi,

> Problem:
> The function t3lib_db->escapeStrForLike() is using preg_replace to escape
> the values '%' and '_' with a '\' for a sql-like-statement.
> preg_replace takes a lot of recources and is not necessary for such a
> simple replacement.
>
> Solution:
> use str_replace instead.

Sorry for dropping out of sight for such a long time. Here's v3 using 
addcslashes() as suggested by Markus, which is indeed far better in 
terms of performance:

Before:  foo\_bar\%
After:   foo\\_bar\\% (preg_replace)  | Parsetime: 0.170912
After:   foo\\_bar\\% (str_replace)   | Parsetime: 0.160822
After:   foo\\_bar\\% (addcslashes)   | Parsetime: 0.054765

(on my Mac laptop, running PHP 5.3.1)

+1 on reading and testing the solution with addcslashes().

Note that there's a new unit test in this patch, which ensure that the 
behavior of t3lib_db::escapeStrForLike() is covered.

Cheers

-- 

Francois Suter
Cobweb Development Sarl - http://www.cobweb.ch
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: 14997_v3.diff
URL: <http://lists.typo3.org/pipermail/typo3-team-core/attachments/20100918/99b335c5/attachment.asc>


More information about the TYPO3-team-core mailing list