resource-identifier icon indicating copy to clipboard operation
resource-identifier copied to clipboard

ResourceIdentifier accessor methods allocate

Open pkoenig10 opened this issue 4 years ago • 0 comments

As part of a recent change to our internal filesystem product, we began checking all ResourceIdentifier log args to determine whether they are safe. We do this by checking the service and locator components of the ResourceIdentifier.

Unfortunately, because we log ResourceIdentifier values very frequently, this resulted in a lot of calls to getService. And getService allocates a substring. As a results, we saw a huge increase in GC time. In a 1 minute JFR recording we saw 48.2 GB of String allocations (43% of all sampled allocations), most of which from getService.

It's fairly unexpected and unintuitive that a method like getService, which appears to be a cheap accessor, allocates.

I'm not sure I have a good solution here, given that Java doesn't have a concept of references and can't return something like a string slice. Nevermind the fact that these methods currently return String and we can't break the ABI here.

Memoizing the substrings here probably isn't a solution either because:

  • This would significantly increase the size of ResourceIdentifier. Given the ubiquity of ResourceIdentifier, especially in things like caches, this would likely cause memory pressure elsewhere.
  • We typically won't call getService more than a few time for a given ResourceIdentifier object, so in practice the benefits here would likely be minimal.

For more details, see https://g.p.b/foundry/c/pull/11844

pkoenig10 avatar Jun 30 '21 23:06 pkoenig10