Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Device detector2 cache #304

Merged
merged 2 commits into from Jun 8, 2014

Conversation

cbonte
Copy link
Contributor

@cbonte cbonte commented Jun 7, 2014

This patch provides an optimization that caches the parsed DeviceDetector.

Using The DeviceDetector2 branch, before the patch, a test case with 100000 line extracted from a real website, the result was :

Total time: 308 seconds
Requests imported per second: 323.87 requests per second

After the patch :

Total time: 144 seconds
Requests imported per second: 690.84 requests per second

The optimization really depends on the number of different user agents and the bulk size but in the worst case, we can expect nearly the same performance as without the patch.

Note : initially, I expected to provide the patch for the master branch but I prefered to provide one for the DeviceDetector2 branch, using CacheStatic instead of a static array.

By caching parsed DeviceDetectors, we reduce the number of regexes to process.
It allows to process more requests per second when the same user agent is met.
$deviceDetector->setCache(new DeviceDetectorCache('tracker', 86400));
$deviceDetector->parse();
static $deviceDetectorsCache;
$deviceDetectorsCache = new CacheStatic();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Making that variable static doesn't make sense if it is always set to a new instance. Could you add an check if the variable has already been set before?

@cbonte
Copy link
Contributor Author

cbonte commented Jun 7, 2014

You're right, I converted the code too quickly to the DeviceDetector2 branch.
Reading the CacheStatic code, I wonder if it's really a good idea to use it in this case : it could leads in a security issue as someone can forge the User Agent to reuse a key that doesn't provide a DeviceDetector.

I'm going to push a commit that reverts the code to the one I used on master.

The previous commit had at least 2 issues :

one identified by sgiehl, who reported that the static variable was useless
if we always create a new cache instance, and indeed, he's absolutely right !

Another one can be classified as a potential security issue, where a request
can forge a user agent as a key to an object which is not meant to be accessed
(CacheStatic instances share their data).
sgiehl pushed a commit that referenced this pull request Jun 8, 2014
@sgiehl sgiehl merged commit 746e62d into matomo-org:DeviceDetector2 Jun 8, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants