Project: SWT
Bug 311129 [Browser][Mozilla]Code refactoring of Parse method in nsID class
Reported: 2010-04-29 23:37:39 | Status: resolved fixed

Build Identifier: I20080617-2000 The Parse method will create 36 new String objects for each new nsID object. So there will be thousands of temp String objects during the creation of a single browser instance. public nsID(String id) { Parse(id); } public void Parse (String aIDStr) { if (aIDStr == null) throw new Error (); int i = 0; for (; i < 8; i++) m0 = (m0 << 4) + Integer.parseInt (aIDStr.substring (i, i + 1), 16); if (aIDStr.charAt (i++) != '-') throw new Error (); for (; i < 13; i++) m1 = (short)((m1 << 4) + Integer.parseInt (aIDStr.substring (i, i + 1), 16)); if (aIDStr.charAt (i++) != '-') throw new Error (); for (; i < 18; i++) m2 = (short)((m2 << 4) + Integer.parseInt (aIDStr.substring (i, i + 1), 16)); if (aIDStr.charAt (i++) != '-') throw new Error (); for (; i < 21; i++) m3[0] = (byte)((m3[0] << 4) + Integer.parseInt (aIDStr.substring (i, i + 1), 16)); for (; i < 23; i++) m3[1] = (byte)((m3[1] << 4) + Integer.parseInt (aIDStr.substring (i, i + 1), 16)); if (aIDStr.charAt (i++) != '-') throw new Error (); for (; i < 26; i++) m3[2] = (byte)((m3[2] << 4) + Integer.parseInt (aIDStr.substring (i, i + 1), 16)); for (; i < 28; i++) m3[3] = (byte)((m3[3] << 4) + Integer.parseInt (aIDStr.substring (i, i + 1), 16)); for (; i < 30; i++) m3[4] = (byte)((m3[4] << 4) + Integer.parseInt (aIDStr.substring (i, i + 1), 16)); for (; i < 32; i++) m3[5] = (byte)((m3[5] << 4) + Integer.parseInt (aIDStr.substring (i, i + 1), 16)); for (; i < 34; i++) m3[6] = (byte)((m3[6] << 4) + Integer.parseInt (aIDStr.substring (i, i + 1), 16)); for (; i < 36; i++) m3[7] = (byte)((m3[7] << 4) + Integer.parseInt (aIDStr.substring (i, i + 1), 16)); } Could we change it like this? public void Parse(String aIDStr) { if(aIDStr == null) throw new Error (); int i = 0; Integer.parseInt (aIDStr.substring (i, i + 1), 16); for (; i < 8; i++) m0 = (m0 << 4) + Character.digit(aIDStr.charAt(i),16); if (aIDStr.charAt (i++) != '-') throw new Error (); for (; i < 13; i++) m1 = (short)((m1 << 4) + Character.digit(aIDStr.charAt(i),16)); if (aIDStr.charAt (i++) != '-') throw new Error (); for (; i < 18; i++) m2 = (short)((m2 << 4) + Character.digit(aIDStr.charAt(i),16)); if (aIDStr.charAt (i++) != '-') throw new Error (); for (; i < 21; i++) m3[0] = (byte)((m3[0] << 4) + Character.digit(aIDStr.charAt(i),16)); for (; i < 23; i++) m3[1] = (byte)((m3[1] << 4) + Character.digit(aIDStr.charAt(i),16)); if (aIDStr.charAt (i++) != '-') throw new Error (); for (; i < 26; i++) m3[2] = (byte)((m3[2] << 4) + Character.digit(aIDStr.charAt(i),16)); for (; i < 28; i++) m3[3] = (byte)((m3[3] << 4) + Character.digit(aIDStr.charAt(i),16)); for (; i < 30; i++) m3[4] = (byte)((m3[4] << 4) + Character.digit(aIDStr.charAt(i),16)); for (; i < 32; i++) m3[5] = (byte)((m3[5] << 4) + Character.digit(aIDStr.charAt(i),16)); for (; i < 34; i++) m3[6] = (byte)((m3[6] << 4) + Character.digit(aIDStr.charAt(i),16)); for (; i < 36; i++) m3[7] = (byte)((m3[7] << 4) + Character.digit(aIDStr.charAt(i),16)); } Reproducible: Always

Files changed in fix:
bundles/org.eclipse.swt/Eclipse SWT Mozilla/common/org/eclipse/swt/internal/mozilla/nsID.java