From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <dcsapak@zita.proxmox.com>
Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68])
 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
 key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256)
 (No client certificate requested)
 by lists.proxmox.com (Postfix) with ESMTPS id 183D362406
 for <pbs-devel@lists.proxmox.com>; Wed, 16 Sep 2020 14:09:47 +0200 (CEST)
Received: from firstgate.proxmox.com (localhost [127.0.0.1])
 by firstgate.proxmox.com (Proxmox) with ESMTP id 0694D2388E
 for <pbs-devel@lists.proxmox.com>; Wed, 16 Sep 2020 14:09:47 +0200 (CEST)
Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com
 [212.186.127.180])
 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
 key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256)
 (No client certificate requested)
 by firstgate.proxmox.com (Proxmox) with ESMTPS id 29B172387C
 for <pbs-devel@lists.proxmox.com>; Wed, 16 Sep 2020 14:09:46 +0200 (CEST)
Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1])
 by proxmox-new.maurer-it.com (Proxmox) with ESMTP id DE4A5448D6
 for <pbs-devel@lists.proxmox.com>; Wed, 16 Sep 2020 14:09:45 +0200 (CEST)
From: Dominik Csapak <d.csapak@proxmox.com>
To: pbs-devel@lists.proxmox.com
Date: Wed, 16 Sep 2020 14:09:44 +0200
Message-Id: <20200916120945.12073-1-d.csapak@proxmox.com>
X-Mailer: git-send-email 2.20.1
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
X-SPAM-LEVEL: Spam detection results:  0
 AWL -0.176 Adjusted score from AWL reputation of From: address
 KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment
 KAM_LAZY_DOMAIN_SECURITY 1 Sending domain does not have any anti-forgery
 methods
 NO_DNS_FOR_FROM         0.379 Envelope sender has no MX or A DNS records
 RCVD_IN_DNSWL_MED        -2.3 Sender listed at https://www.dnswl.org/,
 medium trust
 SPF_HELO_NONE           0.001 SPF: HELO does not publish an SPF Record
 SPF_NONE                0.001 SPF: sender does not publish an SPF Record
 URIBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to URIBL was blocked. See
 http://wiki.apache.org/spamassassin/DnsBlocklists#dnsbl-block for more
 information. [mod.rs]
Subject: [pbs-devel] [PATCH proxmox 1/2] api-macro: fix broken binary ident
 search
X-BeenThere: pbs-devel@lists.proxmox.com
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: Proxmox Backup Server development discussion
 <pbs-devel.lists.proxmox.com>
List-Unsubscribe: <https://lists.proxmox.com/cgi-bin/mailman/options/pbs-devel>, 
 <mailto:pbs-devel-request@lists.proxmox.com?subject=unsubscribe>
List-Archive: <http://lists.proxmox.com/pipermail/pbs-devel/>
List-Post: <mailto:pbs-devel@lists.proxmox.com>
List-Help: <mailto:pbs-devel-request@lists.proxmox.com?subject=help>
List-Subscribe: <https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel>, 
 <mailto:pbs-devel-request@lists.proxmox.com?subject=subscribe>
X-List-Received-Date: Wed, 16 Sep 2020 12:09:47 -0000

the 'properties_' list is sorted by the the literal string of a
fieldname, but we binary-search for the 'ident_str' (which may be
different, since we map '-' to '_' for example)

by creating a hashmap to map from ident to index, we can do a simple
lookup in that case that will work

benchmarks showed no measurable performance difference

Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
---
 proxmox-api-macro/src/api/mod.rs | 25 +++++++++++++------------
 1 file changed, 13 insertions(+), 12 deletions(-)

diff --git a/proxmox-api-macro/src/api/mod.rs b/proxmox-api-macro/src/api/mod.rs
index 2d2dada..0071e81 100644
--- a/proxmox-api-macro/src/api/mod.rs
+++ b/proxmox-api-macro/src/api/mod.rs
@@ -8,6 +8,7 @@
 //! The handling of methods vs type definitions happens in their corresponding submodules.
 
 use std::convert::{TryFrom, TryInto};
+use std::collections::HashMap;
 
 use anyhow::Error;
 
@@ -378,12 +379,14 @@ impl SchemaItem {
 /// Contains a sorted list of properties:
 pub struct SchemaObject {
     properties_: Vec<(FieldName, bool, Schema)>,
+    ident_hash: HashMap<String, usize>,
 }
 
 impl SchemaObject {
     pub fn new() -> Self {
         Self {
             properties_: Vec::new(),
+            ident_hash: HashMap::new(),
         }
     }
 
@@ -394,6 +397,9 @@ impl SchemaObject {
 
     fn sort_properties(&mut self) {
         self.properties_.sort_by(|a, b| (a.0).cmp(&b.0));
+        for (idx, prop) in self.properties_.iter().enumerate() {
+            self.ident_hash.insert(prop.0.as_ident_str().to_string(), idx);
+        }
     }
 
     fn try_extract_from(obj: &mut JSONObject) -> Result<Self, syn::Error> {
@@ -422,6 +428,7 @@ impl SchemaObject {
                         Ok(properties)
                     },
                 )?,
+            ident_hash: HashMap::new(),
         };
         this.sort_properties();
         Ok(this)
@@ -439,22 +446,16 @@ impl SchemaObject {
     }
 
     fn find_property_by_ident(&self, key: &str) -> Option<&(FieldName, bool, Schema)> {
-        match self
-            .properties_
-            .binary_search_by(|p| p.0.as_ident_str().cmp(key))
-        {
-            Ok(idx) => Some(&self.properties_[idx]),
-            Err(_) => None,
+        match self.ident_hash.get(key) {
+            Some(idx) => Some(&self.properties_[*idx]),
+            None => None,
         }
     }
 
     fn find_property_by_ident_mut(&mut self, key: &str) -> Option<&mut (FieldName, bool, Schema)> {
-        match self
-            .properties_
-            .binary_search_by(|p| p.0.as_ident_str().cmp(key))
-        {
-            Ok(idx) => Some(&mut self.properties_[idx]),
-            Err(_) => None,
+        match self.ident_hash.get(key) {
+            Some(idx) => Some(&mut self.properties_[*idx]),
+            None => None,
         }
     }
 
-- 
2.20.1