Skip to content

Commit

Permalink
Size these maps correctly
Browse files Browse the repository at this point in the history
Avoiding resizes and wasted memory
  • Loading branch information
joegallo committed Oct 18, 2023
1 parent 803c510 commit f8348bb
Showing 1 changed file with 8 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.elasticsearch.common.logging.DeprecationLogger;
import org.elasticsearch.common.network.InetAddresses;
import org.elasticsearch.common.network.NetworkAddress;
import org.elasticsearch.common.util.Maps;
import org.elasticsearch.core.Assertions;
import org.elasticsearch.ingest.AbstractProcessor;
import org.elasticsearch.ingest.IngestDocument;
Expand All @@ -35,7 +36,6 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
Expand Down Expand Up @@ -218,8 +218,8 @@ private Map<String, Object> retrieveCityGeoData(GeoIpDatabase geoIpDatabase, Ine
Continent continent = response.getContinent();
Subdivision subdivision = response.getMostSpecificSubdivision();

Map<String, Object> geoData = new HashMap<>();
for (Property property : this.properties) {
Map<String, Object> geoData = Maps.newMapWithExpectedSize(properties.size());
for (Property property : properties) {
switch (property) {
case IP -> geoData.put("ip", NetworkAddress.format(ipAddress));
case COUNTRY_ISO_CODE -> {
Expand Down Expand Up @@ -272,7 +272,7 @@ private Map<String, Object> retrieveCityGeoData(GeoIpDatabase geoIpDatabase, Ine
Double latitude = location.getLatitude();
Double longitude = location.getLongitude();
if (latitude != null && longitude != null) {
Map<String, Object> locationObject = new HashMap<>();
Map<String, Object> locationObject = Maps.newMapWithExpectedSize(2);
locationObject.put("lat", latitude);
locationObject.put("lon", longitude);
geoData.put("location", locationObject);
Expand All @@ -291,8 +291,8 @@ private Map<String, Object> retrieveCountryGeoData(GeoIpDatabase geoIpDatabase,
Country country = response.getCountry();
Continent continent = response.getContinent();

Map<String, Object> geoData = new HashMap<>();
for (Property property : this.properties) {
Map<String, Object> geoData = Maps.newMapWithExpectedSize(properties.size());
for (Property property : properties) {
switch (property) {
case IP -> geoData.put("ip", NetworkAddress.format(ipAddress));
case COUNTRY_ISO_CODE -> {
Expand Down Expand Up @@ -327,8 +327,8 @@ private Map<String, Object> retrieveAsnGeoData(GeoIpDatabase geoIpDatabase, Inet
String organization_name = response.getAutonomousSystemOrganization();
Network network = response.getNetwork();

Map<String, Object> geoData = new HashMap<>();
for (Property property : this.properties) {
Map<String, Object> geoData = Maps.newMapWithExpectedSize(properties.size());
for (Property property : properties) {
switch (property) {
case IP -> geoData.put("ip", NetworkAddress.format(ipAddress));
case ASN -> {
Expand Down

0 comments on commit f8348bb

Please sign in to comment.