Changeset 17848 in josm


Ignore:
Timestamp:
2021-05-01T22:59:59+02:00 (3 years ago)
Author:
simon04
Message:

see #20829 - Avoid heap allocations in OsmWriter

Location:
trunk
Files:
1 added
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/io/OsmWriter.java

    r17787 r17848  
    224224    public void writeDataSources(DataSet ds) {
    225225        for (DataSource s : ds.getDataSources()) {
    226             out.println("  <bounds minlat='"
    227                     + DecimalDegreesCoordinateFormat.INSTANCE.latToString(s.bounds.getMin())
    228                     +"' minlon='"
    229                     + DecimalDegreesCoordinateFormat.INSTANCE.lonToString(s.bounds.getMin())
    230                     +"' maxlat='"
    231                     + DecimalDegreesCoordinateFormat.INSTANCE.latToString(s.bounds.getMax())
    232                     +"' maxlon='"
    233                     + DecimalDegreesCoordinateFormat.INSTANCE.lonToString(s.bounds.getMax())
    234                     +"' origin='"+XmlWriter.encode(s.origin)+"' />");
     226            out.append("  <bounds minlat='").append(DecimalDegreesCoordinateFormat.INSTANCE.latToString(s.bounds.getMin()));
     227            out.append("' minlon='").append(DecimalDegreesCoordinateFormat.INSTANCE.lonToString(s.bounds.getMin()));
     228            out.append("' maxlat='").append(DecimalDegreesCoordinateFormat.INSTANCE.latToString(s.bounds.getMax()));
     229            out.append("' maxlon='").append(DecimalDegreesCoordinateFormat.INSTANCE.lonToString(s.bounds.getMax()));
     230            out.append("' origin='").append(XmlWriter.encode(s.origin)).append("' />");
     231            out.println();
    235232        }
    236233    }
     
    238235    void writeLatLon(LatLon ll) {
    239236        if (ll != null) {
    240             out.print(" lat='"+LatLon.cDdHighPrecisionFormatter.format(ll.lat())+
    241                      "' lon='"+LatLon.cDdHighPrecisionFormatter.format(ll.lon())+'\'');
     237            out.append(" lat='").append(LatLon.cDdHighPrecisionFormatter.format(ll.lat())).append("'");
     238            out.append(" lon='").append(LatLon.cDdHighPrecisionFormatter.format(ll.lon())).append("'");
    242239        }
    243240    }
     
    264261            out.println(">");
    265262            for (int i = 0; i < w.getNodesCount(); ++i) {
    266                 out.println("    <nd ref='"+w.getNodeId(i) +"' />");
     263                out.append("    <nd ref='").append(String.valueOf(w.getNodeId(i))).append("' />");
     264                out.println();
    267265            }
    268266            addTags(w, "way", false);
     
    281279                out.print("    <member type='");
    282280                out.print(e.getMemberType(i).getAPIName());
    283                 out.println("' ref='"+e.getMemberId(i)+"' role='" +
    284                         XmlWriter.encode(e.getRole(i)) + "' />");
     281                out.append("' ref='").append(String.valueOf(e.getMemberId(i)));
     282                out.append("' role='").append(XmlWriter.encode(e.getRole(i))).append("' />");
     283                out.println();
    285284            }
    286285            addTags(e, "relation", false);
     
    293292     */
    294293    public void visit(Changeset cs) {
    295         out.print("  <changeset id='"+cs.getId()+'\'');
     294        out.append("  <changeset id='").append(String.valueOf(cs.getId())).append("'");
    296295        if (cs.getUser() != null) {
    297             out.print(" user='"+ XmlWriter.encode(cs.getUser().getName()) +'\'');
    298             out.print(" uid='"+cs.getUser().getId() +'\'');
     296            out.append(" user='").append(XmlWriter.encode(cs.getUser().getName())).append("'");
     297            out.append(" uid='").append(String.valueOf(cs.getUser().getId())).append("'");
    299298        }
    300299        Instant createdDate = cs.getCreatedAt();
    301300        if (createdDate != null) {
    302             out.print(" created_at='"+ createdDate +'\'');
     301            out.append(" created_at='").append(String.valueOf(createdDate)).append("'");
    303302        }
    304303        Instant closedDate = cs.getClosedAt();
    305304        if (closedDate != null) {
    306             out.print(" closed_at='"+ closedDate +'\'');
    307         }
    308         out.print(" open='"+ (cs.isOpen() ? "true" : "false") +'\'');
     305            out.append(" closed_at='").append(String.valueOf(closedDate)).append("'");
     306        }
     307        out.append(" open='").append(cs.isOpen() ? "true" : "false").append("'");
    309308        if (cs.getMin() != null) {
    310             out.print(" min_lon='"+ DecimalDegreesCoordinateFormat.INSTANCE.lonToString(cs.getMin()) +'\'');
    311             out.print(" min_lat='"+ DecimalDegreesCoordinateFormat.INSTANCE.latToString(cs.getMin()) +'\'');
     309            out.append(" min_lon='").append(DecimalDegreesCoordinateFormat.INSTANCE.lonToString(cs.getMin())).append("'");
     310            out.append(" min_lat='").append(DecimalDegreesCoordinateFormat.INSTANCE.latToString(cs.getMin())).append("'");
    312311        }
    313312        if (cs.getMax() != null) {
    314             out.print(" max_lon='"+ DecimalDegreesCoordinateFormat.INSTANCE.lonToString(cs.getMax()) +'\'');
    315             out.print(" max_lat='"+ DecimalDegreesCoordinateFormat.INSTANCE.latToString(cs.getMax()) +'\'');
     313            out.append(" max_lon='").append(DecimalDegreesCoordinateFormat.INSTANCE.lonToString(cs.getMax())).append("'");
     314            out.append(" max_lat='").append(DecimalDegreesCoordinateFormat.INSTANCE.latToString(cs.getMax())).append("'");
    316315        }
    317316        out.println(">");
     
    329328            entries.sort(byKeyComparator);
    330329            for (Entry<String, String> e : entries) {
    331                 out.println("    <tag k='"+ XmlWriter.encode(e.getKey()) +
    332                         "' v='"+XmlWriter.encode(e.getValue())+ "' />");
     330                out.append("    <tag k='").append(XmlWriter.encode(e.getKey()));
     331                out.append("' v='").append(XmlWriter.encode(e.getValue())).append("' />");
     332                out.println();
    333333            }
    334334            out.println("  </" + tagname + '>');
     
    347347     */
    348348    protected void addCommon(IPrimitive osm, String tagname) {
    349         out.print("  <"+tagname);
     349        out.append("  <").append(tagname);
    350350        if (osm.getUniqueId() != 0) {
    351             out.print(" id='"+ osm.getUniqueId()+'\'');
     351            out.append(" id='").append(String.valueOf(osm.getUniqueId())).append("'");
    352352        } else
    353353            throw new IllegalStateException(tr("Unexpected id 0 for osm primitive found"));
     
    361361                }
    362362                if (action != null) {
    363                     out.print(" action='"+action+'\'');
     363                    out.append(" action='").append(action).append("'");
    364364                }
    365365            }
    366366            if (!osm.isTimestampEmpty()) {
    367                 out.print(" timestamp='"+osm.getInstant()+'\'');
     367                out.append(" timestamp='").append(String.valueOf(osm.getInstant())).append("'");
    368368            }
    369369            // user and visible added with 0.4 API
    370370            if (osm.getUser() != null) {
    371371                if (osm.getUser().isLocalUser()) {
    372                     out.print(" user='"+XmlWriter.encode(osm.getUser().getName())+'\'');
     372                    out.append(" user='").append(XmlWriter.encode(osm.getUser().getName())).append("'");
    373373                } else if (osm.getUser().isOsmUser()) {
    374374                    // uid added with 0.6
    375                     out.print(" uid='"+ osm.getUser().getId()+'\'');
    376                     out.print(" user='"+XmlWriter.encode(osm.getUser().getName())+'\'');
     375                    out.append(" uid='").append(String.valueOf(osm.getUser().getId())).append("'");
     376                    out.append(" user='").append(XmlWriter.encode(osm.getUser().getName())).append("'");
    377377                }
    378378            }
    379379            if (withVisible) {
    380                 out.print(" visible='"+osm.isVisible()+'\'');
     380                out.append(" visible='").append(String.valueOf(osm.isVisible())).append("'");
    381381            }
    382382        }
    383383        if (osm.getVersion() != 0) {
    384             out.print(" version='"+osm.getVersion()+'\'');
     384            out.append(" version='").append(String.valueOf(osm.getVersion())).append("'");
    385385        }
    386386        if (this.changeset != null && this.changeset.getId() != 0) {
    387             out.print(" changeset='"+this.changeset.getId()+'\'');
     387            out.append(" changeset='").append(String.valueOf(this.changeset.getId())).append("'");
    388388        } else if (osm.getChangesetId() > 0 && !osm.isNew()) {
    389             out.print(" changeset='"+osm.getChangesetId()+'\'');
     389            out.append(" changeset='").append(String.valueOf(osm.getChangesetId())).append("'");
    390390        }
    391391    }
Note: See TracChangeset for help on using the changeset viewer.