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 16:33:27 UTC

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

pkluegl opened a new pull request #9:
URL: https://github.com/apache/uima-ruta/pull/9


   


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



[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

Posted by GitBox <gi...@apache.org>.
reckart commented on a change in pull request #9:
URL: https://github.com/apache/uima-ruta/pull/9#discussion_r432087654



##########
File path: ruta-core/src/main/java/org/apache/uima/ruta/cache/RutaPatternCache.java
##########
@@ -0,0 +1,76 @@
+/*
+ * 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.cache;
+
+import java.util.concurrent.ConcurrentMap;
+import java.util.concurrent.TimeUnit;
+import java.util.regex.Pattern;
+
+import com.github.benmanes.caffeine.cache.Caffeine;
+import com.github.benmanes.caffeine.cache.LoadingCache;
+
+/**
+ * Cache and centralize creation of regex patterns in order to reuse them in different language
+ * elements
+ *
+ */
+public class RutaPatternCache {
+
+  private static final LoadingCache<PatternCacheKey, Pattern> CACHE = Caffeine.newBuilder()

Review comment:
       Might care to make these parameters configurable at some point.

##########
File path: ruta-core/src/main/java/org/apache/uima/ruta/cache/RutaPatternCache.java
##########
@@ -0,0 +1,76 @@
+/*
+ * 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.cache;
+
+import java.util.concurrent.ConcurrentMap;
+import java.util.concurrent.TimeUnit;
+import java.util.regex.Pattern;
+
+import com.github.benmanes.caffeine.cache.Caffeine;
+import com.github.benmanes.caffeine.cache.LoadingCache;
+
+/**
+ * Cache and centralize creation of regex patterns in order to reuse them in different language
+ * elements
+ *
+ */
+public class RutaPatternCache {
+
+  private static final LoadingCache<PatternCacheKey, Pattern> CACHE = Caffeine.newBuilder()
+          .expireAfterAccess(6, TimeUnit.HOURS).expireAfterWrite(12, TimeUnit.HOURS)
+          .maximumSize(10_000).build(k -> createPattern(k));
+
+  /**
+   * 
+   * @param patternString
+   *          - string of the pattern

Review comment:
       Why is there a `-` here?

##########
File path: ruta-core/src/main/java/org/apache/uima/ruta/rule/RegExpRule.java
##########
@@ -85,7 +86,7 @@ public ScriptApply apply(RutaStream stream, InferenceCrowd crowd) {
     Map<Integer, List<Type>> groupTypes = getGroup2Types(context, stream);
     Map<Integer, Map<Type, Map<String, Object>>> fa = getFeatureAssignmentMap(stream);
 
-    Pattern pattern = Pattern.compile(regexpString, Pattern.MULTILINE | Pattern.DOTALL);
+    Pattern pattern = RutaPatternCache.getPattern(regexpString, false);

Review comment:
       How about making the `getPattern` API more like the `compile` API by having the `MULTILINE | DOTALL` here instead of the `false`? That would make the code more readable (what's false anyway?) and more versatile.

##########
File path: ruta-core/src/main/java/org/apache/uima/ruta/cache/RutaPatternCache.java
##########
@@ -0,0 +1,76 @@
+/*
+ * 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.cache;
+
+import java.util.concurrent.ConcurrentMap;
+import java.util.concurrent.TimeUnit;
+import java.util.regex.Pattern;
+
+import com.github.benmanes.caffeine.cache.Caffeine;
+import com.github.benmanes.caffeine.cache.LoadingCache;
+
+/**
+ * Cache and centralize creation of regex patterns in order to reuse them in different language
+ * elements
+ *
+ */
+public class RutaPatternCache {
+
+  private static final LoadingCache<PatternCacheKey, Pattern> CACHE = Caffeine.newBuilder()
+          .expireAfterAccess(6, TimeUnit.HOURS).expireAfterWrite(12, TimeUnit.HOURS)
+          .maximumSize(10_000).build(k -> createPattern(k));
+
+  /**
+   * 
+   * @param patternString
+   *          - string of the pattern
+   * @param ignore
+   *          - additional option to compile the pattern with case ignore option activated
+   * @return compiled pattern for the given arguments
+   */
+  public static Pattern getPattern(String patternString, boolean ignore) {
+
+    return CACHE.get(new PatternCacheKey(patternString, ignore));
+  }
+
+  private static synchronized Pattern createPattern(PatternCacheKey key) {
+    if (key.isIgnoreCase()) {
+      return Pattern.compile(key.getPatternString(),
+              Pattern.MULTILINE + Pattern.DOTALL + Pattern.CASE_INSENSITIVE + Pattern.UNICODE_CASE);
+    }
+    return Pattern.compile(key.getPatternString(), Pattern.MULTILINE + Pattern.DOTALL);
+  }
+
+  /**
+   * 
+   * @return view on cache map
+   */
+  public static ConcurrentMap<PatternCacheKey, Pattern> getChacheMap() {

Review comment:
       Typo. Do you need to expose a modifiable map here? If not, I'd suggest applying `Collections.unmodifiableMap()` here.




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



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

Posted by GitBox <gi...@apache.org>.
reckart merged pull request #9:
URL: https://github.com/apache/uima-ruta/pull/9


   


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



[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

Posted by GitBox <gi...@apache.org>.
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



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

Posted by GitBox <gi...@apache.org>.
reckart commented on pull request #9:
URL: https://github.com/apache/uima-ruta/pull/9#issuecomment-638263306


   Jenkins, can you test this please?


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