You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@uima.apache.org by GitBox <gi...@apache.org> on 2020/05/18 20:39:51 UTC

[GitHub] [uima-ruta] reckart commented on a change in pull request #9: [UIMA-6231] introducing new static cache for reusing Pattern objects in REGEXP and RegExpRule

reckart commented on a change in pull request #9:
URL: https://github.com/apache/uima-ruta/pull/9#discussion_r426876898



##########
File path: ruta-core/src/main/java/org/apache/uima/ruta/RutaPatternCache.java
##########
@@ -0,0 +1,61 @@
+/*
+ * 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.
+ */
+package org.apache.uima.ruta;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.regex.Pattern;
+
+/**
+ * Cache and centralize creation of regex patterns in order to reuse them in different language
+ * elements
+ *
+ */
+public class RutaPatternCache {
+
+  protected static final Map<String, Pattern> CACHE = new HashMap<>();

Review comment:
       I think it would be better to create a PatternCacheKey class here in which the pattern and eventual flags are modelled explicitly instead of constructing a String key.

##########
File path: ruta-core/src/main/java/org/apache/uima/ruta/RutaPatternCache.java
##########
@@ -0,0 +1,61 @@
+/*
+ * 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.
+ */
+package org.apache.uima.ruta;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.regex.Pattern;
+
+/**
+ * Cache and centralize creation of regex patterns in order to reuse them in different language
+ * elements
+ *
+ */
+public class RutaPatternCache {
+
+  protected static final Map<String, Pattern> CACHE = new HashMap<>();

Review comment:
       Using a static cache without any strategy for removing cached data is a potential memory leak. Unless we have a good reason not to do so, I would suggest using the Caffeine library where we can define different types of expunging strategies (time based, load based, size-based). Or some other type of cache scoping would be required. One possibility could be to attach the cache to a UIMA context available through an UimaContextHolder. In that case, the context could be used as a key in a WeakHashMap causing cached patterns to be garbage collected when the context goes away.




----------------------------------------------------------------
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.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org