NightOwl888 commented on code in PR #1089: URL: https://github.com/apache/lucenenet/pull/1089#discussion_r1907235951
########## src/Lucene.Net/Support/Text/EncodingExtensions.cs: ########## @@ -0,0 +1,51 @@ +using System.Text; + +namespace Lucene.Net.Support.Text +{ + /* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + /// <summary> + /// Extension methods for <see cref="Encoding"/>. + /// </summary> + internal static class EncodingExtensions + { + /// <summary> + /// Returns a new <see cref="Encoding"/> instance with the <see cref="DecoderFallback"/> set to throw + /// an exception when an invalid byte sequence is encountered. + /// <para /> + /// This is equivalent to Java's <c>CodingErrorAction.REPORT</c> for both <c>onMalformedInput</c> and + /// <c>onUnmappableCharacter</c> and will throw a <see cref="DecoderFallbackException"/> when failing + /// to decode a string. This exception is equivalent to Java's <c>CharacterCodingException</c>, which is + /// a base exception type for both <c>MalformedInputException</c> and <c>UnmappableCharacterException</c>. + /// Thus, to translate Java code that catches any of those exceptions, you can catch + /// <see cref="DecoderFallbackException"/>. + /// </summary> + /// <param name="encoding">The encoding to clone and set the fallback on.</param> + /// <returns>A new <see cref="Encoding"/> instance with the fallback set to throw an exception.</returns> + /// <remarks> + /// Note that it is necessary to return a new, cloned <see cref="Encoding"/> instance because + /// the <see cref="Encoding.DecoderFallback"/> property is read-only without cloning. + /// </remarks> + public static Encoding WithDecoderExceptionFallback(this Encoding encoding) + { + Encoding newEncoding = (Encoding)encoding.Clone(); Review Comment: I like the syntax, but it does a heap allocation every time the extension method is called. Let's change it to use a `ConcurrentDictionary` as a cache. ```c# internal static class EncodingExtensions { private static readonly ConcurrentDictionary<Encoding, Encoding> exceptionFallbackCache = new ConcurrentDictionary<Encoding, Encoding>(); public static Encoding WithDecoderExceptionFallback(this Encoding encoding) { return exceptionFallbackCache.GetOrAdd(encoding, e => { Encoding newEncoding = (Encoding)e.Clone(); newEncoding.DecoderFallback = DecoderFallback.ExceptionFallback; return newEncoding; }); } } ``` `GetOrAdd()` isn't atomic so there is a slight chance we will get an extra instance if 2 threads call it at the same time, but it doesn't affect correctness and isn't very likely to happen. According to ChatGPT, the `GetHashCode()` method of `Encoding` is not particularly well optimized, so it might be worth it to make a key based on the `CodePage` and `DecoderFallback` setting to avoid that overhead. But it would need benchmarking to see whether that makes much of a difference. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: dev-unsubscr...@lucenenet.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org