From dev-return-73534-archive-asf-public=cust-asf.ponee.io@zookeeper.apache.org Sat Sep 22 06:38:45 2018 Return-Path: X-Original-To: archive-asf-public@cust-asf.ponee.io Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by mx-eu-01.ponee.io (Postfix) with SMTP id 3DADE180679 for ; Sat, 22 Sep 2018 06:38:45 +0200 (CEST) Received: (qmail 61854 invoked by uid 500); 22 Sep 2018 04:38:38 -0000 Mailing-List: contact dev-help@zookeeper.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@zookeeper.apache.org Delivered-To: mailing list dev@zookeeper.apache.org Received: (qmail 57134 invoked by uid 99); 22 Sep 2018 04:38:35 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Sat, 22 Sep 2018 04:38:35 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 495ADE11DE; Sat, 22 Sep 2018 04:38:35 +0000 (UTC) From: hanm To: dev@zookeeper.apache.org Reply-To: dev@zookeeper.apache.org References: In-Reply-To: Subject: [GitHub] zookeeper pull request #590: [ZOOKEEPER-1177] Add the memory optimized watch... Content-Type: text/plain Message-Id: <20180922043835.495ADE11DE@git1-us-west.apache.org> Date: Sat, 22 Sep 2018 04:38:35 +0000 (UTC) Github user hanm commented on a diff in the pull request: https://github.com/apache/zookeeper/pull/590#discussion_r219659865 --- Diff: src/java/main/org/apache/zookeeper/server/util/BitMap.java --- @@ -0,0 +1,125 @@ +/** + * 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.zookeeper.server.util; + +import java.util.Map; +import java.util.HashMap; +import java.util.BitSet; +import java.util.concurrent.locks.ReentrantReadWriteLock; + +/** + * This is a helper class to maintain the bit to specific value and the + * reversed value to bit mapping. + */ +public class BitMap { + + private final Map value2Bit = new HashMap(); + private final Map bit2Value = new HashMap(); + + private final BitSet freedBitSet = new BitSet(); + private Integer nextBit = Integer.valueOf(0); + + private final ReentrantReadWriteLock rwLock = new ReentrantReadWriteLock(); + + public Integer add(T value) { + Integer bit = getBit(value); + if (bit != null) { + return bit; + } --- End diff -- I am also wondering, if this optimization is indeed useful, why not do the same for the `remove` methods, that is, check and return early with a read lock before trying to acquire a write lock. ---