Optimization: Realm doing regex string split and String[] allocation on every .equalTo()
See original GitHub issueFor a simple query such as r.where(My.class).equalTo("id", 1).findfirst()
there is a cost of a regex string split causing a String[] allocation even if the field in question doesn’t contain any “.” separators.
private List<String> parseFieldDescription(String fieldDescription) {
if (fieldDescription == null || fieldDescription.equals("")) {
throw new IllegalArgumentException("Invalid query: field name is empty");
}
if (fieldDescription.endsWith(".")) {
throw new IllegalArgumentException("Invalid query: field name must not end with a period ('.')");
}
return Arrays.asList(fieldDescription.split("\\."));
}
parseFieldDescription:272, FileDescriptor (regex split + String[] allocation)
...
getColumnIndices:428, RealmObjectSchema
...
equalTo:385 RealmQuery
As it is there is no apparent issue but imagine if the small query is in a scrolling RecyclerView done on every onBindView() pass. Now, we have an unnecessary allocation problem in a tight loop. I would suggest:
-
avoid any parsing if we know ahead of time this field cannot contain “.”. I believe the transformer can keep a mapped cache of ALL mapped fields that do contain a “.” and any negative match against it will skip this parsing code.
-
Cache the parseFieldDescription in a map and only dynamically parse on first attempt for this Realm.class field.
Issue Analytics
- State:
- Created 6 years ago
- Comments:9 (9 by maintainers)
@diegomontoya https://github.com/realm/realm-java/pull/5952 Should fix it I believe?
@cmelchior Thanks for the hard work. =) I will report back once this fix is released in the next stable realm version to see if it helped to resolve our GC issues.