Received: by 2002:ab2:6857:0:b0:1ef:ffd0:ce49 with SMTP id l23csp1513840lqp; Fri, 22 Mar 2024 19:09:19 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCV3qkXorY4GqK8mgocNv3g+mmUv6KlL1xHkb4DoNuztXfWK0svgW2EAx2qmIbwwa2cteief9DdKG1O0HV28baXdZPVp+RubtuWGZ2B4xw== X-Google-Smtp-Source: AGHT+IEO3bfTIIba1JQurjM6QIC3LQGQReGgO9HwpY3NxwhMfnsqqlg3rwNZ8MQipfzyO/cO8mLb X-Received: by 2002:a05:6808:2e4e:b0:3c3:c090:5ad8 with SMTP id gp14-20020a0568082e4e00b003c3c0905ad8mr1616498oib.47.1711159759203; Fri, 22 Mar 2024 19:09:19 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1711159759; cv=pass; d=google.com; s=arc-20160816; b=B6zM2kbP8KdcuY3DEkHDD4RxZxLLQ1RmkE2DreVUgvviZxT4xkb52V1+qKEm4Bhl59 8eCb6MWAF5+dgUiGiH8kN4jNOzPKO09OVMfS8fvLhJ2L6tWzdb4ncefoatmdnpZJHo3T jsP341qgu4pdaBi5iPPhH3feTQkJIjmby/s6mvhFE1olBb238ubjqEmlAnJlNzGG1ekp a56mpVSe8uIy5LZPfHMhb1C6RFxMTMatCtPA5z6j5ODm1ntnwCbrKJOsdW9AssR3choE PxEjTOEfUVg6EgNh5MQ376apZvvszW2Sz0YSch8kEN4yYNcKUHmEp5V5BBkcmkksUiy0 rs/w== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:content-disposition:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:message-id:subject:cc :to:from:dkim-signature:date; bh=nLjpN8zrN866nOF+7FuN+/+Gp2UQ+rczaODH2hYXzhg=; fh=xNHfdL6dXcw4Xj2CobF4BJICK/hBQkS4/fw+UOO3WQM=; b=TYcomy5FkdO7QQU8kVfxHBeih8BFcnszrDCgcASp6ww/YUzWLoLVo1cUyutpoN64bw /qdnbe0LCF6VYnqlQ+vzwn9GXyqZX5zHebZ8vIE4w4zJwkJzATaEmSaobNU1zxY90bOJ gnRUP689pvgiWB7l4S/+J1gDptvVjjOwEM/uq8rvXPWzP9QFacd3OvH2+z9yfGrCt1PY 9GE1mhsvJWRanglQqR8TAW8KEPCAAnubGPOpJA8kVqU723b9mokw3W0USKsgMhfGqJMA NrZpp2u4htrI3KncdEXHA5k4nKEXk/RyZSJKI1PaSHwI+db3FUVFiyoB616q5lwGUob/ WSNQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=gaCDbCKk; arc=pass (i=1 spf=pass spfdomain=linux.dev dkim=pass dkdomain=linux.dev dmarc=pass fromdomain=linux.dev); spf=pass (google.com: domain of linux-kernel+bounces-112177-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-112177-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id t16-20020a639550000000b005d72fd9d73asi3172807pgn.602.2024.03.22.19.09.18 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 22 Mar 2024 19:09:19 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-112177-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=gaCDbCKk; arc=pass (i=1 spf=pass spfdomain=linux.dev dkim=pass dkdomain=linux.dev dmarc=pass fromdomain=linux.dev); spf=pass (google.com: domain of linux-kernel+bounces-112177-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-112177-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id D6367282063 for ; Sat, 23 Mar 2024 02:09:18 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 240313FE4; Sat, 23 Mar 2024 02:09:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="gaCDbCKk" Received: from out-177.mta1.migadu.com (out-177.mta1.migadu.com [95.215.58.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 885E6EC2 for ; Sat, 23 Mar 2024 02:09:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=95.215.58.177 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711159748; cv=none; b=BOB4vrvmwejpsoaP/gi4ozvs2RPSuw2OoEnzZ/MN49JpBeMfjGreIEe23Z0/uSFC06UXUM49ejXy8Y5/GERZhQCnoPyhGBvy9LHRYk40lt98Akd8VWVabmrIksuJwDTtFEl2E3pNIQ5QBeooznt5wq5R6pX3G3mDO/f8VdJL9Mo= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711159748; c=relaxed/simple; bh=RYp/JsSSfKdzCtxZ1QFJG90hGxBYwOT+js6mYmwvLoI=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=cgCLjzx6PCS7Sm9xDjJGpTEgHx91HSyERpYzk4Uc/JFXTGxEDbJSxe1UXiuaI3WwyUHxfg3NdQRknOcxtJo/XmvbsdqwzBb0iMETSzL465KnXihM1z3Ru3HhQplz54tAMruSdyb53qDdZy2EiXTpZzTxZK0Qd2mLP489Ef7Qwhs= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=gaCDbCKk; arc=none smtp.client-ip=95.215.58.177 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Date: Fri, 22 Mar 2024 22:08:59 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1711159744; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=nLjpN8zrN866nOF+7FuN+/+Gp2UQ+rczaODH2hYXzhg=; b=gaCDbCKkN+qK3InSbhyhJcc+wrz8L0wfUqMZiAEkaM4egtEUJCElSxG7mwOXsA29YHs/vq lYUhrNWVqxOEUvoFtsu4sMTNaCIiYNxRddKWtt1dktqSYzMmwZmkBSBDjr4Ixdtn4GVpf3 ZfurCuI1A0kG1Q6KDtMuW5JMIOhbLY4= X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. From: Kent Overstreet To: Kuan-Wei Chiu Cc: colyli@suse.de, msakai@redhat.com, peterz@infradead.org, mingo@redhat.com, acme@kernel.org, namhyung@kernel.org, akpm@linux-foundation.org, bfoster@redhat.com, mark.rutland@arm.com, alexander.shishkin@linux.intel.com, jolsa@kernel.org, irogers@google.com, adrian.hunter@intel.com, jserv@ccns.ncku.edu.tw, dm-devel@lists.linux.dev, linux-bcache@vger.kernel.org, linux-kernel@vger.kernel.org, linux-bcachefs@vger.kernel.org, linux-perf-users@vger.kernel.org Subject: Re: [PATCH v2 04/15] lib min_heap: Add type safe interface Message-ID: References: <20240320145417.336208-1-visitorckw@gmail.com> <20240320145417.336208-5-visitorckw@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Migadu-Flow: FLOW_OUT On Sat, Mar 23, 2024 at 09:53:08AM +0800, Kuan-Wei Chiu wrote: > On Fri, Mar 22, 2024 at 02:23:26PM -0400, Kent Overstreet wrote: > > On Sat, Mar 23, 2024 at 01:02:29AM +0800, Kuan-Wei Chiu wrote: > > > On Thu, Mar 21, 2024 at 05:22:14PM -0400, Kent Overstreet wrote: > > > > On Thu, Mar 21, 2024 at 07:57:47PM +0800, Kuan-Wei Chiu wrote: > > > > > On Wed, Mar 20, 2024 at 04:56:57PM -0400, Kent Overstreet wrote: > > > > > > On Wed, Mar 20, 2024 at 10:54:06PM +0800, Kuan-Wei Chiu wrote: > > > > > > > Introduce a type-safe interface for min_heap by adding small macro > > > > > > > wrappers around functions and using a 0-size array to store type > > > > > > > information. This enables the use of __minheap_cast and > > > > > > > __minheap_obj_size macros for type casting and obtaining element size. > > > > > > > The implementation draws inspiration from generic-radix-tree.h, > > > > > > > eliminating the need to pass element size in min_heap_callbacks. > > > > > > > > > > > > let's avoid the heap->heap.nr - darray (fs/bcachefs/darray.h) has a > > > > > > trick for that. All heaps have the same memory layout, so we can just > > > > > > cast to a void pointer heap to get something the C code can use. > > > > > > > > > > > If I understand correctly, you're suggesting adding APIs similar to > > > > > darray_top(), darray_first(), and darray_last() within min_heap and > > > > > having them return a pointer. However, some users are using heap.nr in > > > > > conditional statements instead of utilizing heap.nr for memory > > > > > operations, so returning pointers may not be as convenient. What about > > > > > adding get and set functions for nr instead? > > > > > > > > No, I mean not having separate inner and outer types. Want me to sketch > > > > something out? > > > > > > Based on your suggestion, I've come up with the following code snippet: > > > > > > #define MIN_HEAP_PREALLOCATED(_type, _name, _nr) \ > > > struct _name { \ > > > int nr; \ > > > int size; \ > > > _type *data; \ > > > _type preallocated[_nr]; \ > > > }; > > > > > > #define MIN_HEAP(_type, _name) MIN_HEAP_PREALLOCATED(_type, _name, 0) > > > > > > typdef MIN_HEAP(char, _) min_heap_char; > > > > > > static __always_inline > > > void min_heap_init(min_heap_char *heap, void *data, int size) > > > { > > > heap->nr = 0; > > > heap->size = size; > > > heap->data = size <= ARRAY_SIZE(heap->preallocated) ? heap->preallocated : data; > > > } > > > > > > But I'm not sure how to implement other inline functions like > > > min_heap_push or min_heap_pop if I do that, unless they are rewritten > > > using macros. Also, I'm not sure how to make the less and swp functions > > > in the min_heap_callbacks not use void * type parameters. Or perhaps I > > > misunderstood your meaning again. If you could sketch out your idea or > > > have a better approach, it would be a great help to me. Any guidance > > > would be greatly appreciated. > > > > No, you're on the right track. To call C functions on different types of > > heaps you have to cast them all to a common type, say HEAP(char), also > > pass the element size as a paremeter (which you had to do previously > > anyways). > > The other question I want to ask is, I'm not sure how this relates to > avoiding the heap->heap.nr. In cases where we need to know the current > number of elements in the heap, don't we still need to use the same > method to determine the number of elements? Yes, but this eliminates the nested types; so it's just heap->nr. It's a pretty minor detail, cosmetic really, but I managed it in darray so it'd be nice to have here as well :)