Skip to content

Commit

Permalink
Device_Detection: get_info(): memoize (#39338)
Browse files Browse the repository at this point in the history
  • Loading branch information
mreishus authored and matticbot committed Sep 11, 2024
1 parent f1c03d2 commit 7c9c60a
Show file tree
Hide file tree
Showing 7 changed files with 130 additions and 91 deletions.
62 changes: 31 additions & 31 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions vendor/automattic/jetpack-device-detection/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [2.1.5-alpha] - unreleased

This is an alpha version! The changes listed here are not final.

### Changed
- Device_Detection::get_info() will now memoize its result

## [2.1.4] - 2024-08-23
### Changed
- Updated package dependencies. [#39004]
Expand Down Expand Up @@ -200,6 +207,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Moving jetpack_is_mobile into a package

[2.1.5-alpha]: https://github.com/Automattic/jetpack-device-detection/compare/v2.1.4...v2.1.5-alpha
[2.1.4]: https://github.com/Automattic/jetpack-device-detection/compare/v2.1.3...v2.1.4
[2.1.3]: https://github.com/Automattic/jetpack-device-detection/compare/v2.1.2...v2.1.3
[2.1.2]: https://github.com/Automattic/jetpack-device-detection/compare/v2.1.1...v2.1.2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,20 @@
*/
class Device_Detection {

/**
* Memoization cache for get_info() results.
*
* @var array
*/
private static $get_info_memo = array();

/**
* Maximum size of the memoization cache.
*
* @var int
*/
private static $max_memo_size = 100;

/**
* Returns information about the current device accessing the page.
*
Expand All @@ -41,6 +55,16 @@ class Device_Detection {
* );
*/
public static function get_info( $ua = '' ) {
// Return memoized result if available.
// phpcs:disable WordPress.Security.ValidatedSanitizedInput
$memo_key = ! empty( $ua ) ? $ua : ( $_SERVER['HTTP_USER_AGENT'] ?? '' );
// Note: UA string used raw for compatibility reasons.
// No sanitization is needed as the value is never output or persisted, and is only used for memoization.
// phpcs:enable WordPress.Security.ValidatedSanitizedInput
if ( isset( self::$get_info_memo[ $memo_key ] ) ) {
return self::$get_info_memo[ $memo_key ];
}

$ua_info = new User_Agent_Info( $ua );

$info = array(
Expand Down Expand Up @@ -68,6 +92,13 @@ public static function get_info( $ua = '' ) {
*/
$info = apply_filters( 'jetpack_device_detection_get_info', $info, $ua, $ua_info );
}

// Memoize the result.
self::$get_info_memo[ $memo_key ] = $info;
if ( count( self::$get_info_memo ) > self::$max_memo_size ) {
array_shift( self::$get_info_memo );
}

return $info;
}

Expand Down
2 changes: 1 addition & 1 deletion vendor/automattic/jetpack-jitm/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"automattic/jetpack-a8c-mc-stats": "^2.0.2",
"automattic/jetpack-assets": "^2.3.8",
"automattic/jetpack-connection": "^4.0.2",
"automattic/jetpack-device-detection": "^2.1.4",
"automattic/jetpack-device-detection": "^2.1.5-alpha",
"automattic/jetpack-logo": "^2.0.4",
"automattic/jetpack-redirect": "^2.0.4",
"automattic/jetpack-status": "^4.0.1"
Expand Down
2 changes: 1 addition & 1 deletion vendor/automattic/jetpack-masterbar/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"automattic/jetpack-assets": "^2.3.8",
"automattic/jetpack-blaze": "^0.22.10",
"automattic/jetpack-compat": "^3.0.2",
"automattic/jetpack-device-detection": "^2.1.4",
"automattic/jetpack-device-detection": "^2.1.5-alpha",
"automattic/jetpack-connection": "^4.0.2",
"automattic/jetpack-jitm": "^3.1.22",
"automattic/jetpack-logo": "^2.0.4",
Expand Down
Loading

0 comments on commit 7c9c60a

Please sign in to comment.