|
1 | 1 | /* |
2 | | - * Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved. |
| 2 | + * Copyright (c) 2000, 2021, Oracle and/or its affiliates. All rights reserved. |
3 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 | 4 | * |
5 | 5 | * This code is free software; you can redistribute it and/or modify it |
@@ -523,16 +523,25 @@ static byte[] encode(byte coder, byte[] val) { |
523 | 523 | private static native void err(String msg); |
524 | 524 |
|
525 | 525 | /* The cached Result for each thread */ |
526 | | - private static final ThreadLocal<StringCoding.Result> |
| 526 | + private static final ThreadLocal<SoftReference<Result>> |
527 | 527 | resultCached = new ThreadLocal<>() { |
528 | | - protected StringCoding.Result initialValue() { |
529 | | - return new StringCoding.Result(); |
| 528 | + protected SoftReference<Result> initialValue() { |
| 529 | + return new SoftReference<>(new Result()); |
530 | 530 | }}; |
| 531 | + private static Result resultCached() { |
| 532 | + SoftReference<Result> sr = resultCached.get(); |
| 533 | + Result r; |
| 534 | + if (sr == null || (r = sr.get()) == null) { |
| 535 | + r = new Result(); |
| 536 | + resultCached.set(new SoftReference<>(r)); |
| 537 | + } |
| 538 | + return r; |
| 539 | + } |
531 | 540 |
|
532 | 541 | ////////////////////////// ascii ////////////////////////////// |
533 | 542 |
|
534 | 543 | private static Result decodeASCII(byte[] ba, int off, int len) { |
535 | | - Result result = resultCached.get(); |
| 544 | + Result result = resultCached(); |
536 | 545 | if (COMPACT_STRINGS && !hasNegatives(ba, off, len)) { |
537 | 546 | return result.with(Arrays.copyOfRange(ba, off, off + len), |
538 | 547 | LATIN1); |
@@ -582,7 +591,7 @@ private static byte[] encodeASCII(byte coder, byte[] val) { |
582 | 591 | ////////////////////////// latin1/8859_1 /////////////////////////// |
583 | 592 |
|
584 | 593 | private static Result decodeLatin1(byte[] ba, int off, int len) { |
585 | | - Result result = resultCached.get(); |
| 594 | + Result result = resultCached(); |
586 | 595 | if (COMPACT_STRINGS) { |
587 | 596 | return result.with(Arrays.copyOfRange(ba, off, off + len), LATIN1); |
588 | 597 | } else { |
@@ -720,13 +729,13 @@ private static void throwUnmappable(byte[] val) { |
720 | 729 | private static Result decodeUTF8(byte[] src, int sp, int len, boolean doReplace) { |
721 | 730 | // ascii-bais, which has a relative impact to the non-ascii-only bytes |
722 | 731 | if (COMPACT_STRINGS && !hasNegatives(src, sp, len)) |
723 | | - return resultCached.get().with(Arrays.copyOfRange(src, sp, sp + len), |
| 732 | + return resultCached().with(Arrays.copyOfRange(src, sp, sp + len), |
724 | 733 | LATIN1); |
725 | 734 | return decodeUTF8_0(src, sp, len, doReplace); |
726 | 735 | } |
727 | 736 |
|
728 | 737 | private static Result decodeUTF8_0(byte[] src, int sp, int len, boolean doReplace) { |
729 | | - Result ret = resultCached.get(); |
| 738 | + Result ret = resultCached(); |
730 | 739 |
|
731 | 740 | int sl = sp + len; |
732 | 741 | int dp = 0; |
@@ -1057,7 +1066,7 @@ static String newStringNoRepl1(byte[] src, Charset cs) { |
1057 | 1066 | } catch (CharacterCodingException x) { |
1058 | 1067 | throw new IllegalArgumentException(x); // todo |
1059 | 1068 | } |
1060 | | - Result ret = resultCached.get().with(ca, 0, cb.position()); |
| 1069 | + Result ret = resultCached().with(ca, 0, cb.position()); |
1061 | 1070 | return new String(ret.value, ret.coder); |
1062 | 1071 | } |
1063 | 1072 |
|
|
0 commit comments