Skip to content

Commit ceed9ea

Browse files
Optimize performance: cache Locale objects, EmailValidator, and improve hex conversion
Co-authored-by: thomasturrell <1552612+thomasturrell@users.noreply.github.com>
1 parent dc02da4 commit ceed9ea

4 files changed

Lines changed: 28 additions & 17 deletions

File tree

xapi-client/src/main/java/dev/learning/xapi/client/StatementHttpMessageWriter.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -188,10 +188,15 @@ private List<Object> getParts(Object object) {
188188
*/
189189
private Stream<Attachment> getRealAttachments(Statement statement) {
190190

191+
Stream<Attachment> stream;
192+
191193
// handle the rare scenario when a sub-statement has an attachment
192-
var stream = statement.getObject() instanceof final SubStatement substatement
193-
&& substatement.getAttachments() != null ? substatement.getAttachments().stream()
194-
: Stream.<Attachment>empty();
194+
if (statement.getObject() instanceof final SubStatement substatement
195+
&& substatement.getAttachments() != null) {
196+
stream = substatement.getAttachments().stream();
197+
} else {
198+
stream = Stream.empty();
199+
}
195200

196201
if (statement.getAttachments() != null) {
197202
stream = Stream.concat(stream, statement.getAttachments().stream());

xapi-model/src/main/java/dev/learning/xapi/model/Attachment.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -210,11 +210,10 @@ private static String sha256Hex(byte[] data) {
210210
final var hash = digest.digest(data);
211211
final var hexString = new StringBuilder(2 * hash.length);
212212
for (final byte element : hash) {
213-
final var hex = Integer.toHexString(0xff & element);
214-
if (hex.length() == 1) {
215-
hexString.append('0');
216-
}
217-
hexString.append(hex);
213+
// Use bitwise operations to avoid string allocation per byte
214+
final int value = 0xff & element;
215+
hexString.append(Character.forDigit(value >>> 4, 16));
216+
hexString.append(Character.forDigit(value & 0x0f, 16));
218217
}
219218
return hexString.toString();
220219
}

xapi-model/src/main/java/dev/learning/xapi/model/LanguageMap.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ public class LanguageMap extends LinkedHashMap<Locale, String> {
2626

2727
private static final long serialVersionUID = 7375610804995032187L;
2828

29+
/**
30+
* Cached undefined locale instance to avoid repeated object creation.
31+
*/
32+
private static final Locale UNDEFINED_LOCALE = new Locale("und");
33+
2934
/**
3035
* Constructs an empty LanguageMap.
3136
*/
@@ -49,7 +54,7 @@ public LanguageMap(Map<Locale, String> languageMap) {
4954
* @param value to be added with the undefined locale as a key
5055
*/
5156
public void put(String value) {
52-
this.put(new Locale("und"), value);
57+
this.put(UNDEFINED_LOCALE, value);
5358
}
5459

5560
/**
@@ -83,15 +88,15 @@ public String get(List<LanguageRange> languageRanges) {
8388
}
8489

8590
// Otherwise return UND
86-
final var und = get(new Locale("und"));
91+
final var und = get(UNDEFINED_LOCALE);
8792
if (und != null) {
8893
return und;
8994
}
9095

9196
// Otherwise return first
92-
final var first = keySet().stream().findFirst();
93-
if (first.isPresent()) {
94-
return get(first.get());
97+
final var iterator = keySet().iterator();
98+
if (iterator.hasNext()) {
99+
return get(iterator.next());
95100
}
96101

97102
// Map must be empty

xapi-model/src/main/java/dev/learning/xapi/model/validation/internal/validators/MboxValidator.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,14 @@ public class MboxValidator extends DisableableValidator<Mbox, String> {
2121

2222
public static final String PREFIX = "mailto:";
2323

24-
EmailValidator emailValidator;
24+
/**
25+
* Cached email validator instance to avoid repeated object creation.
26+
*/
27+
private static final EmailValidator EMAIL_VALIDATOR = new EmailValidator();
2528

2629
@Override
2730
public void initialize(Mbox mbox) {
28-
29-
emailValidator = new EmailValidator();
31+
// No initialization needed - using static validator
3032
}
3133

3234
@Override
@@ -36,7 +38,7 @@ public boolean isValidIfEnabled(String value, ConstraintValidatorContext context
3638
}
3739

3840
return value.startsWith(PREFIX)
39-
&& emailValidator.isValid(value.substring(PREFIX.length()), context);
41+
&& EMAIL_VALIDATOR.isValid(value.substring(PREFIX.length()), context);
4042
}
4143

4244
}

0 commit comments

Comments
 (0)