Skip to content

Commit b594df3

Browse files
committed
Optimized library performance.
1 parent ca817b1 commit b594df3

File tree

1 file changed

+42
-24
lines changed

1 file changed

+42
-24
lines changed

IP2Location.php

Lines changed: 42 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class Database {
3232
*
3333
* @var string
3434
*/
35-
const VERSION = '8.0.3';
35+
const VERSION = '8.1.0';
3636

3737
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
3838
// Error field constants ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
@@ -616,6 +616,9 @@ class Database {
616616
private $year;
617617
private $month;
618618
private $day;
619+
620+
// This variable will be used to hold the raw row of columns's positions
621+
private $raw_positions_row;
619622

620623
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
621624
// Default fields //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@@ -1063,11 +1066,13 @@ private function read($pos, $len) {
10631066
* @return string
10641067
*/
10651068
private function readString($pos, $additional = 0) {
1066-
// Get the actual pointer to the string's head
1067-
$spos = $this->readWord($pos) + $additional;
1068-
1069+
1070+
// Get the actual pointer to the string's head by extract from the raw row
1071+
$spos = unpack('V', substr($this->raw_positions_row, $pos, 4))[1] + $additional;
1072+
10691073
// Read as much as the length (first "string" byte) indicates
1070-
return $this->read($spos + 1, $this->readByte($spos + 1));
1074+
return $this->read($spos + 1, $this->readByte($spos + 1));
1075+
10711076
}
10721077

10731078
/**
@@ -1142,8 +1147,8 @@ private function readCountryNameAndCode($pointer) {
11421147
// Read the country code and name (the name shares the country's pointer,
11431148
// but it must be artificially displaced 3 bytes ahead: 2 for the country code, one
11441149
// for the country name's length)
1145-
$countryCode = $this->readString($pointer + self::$columns[self::COUNTRY_CODE][$this->type]);
1146-
$countryName = $this->readString($pointer + self::$columns[self::COUNTRY_NAME][$this->type], 3);
1150+
$countryCode = $this->readString(self::$columns[self::COUNTRY_CODE][$this->type]);
1151+
$countryName = $this->readString(self::$columns[self::COUNTRY_NAME][$this->type], 3);
11471152
}
11481153

11491154
return [$countryName, $countryCode];
@@ -1165,7 +1170,7 @@ private function readRegionName($pointer) {
11651170
$regionName = self::FIELD_NOT_SUPPORTED;
11661171
} else {
11671172
// Read the region name
1168-
$regionName = $this->readString($pointer + self::$columns[self::REGION_NAME][$this->type]);
1173+
$regionName = $this->readString(self::$columns[self::REGION_NAME][$this->type]);
11691174
}
11701175
return $regionName;
11711176
}
@@ -1186,7 +1191,7 @@ private function readCityName($pointer) {
11861191
$cityName = self::FIELD_NOT_SUPPORTED;
11871192
} else {
11881193
// Read the city name
1189-
$cityName = $this->readString($pointer + self::$columns[self::CITY_NAME][$this->type]);
1194+
$cityName = $this->readString(self::$columns[self::CITY_NAME][$this->type]);
11901195
}
11911196
return $cityName;
11921197
}
@@ -1231,7 +1236,7 @@ private function readIsp($pointer) {
12311236
$isp = self::FIELD_NOT_SUPPORTED;
12321237
} else {
12331238
// Read isp name
1234-
$isp = $this->readString($pointer + self::$columns[self::ISP][$this->type]);
1239+
$isp = $this->readString(self::$columns[self::ISP][$this->type]);
12351240
}
12361241
return $isp;
12371242
}
@@ -1252,7 +1257,7 @@ private function readDomainName($pointer) {
12521257
$domainName = self::FIELD_NOT_SUPPORTED;
12531258
} else {
12541259
// Read the domain name
1255-
$domainName = $this->readString($pointer + self::$columns[self::DOMAIN_NAME][$this->type]);
1260+
$domainName = $this->readString(self::$columns[self::DOMAIN_NAME][$this->type]);
12561261
}
12571262
return $domainName;
12581263
}
@@ -1273,7 +1278,7 @@ private function readZipCode($pointer) {
12731278
$zipCode = self::FIELD_NOT_SUPPORTED;
12741279
} else {
12751280
// Read the zip code
1276-
$zipCode = $this->readString($pointer + self::$columns[self::ZIP_CODE][$this->type]);
1281+
$zipCode = $this->readString(self::$columns[self::ZIP_CODE][$this->type]);
12771282
}
12781283
return $zipCode;
12791284
}
@@ -1294,7 +1299,7 @@ private function readTimeZone($pointer) {
12941299
$timeZone = self::FIELD_NOT_SUPPORTED;
12951300
} else {
12961301
// Read the time zone
1297-
$timeZone = $this->readString($pointer + self::$columns[self::TIME_ZONE][$this->type]);
1302+
$timeZone = $this->readString(self::$columns[self::TIME_ZONE][$this->type]);
12981303
}
12991304
return $timeZone;
13001305
}
@@ -1315,7 +1320,7 @@ private function readNetSpeed($pointer) {
13151320
$netSpeed = self::FIELD_NOT_SUPPORTED;
13161321
} else {
13171322
// Read the net speed
1318-
$netSpeed = $this->readString($pointer + self::$columns[self::NET_SPEED][$this->type]);
1323+
$netSpeed = $this->readString(self::$columns[self::NET_SPEED][$this->type]);
13191324
}
13201325
return $netSpeed;
13211326
}
@@ -1338,8 +1343,8 @@ private function readIddAndAreaCodes($pointer) {
13381343
$areaCode = self::FIELD_NOT_SUPPORTED;
13391344
} else {
13401345
// Read IDD and area codes
1341-
$iddCode = $this->readString($pointer + self::$columns[self::IDD_CODE][$this->type]);
1342-
$areaCode = $this->readString($pointer + self::$columns[self::AREA_CODE][$this->type]);
1346+
$iddCode = $this->readString(self::$columns[self::IDD_CODE][$this->type]);
1347+
$areaCode = $this->readString(self::$columns[self::AREA_CODE][$this->type]);
13431348
}
13441349
return [$iddCode, $areaCode];
13451350
}
@@ -1362,8 +1367,8 @@ private function readWeatherStationNameAndCode($pointer) {
13621367
$weatherStationCode = self::FIELD_NOT_SUPPORTED;
13631368
} else {
13641369
// Read weather station name and code
1365-
$weatherStationName = $this->readString($pointer + self::$columns[self::WEATHER_STATION_NAME][$this->type]);
1366-
$weatherStationCode = $this->readString($pointer + self::$columns[self::WEATHER_STATION_CODE][$this->type]);
1370+
$weatherStationName = $this->readString(self::$columns[self::WEATHER_STATION_NAME][$this->type]);
1371+
$weatherStationCode = $this->readString(self::$columns[self::WEATHER_STATION_CODE][$this->type]);
13671372
}
13681373
return [$weatherStationName, $weatherStationCode];
13691374
}
@@ -1388,9 +1393,9 @@ private function readMccMncAndMobileCarrierName($pointer) {
13881393
$mobileCarrierName = self::FIELD_NOT_SUPPORTED;
13891394
} else {
13901395
// Read MCC, MNC, and mobile carrier name
1391-
$mcc = $this->readString($pointer + self::$columns[self::MCC][$this->type]);
1392-
$mnc = $this->readString($pointer + self::$columns[self::MNC][$this->type]);
1393-
$mobileCarrierName = $this->readString($pointer + self::$columns[self::MOBILE_CARRIER_NAME][$this->type]);
1396+
$mcc = $this->readString(self::$columns[self::MCC][$this->type]);
1397+
$mnc = $this->readString(self::$columns[self::MNC][$this->type]);
1398+
$mobileCarrierName = $this->readString(self::$columns[self::MOBILE_CARRIER_NAME][$this->type]);
13941399
}
13951400
return [$mcc, $mnc, $mobileCarrierName];
13961401
}
@@ -1411,7 +1416,7 @@ private function readElevation($pointer) {
14111416
$elevation = self::FIELD_NOT_SUPPORTED;
14121417
} else {
14131418
// Read the elevation
1414-
$elevation = $this->readString($pointer + self::$columns[self::ELEVATION][$this->type]);
1419+
$elevation = $this->readString(self::$columns[self::ELEVATION][$this->type]);
14151420
}
14161421
return $elevation;
14171422
}
@@ -1431,7 +1436,7 @@ private function readUsageType($pointer) {
14311436
// If the field is not suported, return accordingly
14321437
$usageType = self::FIELD_NOT_SUPPORTED;
14331438
} else {
1434-
$usageType = $this->readString($pointer + self::$columns[self::USAGE_TYPE][$this->type]);
1439+
$usageType = $this->readString(self::$columns[self::USAGE_TYPE][$this->type]);
14351440
}
14361441
return $usageType;
14371442
}
@@ -1607,6 +1612,7 @@ public function getDatabaseVersion() {
16071612
* @return mixed|array|boolean
16081613
*/
16091614
public function lookup($ip, $fields = null, $asNamed = true) {
1615+
16101616
// extract IP version and number
16111617
list($ipVersion, $ipNumber) = self::ipVersionAndNumber($ip);
16121618
// perform the binary search proper (if the IP address was invalid, binSearch will return false)
@@ -1618,10 +1624,20 @@ public function lookup($ip, $fields = null, $asNamed = true) {
16181624
$fields = $this->defaultFields;
16191625
}
16201626

1627+
// Get the entire row based on the pointer value
1628+
// The length of the row differs based on the IP version
1629+
if (4 === $ipVersion) {
1630+
$this->raw_positions_row = $this->read($pointer - 1, $this->columnWidth[4] + 4);
1631+
} elseif (6 === $ipVersion) {
1632+
$this->raw_positions_row = $this->read($pointer - 1, $this->columnWidth[6]);
1633+
}
1634+
16211635
// turn fields into an array in case it wasn't already
16221636
$ifields = (array) $fields;
1623-
// add fields if needed
1637+
1638+
// add fields if needed
16241639
if (in_array(self::ALL, $ifields)) {
1640+
16251641
$ifields[] = self::REGION_NAME;
16261642
$ifields[] = self::CITY_NAME;
16271643
$ifields[] = self::ISP;
@@ -1641,6 +1657,8 @@ public function lookup($ip, $fields = null, $asNamed = true) {
16411657
$ifields[] = self::IP_ADDRESS;
16421658
$ifields[] = self::IP_VERSION;
16431659
$ifields[] = self::IP_NUMBER;
1660+
1661+
16441662
}
16451663
// turn into a uniquely-valued array the fast way
16461664
// (see: http://php.net/manual/en/function.array-unique.php#77743)

0 commit comments

Comments
 (0)