Optimize entities cache

This commit is contained in:
Andrea Cavalli 2022-02-09 21:06:02 +01:00
parent 0327638e0d
commit a202c47c65
3 changed files with 88 additions and 10 deletions

View File

@ -0,0 +1,38 @@
package org.telegram.telegrambots.meta;
import java.nio.charset.StandardCharsets;
public class MemoizedUTF16String {
private final String original;
private byte[] cache;
public MemoizedUTF16String(String string) {
this.original = string;
}
public MemoizedUTF16String(byte[] utf16Data) {
this.original = null;
this.cache = utf16Data;
}
public byte[] getBytes() {
if (original != null && cache == null) {
byte[] newCache = original.getBytes(StandardCharsets.UTF_16LE);
this.cache = newCache;
return newCache;
} else {
return cache;
}
}
public String substring(int offsetUtf16, int lengthUtf16) {
byte[] bytes = getBytes();
if (bytes != null) {
return new String(bytes, offsetUtf16, lengthUtf16, StandardCharsets.UTF_16LE);
} else {
return "";
}
}
}

View File

@ -0,0 +1,35 @@
package org.telegram.telegrambots.meta;
import java.nio.charset.StandardCharsets;
public class MemoizedUTF16Substring {
private final MemoizedUTF16String parentString;
private final int offsetUtf16;
private final int lengthUtf16;
private String cache;
public MemoizedUTF16Substring(MemoizedUTF16String parentString, int offsetUtf16, int lengthUtf16) {
this.parentString = parentString;
this.offsetUtf16 = offsetUtf16;
this.lengthUtf16 = lengthUtf16;
}
public String getString() {
if (parentString != null && cache == null) {
String newString = parentString.substring(offsetUtf16, lengthUtf16);
if (newString == null) {
this.cache = "";
return "";
} else {
this.cache = newString;
return newString;
}
} else if (cache != null) {
return cache;
} else {
return "";
}
}
}

View File

@ -5,6 +5,8 @@ import com.fasterxml.jackson.annotation.JsonProperty;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.function.Supplier;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.EqualsAndHashCode;
@ -14,6 +16,8 @@ import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
import lombok.ToString;
import org.telegram.telegrambots.meta.MemoizedUTF16String;
import org.telegram.telegrambots.meta.MemoizedUTF16Substring;
import org.telegram.telegrambots.meta.api.interfaces.BotApiObject;
/**
@ -73,18 +77,19 @@ public class MessageEntity implements BotApiObject {
private User user; ///< Optional. For text_mention only, the mentioned user
@JsonProperty(LANGUAGE_FIELD)
private String language; ///< Optional. For pre only, the programming language of the entity text
@Setter(AccessLevel.NONE)
@JsonIgnore
private String text; ///< Text present in the entity. Computed from offset and length
private MemoizedUTF16Substring text; ///< Text present in the entity. Computed from offset and length
public void computeText(String message) {
if (message != null) {
byte[] bytes = message.getBytes(StandardCharsets.UTF_16LE);
if (bytes.length >= offset && offset >= 0 && length >= 0) {
text = new String(Arrays.copyOfRange(bytes, offset, offset + length),
StandardCharsets.UTF_16LE);
} else {
text = "";
}
public String getText() {
if (text == null) {
return "";
} else {
return text.getString();
}
}
public void computeText(String message) {
text = new MemoizedUTF16Substring(new MemoizedUTF16String(message), offset, length);
}
}