Skip to content

Alter parseString to accumulate StringBuilder appends vs individual calls - #34

Merged
ctrueden merged 3 commits into
scijava:mainfrom
JaredDavis22:parseString
Nov 17, 2026
Merged

ctrueden merged 3 commits into
scijava:mainfrom
JaredDavis22:parseString

Conversation

@JaredDavis22

Copy link
Copy Markdown
Contributor

Code change to batch StringBuilder appends vs one per character.

Benchmark of STRING("'hello world'") shows a 36% improvement .

It passes the existing tests and my test batch. It needs another set of eyes on it before it is trusted.

Screenshot 2026-07-28 at 9 13 57 AM

@JaredDavis22

Copy link
Copy Markdown
Contributor Author

fasterParsington fork has this in main.

https://github.com/JaredDavis22/fasterParsington

An empty range append is a no-op, so the length checks are unnecessary.
And since every escape sequence resets the segment start before the
next backslash or quote is reached, the -1 sentinel is unnecessary too.

Quick benchmark results (STRING token in LiteralsBenchmark):

┌─────────────────────────────────┬─────────────────┬────────────────┐
│              Version            │  Time per call  │ Change vs main │
├─────────────────────────────────┼─────────────────┼────────────────┤
│ main (7f21508)                  │ 35.16 ± 0.16 ns │ —              │
│ First commit alone (b3bfe8b)    │ 32.12 ± 0.19 ns │ −8.6%          │
│ With the tidy-ups (this commit) │ 27.35 ± 0.31 ns │ −22.2%         │
└─────────────────────────────────┴─────────────────┴────────────────┘

Settings were the same each time: 3 forks, 5 warmup and 10
measured iterations of 2 seconds each, with the GC profiler on.

Assisted-by: Claude Code (Opus 5.5)
Escapes adjacent to the closing quote, consecutive escapes,
empty strings, verbatim single-quoted strings, and the final
position after parsing a string containing escapes.

Assisted-by: Claude Code (Opus 5.5)
@ctrueden
ctrueden merged commit 3758719 into scijava:main Nov 17, 2026
1 check passed
@JaredDavis22

Copy link
Copy Markdown
Contributor Author

Interesting. I used Claude to speed up the parseString today and got a different answer.

It passes the tests (and the new ones added here)

I have not pushed it yet in the fork nor done a benchmark run.

	public static String parseString(final CharSequence s, final Position pos) {
		final char quote = pos.ch(s);
		if (quote != '"' && quote != '\'') return null;

		final int len = s.length();
		final int start = pos.get() + 1;
		int index = start;

		// Single quotes: verbatim, no escapes -- just find the closing quote.
		if (quote == '\'') {
			while (index < len && s.charAt(index) != '\'') index++;
			if (index >= len) pos.die("Unclosed string literal");
			pos.set(index + 1);
			return s.subSequence(start, index).toString();
		}

		// Double quotes, fast path: scan until closing quote or first backslash.
		char c = 0;
		while (index < len && (c = s.charAt(index)) != '"' && c != '\\') index++;
		if (index >= len) pos.die("Unclosed string literal");
		if (c == '"') {
			pos.set(index + 1);
			return s.subSequence(start, index).toString();
		}

		// Slow path: at least one escape. Builder sized to avoid early regrowth.
		final StringBuilder sb = new StringBuilder(index - start + 16);
		sb.append(s, start, index);

		while (true) {
			// Here s.charAt(index) == '\\'
			index++;
			if (index >= len) pos.die("Unclosed string literal");
			c = s.charAt(index);

			if (isOctal(c)) { // octal sequence: up to 3 digits, max \377
				int value = c - '0';
				index++;
				if (index < len && isOctal(s.charAt(index))) {
					value = (value << 3) + (s.charAt(index) - '0');
					index++;
					if (c <= '3' && index < len && isOctal(s.charAt(index))) {
						value = (value << 3) + (s.charAt(index) - '0');
						index++;
					}
				}
				sb.append((char) value);
			}
			else {
				switch (c) {
					case 'b':  sb.append('\b'); break;
					case 't':  sb.append('\t'); break;
					case 'n':  sb.append('\n'); break;
					case 'f':  sb.append('\f'); break;
					case 'r':  sb.append('\r'); break;
					case '"':  sb.append('"');  break;
					case '\\': sb.append('\\'); break;
					case 'u': { // unicode sequence
						final int v =
								(Character.digit(hex(s, pos, index + 1), 16) << 12) |
								(Character.digit(hex(s, pos, index + 2), 16) << 8)  |
								(Character.digit(hex(s, pos, index + 3), 16) << 4)  |
								 Character.digit(hex(s, pos, index + 4), 16);
						sb.append((char) v);
						index += 4;
						break;
					}
					default:
						pos.die("Invalid escape sequence");
				}
				index++;
			}

			// Copy the next run of plain characters in one bulk append.
			final int segStart = index;
			while (index < len && (c = s.charAt(index)) != '"' && c != '\\') index++;
			if (index >= len) pos.die("Unclosed string literal");
			if (index > segStart) sb.append(s, segStart, index);
			if (c == '"') break;
		}

		pos.set(index + 1);
		return sb.toString();
	}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants