question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Optimization: Realm doing regex string split and String[] allocation on every .equalTo()

See original GitHub issue

For 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:

  1. 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.

  2. Cache the parseFieldDescription in a map and only dynamically parse on first attempt for this Realm.class field.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:9 (9 by maintainers)

github_iconTop GitHub Comments

3reactions
cmelchiorcommented, May 15, 2018
0reactions
diegomontoyacommented, May 25, 2018

@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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why doesn't Java optimize String.split("regex") calls for String ...
Satisfying your request with a special case for String.split() ... The claim is that the Just-in-time compiler can do all the optimisation.
Read more >
String Functions | Elasticsearch Guide [8.5] | Elastic
Description: Returns a string equal to that in string_exp , with all uppercase characters converted to lowercase. SELECT LCASE('Elastic'); LCASE('Elastic') ...
Read more >
4. Optimize String Use: A Case Study - Optimized C++ [Book]
In a COW string, the dynamically allocated storage can be shared between strings. A reference count lets each string know if it is...
Read more >
Regex.Split Method (System.Text.RegularExpressions)
Splits an input string into an array of substrings at the positions defined by a regular expression pattern specified in the Regex constructor....
Read more >
split() Function in Java | Learn How does the split ... - eduCBA
In Java Split() function, we split the string using various methods; ... limit = 0: If the limit is set as equal to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found