git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH JGIT] Method invokes inefficient new String(String) constructor

From
Yann Simon <yann.simon.fr@gmail.com>
Date
Mar 19, 2009, 16:44 UTC
Message-ID
<551f769b0903190944ofb03d14u1be316e1dacc1f86@mail.gmail.com>
In-Reply-To
<20090319160102.GQ23521@spearce.org>
2009/3/19 Shawn O. Pearce <spearce@spearce.org>:
Show 9 quoted lines
> The line in question, p, is from the packed-refs file and
> contains the entire SHA-1 in hex form at the beginning of it.
> We've converted that into binary as an ObjectId, it uses 1/4 the
> space of the string portion.
>
> The Ref object, its ObjectId, and its name string, are going to be
> cached in a Map, probably long-term.  We're better off shedding the
> 80 bytes of memory used to hold the hex SHA-1 then risk substring()
> deciding its "faster" to reuse the char[] then to make a copy of it.

Thany you for the explanation. I learn something, and my tests confirm it. The p.substring(...) can keep the entire array of char by only updating the intern offset value. new String(p.subtring(...)) make sure that the variable contains only the final chars.

Yann
Previous: Shawn O. PearceNext: Yann Simon
Message 3 of 10 in “Method invokes inefficient new String(String) constructor”
  1. Method invokes inefficient new String(String) constructorYann Simon, Mar 19, 2009
  2. Shawn O. PearceMar 19, 2009
  3. Yann SimonMar 19, 2009
  4. Yann SimonJul 9, 2009
  5. FindBugs: don't use new String(String) in RefDatabaseShawn O. Pearce, Jul 10, 2009
  6. Yann SimonJul 13, 2009
  7. FindBugs: don't use new String(String) in RefDatabaseShawn O. Pearce, Jul 13, 2009
  8. Robin RosenbergJul 21, 2009
  9. Shawn O. PearceJul 21, 2009
  10. Robin RosenbergJul 21, 2009

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.